From aef19e5d2b3b9f91bb85db0de28053681a8fc7dd Mon Sep 17 00:00:00 2001 From: cnaude Date: Sat, 24 Nov 2012 16:19:48 -0700 Subject: [PATCH] Remove players from sit list after they log off. Add chair break event check. --- src/config.yml | 4 +- src/net/spoothie/chairs/Chairs.java | 50 +++++++--- src/net/spoothie/chairs/EventListener.java | 104 ++++++++++++++++----- src/plugin.yml | 2 +- 4 files changed, 123 insertions(+), 37 deletions(-) diff --git a/src/config.yml b/src/config.yml index 142f160..f11783f 100644 --- a/src/config.yml +++ b/src/config.yml @@ -10,6 +10,7 @@ # 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. +# seat-occupied-check: Check if seat is already occupied. # ------ allowed-blocks: - WOOD_STAIRS @@ -29,4 +30,5 @@ distance: 2 sitting-height: 0.7 permissions: true notify-player: true -upside-down-check: true \ No newline at end of file +upside-down-check: true +seat-occupied-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 7675968..f0cd32e 100644 --- a/src/net/spoothie/chairs/Chairs.java +++ b/src/net/spoothie/chairs/Chairs.java @@ -4,6 +4,8 @@ import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import net.minecraft.server.Packet40EntityMetadata; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -18,13 +20,16 @@ import org.bukkit.plugin.java.JavaPlugin; public class Chairs extends JavaPlugin { public List allowedBlocks = new ArrayList(); - public boolean sneaking, autorotate, signcheck, permissions, notifyplayer, upsidedowncheck; - public double sittingheight, distance; - public int maxchairwidth; + public boolean sneaking, autoRotate, signCheck, permissions, notifyplayer, invertedStairCheck, seatOccupiedCheck; + public double sittingHeight, distance; + public int maxChairWidth; private File pluginFolder; private File configFile; public byte metadata; public HashMap sit = new HashMap(); + public static final String PLUGIN_NAME = "Chairs"; + public static final String LOG_HEADER = "[" + PLUGIN_NAME + "]"; + static final Logger log = Logger.getLogger("Minecraft"); @Override public void onEnable() { @@ -47,7 +52,7 @@ public class Chairs extends JavaPlugin { try { pluginFolder.mkdir(); } catch (Exception e) { - e.printStackTrace(); + logInfo("ERROR: " + e.getMessage()); } } @@ -55,24 +60,30 @@ public class Chairs extends JavaPlugin { try { configFile.createNewFile(); } catch (Exception e) { - e.printStackTrace(); + logInfo("ERROR: " + e.getMessage()); } } } private void loadConfig() { - autorotate = getConfig().getBoolean("auto-rotate"); + autoRotate = getConfig().getBoolean("auto-rotate"); sneaking = getConfig().getBoolean("sneaking"); - signcheck = getConfig().getBoolean("sign-check"); - sittingheight = getConfig().getDouble("sitting-height"); + signCheck = getConfig().getBoolean("sign-check"); + sittingHeight = getConfig().getDouble("sitting-height"); distance = getConfig().getDouble("distance"); - maxchairwidth = getConfig().getInt("max-chair-width"); + maxChairWidth = getConfig().getInt("max-chair-width"); permissions = getConfig().getBoolean("permissions"); notifyplayer = getConfig().getBoolean("notify-player"); - upsidedowncheck = getConfig().getBoolean("upside-down-check"); + invertedStairCheck = getConfig().getBoolean("upside-down-check"); + seatOccupiedCheck = getConfig().getBoolean("seat-occupied-check"); for (String type : getConfig().getStringList("allowed-blocks")) { - allowedBlocks.add(Material.getMaterial(type)); + try { + allowedBlocks.add(Material.getMaterial(type)); + } + catch (Exception e) { + logInfo("ERROR: " + e.getMessage()); + } } } @@ -103,6 +114,15 @@ public class Chairs extends JavaPlugin { } } + public void sendSit() { + for (String s : sit.keySet()) { + Player p = Bukkit.getPlayer(s); + if (p != null) { + sendSit(p); + } + } + } + // Send stand packet to all online players public void sendStand(Player p) { if (sit.containsKey(p.getName())) { @@ -116,4 +136,12 @@ public class Chairs extends JavaPlugin { ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet); } } + + public void logInfo(String _message) { + log.log(Level.INFO, String.format("%s %s", LOG_HEADER, _message)); + } + + public void logError(String _message) { + log.log(Level.SEVERE, String.format("%s %s", LOG_HEADER, _message)); + } } diff --git a/src/net/spoothie/chairs/EventListener.java b/src/net/spoothie/chairs/EventListener.java index e0c3975..4608497 100644 --- a/src/net/spoothie/chairs/EventListener.java +++ b/src/net/spoothie/chairs/EventListener.java @@ -1,5 +1,8 @@ package net.spoothie.chairs; +import java.util.Timer; +import java.util.TimerTask; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -11,7 +14,10 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.material.Stairs; public class EventListener implements Listener { @@ -23,28 +29,65 @@ public class EventListener implements Listener { } @EventHandler - public void onPlayerMove (PlayerMoveEvent event) { - Player player = event.getPlayer(); + public void onPlayerMove(PlayerMoveEvent event) { + Player player = event.getPlayer(); String pname = player.getName(); 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); + if (from.distance(to) > 1) { + plugin.sendStand(player); } else { - plugin.sendSit(player); + plugin.sendSit(player); } } else { - plugin.sendStand(player); + plugin.sendStand(player); } } } + + class sendSitTask extends TimerTask { + + @Override + public void run() { + plugin.sendSit(); + } + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + Timer timer = new Timer(); + long delay = 1 * 2000; + timer.schedule(new sendSitTask(), delay); + //plugin.sendSit(); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + String pName = event.getPlayer().getName(); + if (plugin.sit.containsKey(pName)) { + plugin.sit.remove(pName); + } + } + @EventHandler + public void onBlockDestroy(BlockBreakEvent event) { + Block block = event.getBlock(); + if (!plugin.sit.isEmpty()) { + for (String s : plugin.sit.keySet()) { + if (plugin.sit.get(s).equals(block.getLocation())) { + Player player = Bukkit.getPlayer(s); + plugin.sendStand(player); + } + } + } + } + @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(); @@ -75,30 +118,30 @@ public class EventListener implements Listener { } // Check if player is sitting. - + if (plugin.sit.containsKey(event.getPlayer().getName())) { plugin.sit.remove(player.getName()); event.setCancelled(true); if (plugin.notifyplayer) { player.sendMessage(ChatColor.GRAY + "You are no longer sitting."); } - plugin.sendStand(player); - return; + plugin.sendStand(player); + return; } // Check for distance distance between player and chair. 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) { + + if (stairs != null) { + if (stairs.isInverted() && plugin.invertedStairCheck) { return; - } + } } // Check for signs. - if (plugin.signcheck == true && stairs != null) { + if (plugin.signCheck == true && stairs != null) { boolean sign1 = false; boolean sign2 = false; @@ -116,7 +159,7 @@ public class EventListener implements Listener { } // Check for maximal chair width. - if (plugin.maxchairwidth > 0 && stairs != null) { + 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); @@ -125,23 +168,32 @@ public class EventListener implements Listener { chairwidth += getChairWidth(block, BlockFace.SOUTH); } - if (chairwidth > plugin.maxchairwidth) { + if (chairwidth > plugin.maxChairWidth) { return; } } // Sit-down process. if (!plugin.sneaking || (plugin.sneaking && event.getPlayer().isSneaking())) { + if (plugin.seatOccupiedCheck) { + if (!plugin.sit.isEmpty()) { + for (String s : plugin.sit.keySet()) { + if (plugin.sit.get(s).equals(block.getLocation())) { + player.sendMessage(ChatColor.GRAY + "This seat is occupied by " + s + "!"); + return; + } + } + } + } + if (player.getVehicle() != null) { player.getVehicle().remove(); } - - plugin.sendSit(player); - + // Rotate the player's view to the descending side of the block. - if (plugin.autorotate && stairs != null) { + if (plugin.autoRotate && stairs != null) { Location plocation = block.getLocation().clone(); - plocation.add(0.5D, (plugin.sittingheight - 0.5), 0.5D); + plocation.add(0.5D, (plugin.sittingHeight - 0.5), 0.5D); switch (stairs.getDescendingDirection()) { case NORTH: plocation.setYaw(90); @@ -166,8 +218,12 @@ public class EventListener implements Listener { player.sendMessage(ChatColor.GRAY + "You are now sitting."); } plugin.sit.put(player.getName(), block.getLocation()); - event.setUseInteractedBlock(Result.DENY); + + Timer timer = new Timer(); + long delay = 1 * 2000; + timer.schedule(new sendSitTask(), delay); + //plugin.sendSit(player); } } } @@ -177,7 +233,7 @@ public class EventListener implements Listener { int width = 0; // Go through the blocks next to the clicked block and check if there are any further stairs. - for (int i = 1; i <= plugin.maxchairwidth; i++) { + for (int i = 1; i <= plugin.maxChairWidth; i++) { Block relative = block.getRelative(face, i); if (plugin.allowedBlocks.contains(relative.getType()) && ((Stairs) relative.getState().getData()).getDescendingDirection() == ((Stairs) block.getState().getData()).getDescendingDirection()) { diff --git a/src/plugin.yml b/src/plugin.yml index e2a2a99..d224fc6 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: Chairs -version: 1.8 +version: 1.10.1 description: Let players sit on stairs and slabs. authors: - spoothie