diff --git a/src/com/cnaude/chairs/Chairs.java b/src/com/cnaude/chairs/Chairs.java index 4c69491..cc01a30 100644 --- a/src/com/cnaude/chairs/Chairs.java +++ b/src/com/cnaude/chairs/Chairs.java @@ -11,6 +11,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; @@ -39,6 +40,8 @@ public class Chairs extends JavaPlugin { private File pluginFolder; private File configFile; public HashMap sit = new HashMap(); + public HashMap sitblock = new HashMap(); + public HashMap sitblockbr = new HashMap(); public static final String PLUGIN_NAME = "Chairs"; public static final String LOG_HEADER = "[" + PLUGIN_NAME + "]"; static final Logger log = Bukkit.getLogger(); diff --git a/src/com/cnaude/chairs/EventListener.java b/src/com/cnaude/chairs/EventListener.java index d2568c4..12abfce 100644 --- a/src/com/cnaude/chairs/EventListener.java +++ b/src/com/cnaude/chairs/EventListener.java @@ -1,15 +1,19 @@ package com.cnaude.chairs; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.craftbukkit.v1_6_R2.entity.CraftArrow; import org.bukkit.entity.Entity; import org.bukkit.entity.ItemFrame; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; 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.material.Stairs; import org.bukkit.material.Step; @@ -51,11 +55,32 @@ public class EventListener implements Listener { private void unSit(Player player) { plugin.sit.get(player.getName()).remove(); + plugin.sitblock.remove(plugin.sitblockbr.get(player.getName())); + plugin.sitblockbr.remove(player.getName()); plugin.sit.remove(player.getName()); if (plugin.notifyplayer && !plugin.msgStanding.isEmpty()) { player.sendMessage(plugin.msgStanding); } } + + @EventHandler(priority=EventPriority.MONITOR,ignoreCancelled=true) + public void onBlockBreak(BlockBreakEvent event) + { + Block b = event.getBlock(); + if (plugin.sitblock.containsKey(b)) + { + String playername = plugin.sitblock.get(b); + Player player = Bukkit.getPlayerExact(playername); + final Entity arrow = plugin.sit.get(playername); + net.minecraft.server.v1_6_R2.EntityArrow nmsarrow = ((CraftArrow) arrow).getHandle(); + nmsarrow.motX = 0; + nmsarrow.motY = 0; + nmsarrow.motZ = 0; + nmsarrow.boundingBox.b = -1; + player.eject(); + unSit(player); + } + } @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { @@ -209,14 +234,13 @@ public class EventListener implements Listener { // Sit-down process. if (plugin.seatOccupiedCheck) { if (!plugin.sit.isEmpty()) { - for (String s : plugin.sit.keySet()) { - if (plugin.sit.get(s).equals(block.getLocation())) { - if (!plugin.msgOccupied.isEmpty()) { - player.sendMessage(plugin.msgOccupied.replaceAll("%PLAYER%", s)); - } - return; - } - } + if (plugin.sitblock.containsKey(block)) + { + if (!plugin.msgOccupied.isEmpty()) { + player.sendMessage(plugin.msgOccupied.replaceAll("%PLAYER%", plugin.sitblock.get(block))); + } + return; + } } } @@ -249,12 +273,14 @@ public class EventListener implements Listener { player.sendMessage(plugin.msgSitting); } - player.getPlayer().teleport(plocation); + player.teleport(plocation); Entity arrow = block.getWorld().spawnArrow(getBlockCentre(block).subtract(0, 0.5, 0), new Vector(0, 0, 0), 0, 0); arrow.setPassenger(player); player.setSneaking(false); arrow.setTicksLived(1); plugin.sit.put(player.getName(), arrow); + plugin.sitblock.put(block, player.getName()); + plugin.sitblockbr.put(player.getName(), block); event.setCancelled(true); } } diff --git a/src/com/cnaude/chairs/PacketListener.java b/src/com/cnaude/chairs/PacketListener.java index 2182053..bda5416 100644 --- a/src/com/cnaude/chairs/PacketListener.java +++ b/src/com/cnaude/chairs/PacketListener.java @@ -1,6 +1,8 @@ package com.cnaude.chairs; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Block; import org.bukkit.craftbukkit.v1_6_R2.entity.CraftArrow; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -41,6 +43,7 @@ public class PacketListener { final Player player = e.getPlayer(); if (e.getPacket().getBooleans().getValues().get(1)) { + //hacks to avoid nope error final Entity arrow = pluginInstance.sit.get(player.getName()); if (arrow != null) { @@ -50,6 +53,9 @@ public class PacketListener { nmsarrow.motZ = 0; nmsarrow.boundingBox.b = -1; } + //teleport player to correct location + + //unsit player Bukkit.getScheduler().scheduleSyncDelayedTask(pluginInstance, new Runnable() { public void run() @@ -69,11 +75,24 @@ public class PacketListener { if (pluginInstance.sit.containsKey(player.getName())) { pluginInstance.sit.get(player.getName()).remove(); + pluginInstance.sitblock.remove(pluginInstance.sitblockbr.get(player.getName())); + pluginInstance.sitblockbr.remove(player.getName()); pluginInstance.sit.remove(player.getName()); if (pluginInstance.notifyplayer && !pluginInstance.msgStanding.isEmpty()) { player.sendMessage(pluginInstance.msgStanding); } } } + + + private Location getTeleportLoc(Player player) + { + Block sittingon = pluginInstance.sitblockbr.get(player.getName()); + sittingon.getLocation(); + player.getLocation().getYaw(); + Location to = player.getLineOfSight(null, 5).get(0).getLocation(); + return to; + } + } diff --git a/target/Chairs.jar b/target/Chairs.jar index b9d439d..92b14c8 100644 Binary files a/target/Chairs.jar and b/target/Chairs.jar differ