Properly check for stair type.

Add upside down chair sit prevention
This commit is contained in:
cnaude 2012-11-23 01:39:10 -07:00
parent 53ec8b02db
commit edf2434b30
4 changed files with 65 additions and 89 deletions

View File

@ -3,13 +3,13 @@
# A list of all compatible block and item names: http://bit.ly/AmJgMb. # A list of all compatible block and item names: http://bit.ly/AmJgMb.
# ------ # ------
# allowed-blocks: Set the blocks you want to be able to sit down on. Currently, only stairs-blocks are working. # allowed-blocks: Set the blocks you want to be able to sit down on. Currently, only stairs-blocks are working.
# item: Set the item you want to sit on (default is LEVER, because it is quite small so that you can't really see it).
# auto-rotate: If set to true, you are automatically rotated to the descending face of the stairs-block when sitting down. # auto-rotate: If set to true, you are automatically rotated to the descending face of the stairs-block when sitting down.
# sneaking: If set to true, you have to sneak to sit down on a chair. # sneaking: If set to true, you have to sneak to sit down on a chair.
# max-chair-width: Define how many blocks a chair can be long (set to number <= 0 for unlimited width). # max-chair-width: Define how many blocks a chair can be long (set to number <= 0 for unlimited width).
# sign-check: If set to true, you will only be able to sit down when there are signs on both of the ends of the chair. # sign-check: If set to true, you will only be able to sit down when there are signs on both of the ends of the chair.
# distance: The maximum distance between the chair (the center of the block) and the player to be able to sit down (to prevent glitching through walls, etc.). # distance: The maximum distance between the chair (the center of the block) and the player to be able to sit down (to prevent glitching through walls, etc.).
# sitting-height: Set how high you are sitting 'in' the stairs block (default is 0.7). # sitting-height: Set how high you are sitting 'in' the stairs block (default is 0.7).
# upsidedown-check: If true then prevent players from sitting on upside down stairs.
# ------ # ------
allowed-blocks: allowed-blocks:
- WOOD_STAIRS - WOOD_STAIRS
@ -21,7 +21,6 @@ allowed-blocks:
- BRICK_STAIRS - BRICK_STAIRS
- SMOOTH_STAIRS - SMOOTH_STAIRS
- NETHER_BRICK_STAIRS - NETHER_BRICK_STAIRS
item: LEVER
auto-rotate: true auto-rotate: true
sneaking: true sneaking: true
max-chair-width: 3 max-chair-width: 3
@ -30,3 +29,4 @@ distance: 2
sitting-height: 0.7 sitting-height: 0.7
permissions: true permissions: true
notify-player: true notify-player: true
upside-down-check: true

View File

