From edf2434b300553bb17144182572158daa5e8aa34 Mon Sep 17 00:00:00 2001 From: cnaude Date: Fri, 23 Nov 2012 01:39:10 -0700 Subject: [PATCH] Properly check for stair type. Add upside down chair sit prevention --- src/config.yml | 6 +- src/net/spoothie/chairs/Chairs.java | 35 ++++++- src/net/spoothie/chairs/EventListener.java | 111 ++++++--------------- src/plugin.yml | 2 +- 4 files changed, 65 insertions(+), 89 deletions(-) diff --git a/src/config.yml b/src/config.yml index 8e20167..142f160 100644 --- a/src/config.yml +++ b/src/config.yml @@ -3,13 +3,13 @@ # 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. -# 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. # 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). # 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.). # 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: - WOOD_STAIRS @@ -21,7 +21,6 @@ allowed-blocks: - BRICK_STAIRS - SMOOTH_STAIRS - NETHER_BRICK_STAIRS -item: LEVER auto-rotate: true sneaking: true max-chair-width: 3 @@ -29,4 +28,5 @@ sign-check: false distance: 2 sitting-height: 0.7 permissions: true -notify-player: true \ No newline at end of file +notify-player: true +upside-down-check: true \ No newline at end of file diff --git a/src/net/spoothie/chairs/Chairs.java b/src/net/spoothie/chairs/Chairs.java index 3b65570..7675968 100644 --- a/src/net/spoothie/chairs/Chairs.java +++ b/src/net/spoothie/chairs/Chairs.java @@ -4,18 +4,21 @@ import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import net.minecraft.server.Packet40EntityMetadata; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; public class Chairs extends JavaPlugin { - public List allowedBlocks = new ArrayList(); - public Material item; - public boolean sneaking, autorotate, signcheck, permissions, notifyplayer; + public List allowedBlocks = new ArrayList(); + public boolean sneaking, autorotate, signcheck, permissions, notifyplayer, upsidedowncheck; public double sittingheight, distance; public int maxchairwidth; private File pluginFolder; @@ -57,8 +60,7 @@ public class Chairs extends JavaPlugin { } } - private void loadConfig() { - item = Material.getMaterial(getConfig().getString("item")); + private void loadConfig() { autorotate = getConfig().getBoolean("auto-rotate"); sneaking = getConfig().getBoolean("sneaking"); signcheck = getConfig().getBoolean("sign-check"); @@ -67,6 +69,7 @@ public class Chairs extends JavaPlugin { maxchairwidth = getConfig().getInt("max-chair-width"); permissions = getConfig().getBoolean("permissions"); notifyplayer = getConfig().getBoolean("notify-player"); + upsidedowncheck = getConfig().getBoolean("upside-down-check"); for (String type : getConfig().getStringList("allowed-blocks")) { allowedBlocks.add(Material.getMaterial(type)); @@ -91,4 +94,26 @@ public class Chairs extends JavaPlugin { 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); + } + } } diff --git a/src/net/spoothie/chairs/EventListener.java b/src/net/spoothie/chairs/EventListener.java index 769a892..e0c3975 100644 --- a/src/net/spoothie/chairs/EventListener.java +++ b/src/net/spoothie/chairs/EventListener.java @@ -1,29 +1,18 @@ 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.Location; import org.bukkit.Material; import org.bukkit.block.Block; 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.event.Event.Result; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.player.PlayerInteractEvent; 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.util.Vector; public class EventListener implements Listener { @@ -37,21 +26,17 @@ public class EventListener implements Listener { public void onPlayerMove (PlayerMoveEvent event) { Player player = event.getPlayer(); String pname = player.getName(); - if (plugin.sit.containsKey(player.getName())) { - if (player.getLocation().distance(plugin.sit.get(pname)) > 1) { - if (plugin.notifyplayer) { - player.sendMessage(ChatColor.GRAY + "You are no longer sitting."); - } - plugin.sit.remove(player.getName()); - Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false); - for (Player play : Bukkit.getOnlinePlayers()) { - ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet); + if (plugin.sit.containsKey(player.getName())) { + Location from = player.getLocation(); + Location to = plugin.sit.get(pname); + if (from.getWorld() == to.getWorld()) { + if (from.distance(to) > 1) { + plugin.sendStand(player); + } else { + plugin.sendSit(player); } } else { - Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false); - for (Player play : Bukkit.getOnlinePlayers()) { - ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet); - } + plugin.sendStand(player); } } } @@ -59,7 +44,11 @@ public class EventListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { 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())) { Player player = event.getPlayer(); int chairwidth = 1; @@ -93,10 +82,7 @@ public class EventListener implements Listener { if (plugin.notifyplayer) { player.sendMessage(ChatColor.GRAY + "You are no longer sitting."); } - Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false); - for (Player play : Bukkit.getOnlinePlayers()) { - ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet); - } + plugin.sendStand(player); return; } @@ -104,10 +90,15 @@ public class EventListener implements Listener { if (plugin.distance > 0 && player.getLocation().distance(block.getLocation().add(0.5, 0, 0.5)) > plugin.distance) { return; } + + if (stairs != null) { + if (stairs.isInverted() && plugin.upsidedowncheck) { + return; + } + } // Check for signs. - if (plugin.signcheck == true && block instanceof Stairs) { - Stairs stairs = (Stairs) block.getState().getData(); + if (plugin.signcheck == true && stairs != null) { boolean sign1 = false; boolean sign2 = false; @@ -125,8 +116,7 @@ public class EventListener implements Listener { } // Check for maximal chair width. - if (plugin.maxchairwidth > 0 && block instanceof Stairs) { - Stairs stairs = (Stairs) block.getState().getData(); + if (plugin.maxchairwidth > 0 && stairs != null) { if (stairs.getDescendingDirection() == BlockFace.NORTH || stairs.getDescendingDirection() == BlockFace.SOUTH) { chairwidth += getChairWidth(block, BlockFace.EAST); chairwidth += getChairWidth(block, BlockFace.WEST); @@ -141,21 +131,17 @@ public class EventListener implements Listener { } // 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) { player.getVehicle().remove(); } - - // Rotate the player's view to the descending side of the block. - - Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false); - for (Player play : Bukkit.getOnlinePlayers()) { - ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet); - } - if (plugin.autorotate == true && block instanceof Stairs) { - Location plocation = block.getLocation(); - Stairs stairs = (Stairs) block.getState().getData(); + plugin.sendSit(player); + + // Rotate the player's view to the descending side of the block. + if (plugin.autorotate && stairs != null) { + Location plocation = block.getLocation().clone(); + plocation.add(0.5D, (plugin.sittingheight - 0.5), 0.5D); switch (stairs.getDescendingDirection()) { case NORTH: 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) { 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. for (int i = 1; true; 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) { return true; } else { diff --git a/src/plugin.yml b/src/plugin.yml index 44c0ab9..e2a2a99 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: Chairs -version: 1.7 +version: 1.8 description: Let players sit on stairs and slabs. authors: - spoothie