@ -4,18 +4,21 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import net.minecraft.server.Packet40EntityMetadata;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class Chairs extends JavaPlugin { public class Chairs extends JavaPlugin {
public List<Material> allowedBlocks = new ArrayList<Material>(); public List<Material> allowedBlocks = new ArrayList<Material>();
public Material item; public boolean sneaking, autorotate, signcheck, permissions, notifyplayer, upsidedowncheck;
public boolean sneaking, autorotate, signcheck, permissions, notifyplayer;
public double sittingheight, distance; public double sittingheight, distance;
public int maxchairwidth; public int maxchairwidth;
private File pluginFolder; private File pluginFolder;
@ -58,7 +61,6 @@ public class Chairs extends JavaPlugin {
} }
private void loadConfig() { private void loadConfig() {
item = Material.getMaterial(getConfig().getString("item"));
autorotate = getConfig().getBoolean("auto-rotate"); autorotate = getConfig().getBoolean("auto-rotate");
sneaking = getConfig().getBoolean("sneaking"); sneaking = getConfig().getBoolean("sneaking");
signcheck = getConfig().getBoolean("sign-check"); signcheck = getConfig().getBoolean("sign-check");
@ -67,6 +69,7 @@ public class Chairs extends JavaPlugin {
maxchairwidth = getConfig().getInt("max-chair-width"); maxchairwidth = getConfig().getInt("max-chair-width");
permissions = getConfig().getBoolean("permissions"); permissions = getConfig().getBoolean("permissions");
notifyplayer = getConfig().getBoolean("notify-player"); notifyplayer = getConfig().getBoolean("notify-player");
upsidedowncheck = getConfig().getBoolean("upside-down-check");
for (String type : getConfig().getStringList("allowed-blocks")) { for (String type : getConfig().getStringList("allowed-blocks")) {
allowedBlocks.add(Material.getMaterial(type)); allowedBlocks.add(Material.getMaterial(type));
@ -91,4 +94,26 @@ public class Chairs extends JavaPlugin {
return true; return true;
} }
// Send sit packet to all online players
public void sendSit(Player p) {
Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
}
// Send stand packet to all online players
public void sendStand(Player p) {
if (sit.containsKey(p.getName())) {
if (notifyplayer) {
p.sendMessage(ChatColor.GRAY + "You are no longer sitting.");
}
sit.remove(p.getName());
}
Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
}
} }

View File

@ -1,29 +1,18 @@
package net.spoothie.chairs; package net.spoothie.chairs;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.Packet40EntityMetadata;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result; import org.bukkit.event.Event.Result;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Stairs; import org.bukkit.material.Stairs;
import org.bukkit.util.Vector;
public class EventListener implements Listener { public class EventListener implements Listener {
@ -38,20 +27,16 @@ public class EventListener implements Listener {
Player player = event.getPlayer(); Player player = event.getPlayer();
String pname = player.getName(); String pname = player.getName();
if (plugin.sit.containsKey(player.getName())) { if (plugin.sit.containsKey(player.getName())) {
if (player.getLocation().distance(plugin.sit.get(pname)) > 1) { Location from = player.getLocation();
if (plugin.notifyplayer) { Location to = plugin.sit.get(pname);
player.sendMessage(ChatColor.GRAY + "You are no longer sitting."); if (from.getWorld() == to.getWorld()) {
} if (from.distance(to) > 1) {
plugin.sit.remove(player.getName()); plugin.sendStand(player);
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false); } else {
for (Player play : Bukkit.getOnlinePlayers()) { plugin.sendSit(player);
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
} }
} else { } else {
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false); plugin.sendStand(player);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
} }
} }
} }
@ -60,6 +45,10 @@ public class EventListener implements Listener {
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
Stairs stairs = null;
if (block.getState().getData() instanceof Stairs) {
stairs = (Stairs) block.getState().getData();
}
if (plugin.allowedBlocks.contains(block.getType())) { if (plugin.allowedBlocks.contains(block.getType())) {
Player player = event.getPlayer(); Player player = event.getPlayer();
int chairwidth = 1; int chairwidth = 1;
@ -93,10 +82,7 @@ public class EventListener implements Listener {
if (plugin.notifyplayer) { if (plugin.notifyplayer) {
player.sendMessage(ChatColor.GRAY + "You are no longer sitting."); player.sendMessage(ChatColor.GRAY + "You are no longer sitting.");
} }
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false); plugin.sendStand(player);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
return; return;
} }
@ -105,9 +91,14 @@ public class EventListener implements Listener {
return; return;
} }
if (stairs != null) {
if (stairs.isInverted() && plugin.upsidedowncheck) {
return;
}
}
// Check for signs. // Check for signs.
if (plugin.signcheck == true && block instanceof Stairs) { if (plugin.signcheck == true && stairs != null) {
Stairs stairs = (Stairs) block.getState().getData();
boolean sign1 = false; boolean sign1 = false;
boolean sign2 = false; boolean sign2 = false;
@ -125,8 +116,7 @@ public class EventListener implements Listener {
} }
// Check for maximal chair width. // Check for maximal chair width.
if (plugin.maxchairwidth > 0 && block instanceof Stairs) { if (plugin.maxchairwidth > 0 && stairs != null) {
Stairs stairs = (Stairs) block.getState().getData();
if (stairs.getDescendingDirection() == BlockFace.NORTH || stairs.getDescendingDirection() == BlockFace.SOUTH) { if (stairs.getDescendingDirection() == BlockFace.NORTH || stairs.getDescendingDirection() == BlockFace.SOUTH) {
chairwidth += getChairWidth(block, BlockFace.EAST); chairwidth += getChairWidth(block, BlockFace.EAST);
chairwidth += getChairWidth(block, BlockFace.WEST); chairwidth += getChairWidth(block, BlockFace.WEST);
@ -141,21 +131,17 @@ public class EventListener implements Listener {
} }
// Sit-down process. // Sit-down process.
if (plugin.sneaking == false || (plugin.sneaking == true && event.getPlayer().isSneaking())) { if (!plugin.sneaking || (plugin.sneaking && event.getPlayer().isSneaking())) {
if (player.getVehicle() != null) { if (player.getVehicle() != null) {
player.getVehicle().remove(); player.getVehicle().remove();
} }
plugin.sendSit(player);
// Rotate the player's view to the descending side of the block. // Rotate the player's view to the descending side of the block.
if (plugin.autorotate && stairs != null) {
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false); Location plocation = block.getLocation().clone();
for (Player play : Bukkit.getOnlinePlayers()) { plocation.add(0.5D, (plugin.sittingheight - 0.5), 0.5D);
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
if (plugin.autorotate == true && block instanceof Stairs) {
Location plocation = block.getLocation();
Stairs stairs = (Stairs) block.getState().getData();
switch (stairs.getDescendingDirection()) { switch (stairs.getDescendingDirection()) {
case NORTH: case NORTH:
plocation.setYaw(90); plocation.setYaw(90);
@ -187,41 +173,6 @@ public class EventListener implements Listener {
} }
} }
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (plugin.allowedBlocks.contains(event.getBlock().getType())) {
Item drop = dropSeat(event.getBlock());
for (Entity e : drop.getNearbyEntities(0.2, 0.2, 0.2)) {
if (e != null && e instanceof Item && e.getPassenger() != null) {
e.remove();
}
}
drop.remove();
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Entity vehicle = event.getPlayer().getVehicle();
// Let players stand up when leaving the server.
if (vehicle != null && vehicle instanceof Item) {
vehicle.remove();
}
}
private Item dropSeat(Block chair) {
Location location = chair.getLocation().add(0.5, (plugin.sittingheight - 0.5), 0.5);
Item drop = location.getWorld().dropItem(location, new ItemStack(plugin.item));
drop.setPickupDelay(Integer.MAX_VALUE);
drop.teleport(location);
drop.setVelocity(new Vector(0, 0, 0));
return drop;
}
private int getChairWidth(Block block, BlockFace face) { private int getChairWidth(Block block, BlockFace face) {
int width = 0; int width = 0;
@ -243,7 +194,7 @@ public class EventListener implements Listener {
// Go through the blocks next to the clicked block and check if are signs on the end. // Go through the blocks next to the clicked block and check if are signs on the end.
for (int i = 1; true; i++) { for (int i = 1; true; i++) {
Block relative = block.getRelative(face, i); Block relative = block.getRelative(face, i);
if (!plugin.allowedBlocks.contains(relative.getType()) || (block instanceof Stairs && ((Stairs) relative.getState().getData()).getDescendingDirection() != ((Stairs) block.getState().getData()).getDescendingDirection())) { if (!plugin.allowedBlocks.contains(relative.getType()) || (block.getState().getData() instanceof Stairs && ((Stairs) relative.getState().getData()).getDescendingDirection() != ((Stairs) block.getState().getData()).getDescendingDirection())) {
if (relative.getType() == Material.SIGN || relative.getType() == Material.WALL_SIGN || relative.getType() == Material.SIGN_POST) { if (relative.getType() == Material.SIGN || relative.getType() == Material.WALL_SIGN || relative.getType() == Material.SIGN_POST) {
return true; return true;
} else { } else {

View File

@ -1,5 +1,5 @@
name: Chairs name: Chairs
version: 1.7 version: 1.8
description: Let players sit on stairs and slabs. description: Let players sit on stairs and slabs.
authors: authors:
- spoothie - spoothie