diff --git a/src/com/cnaude/chairs/EventListener.java b/src/com/cnaude/chairs/EventListener.java index 3115905..064dca9 100644 --- a/src/com/cnaude/chairs/EventListener.java +++ b/src/com/cnaude/chairs/EventListener.java @@ -32,28 +32,6 @@ public class EventListener implements Listener { this.plugin = plugin; this.ignoreList = ignoreList; } - - public boolean isValidChair(Block block) { - for (ChairBlock cb : plugin.allowedBlocks) { - if (cb.getMat().equals(block.getType())) { - return true; - } - } - return false; - } - - - private boolean isSitting(Player player) { - if (plugin.sit.containsKey(player.getName())) { - if (player.isInsideVehicle()) { - return true; - } else { - plugin.unSit(player); - } - } - return false; - } - @EventHandler(priority=EventPriority.LOWEST,ignoreCancelled=true) public void onJoin(PlayerJoinEvent e) @@ -96,6 +74,7 @@ public class EventListener implements Listener { plugin.ejectPlayer(player); } } + @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { @@ -108,30 +87,32 @@ public class EventListener implements Listener { && plugin.ignoreIfBlockInHand) { return; } - if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) { - sitPlayer(player, event.getClickedBlock()); - if (sit) { - event.setCancelled(true); - sit = false; + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + Block block = event.getClickedBlock(); + if (sitAllowed(player, block)) + { + Location sitLocation = getSitLocation(block, player.getLocation().getYaw()); + sitPlayer(player, block, sitLocation); + event.setCancelled(true); } } } - private boolean sit = false; - protected void sitPlayer(Player player, Block block) + + + private boolean sitAllowed(Player player, Block block) { Stairs stairs = null; Step step = null; WoodenStep wStep = null; - double sh = plugin.sittingHeight; boolean blockOkay = false; if (ignoreList.isIgnored(player.getName())) { - return; + return false; } // Permissions Check if (plugin.permissions) { if (!player.hasPermission("chairs.sit")) { - return; + return false; } } if (plugin.perItemPerms) { @@ -161,13 +142,11 @@ public class EventListener implements Listener { if (cb.getMat().toString().contains("STAIRS")) { if (cb.getMat().equals(block.getType())) { blockOkay = true; - sh = cb.getSitHeight(); continue; } } else if (cb.getMat().equals(block.getType()) && cb.getDamage() == block.getData()) { blockOkay = true; - sh = cb.getSitHeight(); continue; } } @@ -183,41 +162,39 @@ public class EventListener implements Listener { step = (Step) block.getState().getData(); } else if (block.getState().getData() instanceof WoodenStep) { wStep = (WoodenStep) block.getState().getData(); - } else { - sh += plugin.sittingHeightAdj; } int chairwidth = 1; // Check if block beneath chair is solid. if (block.getRelative(BlockFace.DOWN).isLiquid()) { - return; + return false; } if (block.getRelative(BlockFace.DOWN).isEmpty()) { - return; + return false; } if (!block.getRelative(BlockFace.DOWN).getType().isSolid()) { - return; + return false; } // 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; + return false; } if (stairs != null) { if (stairs.isInverted() && plugin.invertedStairCheck) { - return; + return false; } } if (step != null) { if (step.isInverted() && plugin.invertedStepCheck) { - return; + return false; } } if (wStep != null) { if (wStep.isInverted() && plugin.invertedStepCheck) { - return; + return false; } } @@ -235,7 +212,7 @@ public class EventListener implements Listener { } if (!(sign1 == true && sign2 == true)) { - return; + return false; } } @@ -250,7 +227,7 @@ public class EventListener implements Listener { } if (chairwidth > plugin.maxChairWidth) { - return; + return false; } } @@ -262,52 +239,80 @@ public class EventListener implements Listener { if (!plugin.msgOccupied.isEmpty()) { player.sendMessage(plugin.msgOccupied.replaceAll("%PLAYER%", plugin.sitblock.get(block))); } - return; + return false; } } } - - Location plocation = block.getLocation().clone(); - plocation.add(0.5D, (sh - 0.5D), 0.5D); - - // Rotate the player's view to the descending side of the block. - if (plugin.autoRotate && stairs != null) { - switch (stairs.getDescendingDirection()) { - case NORTH: - plocation.setYaw(180); - break; - case EAST: - plocation.setYaw(-90); - break; - case SOUTH: - plocation.setYaw(0); - break; - case WEST: - plocation.setYaw(90); - default: - ; - } - } else { - plocation.setYaw(player.getLocation().getYaw()); - } - - if (plugin.notifyplayer && !plugin.msgSitting.isEmpty()) { - player.sendMessage(plugin.msgSitting); - } - - plugin.sitstopteleportloc.put(player.getName(), player.getLocation()); - player.teleport(plocation); - Entity arrow = block.getWorld().spawnArrow(block.getLocation().add(0.5, 0 , 0.5), new Vector(0, 0, 0), 0, 0); - arrow.setPassenger(player); - plugin.sit.put(player.getName(), arrow); - plugin.sitblock.put(block, player.getName()); - plugin.sitblockbr.put(player.getName(), block); - startReSitTask(player); - sit = true; } + return true; } - protected void startReSitTask(final Player player) + private Location getSitLocation(Block block, Float playerYaw) + { + double sh = plugin.sittingHeight; + + for (ChairBlock cb : plugin.allowedBlocks) { + if (cb.getMat().toString().contains("STAIRS")) { + if (cb.getMat().equals(block.getType())) { + sh = cb.getSitHeight(); + continue; + } + } else if (cb.getMat().equals(block.getType()) + && cb.getDamage() == block.getData()) { + sh = cb.getSitHeight(); + continue; + } + } + + Stairs stairs = null; + if (block.getState().getData() instanceof Stairs) { + stairs = (Stairs) block.getState().getData(); + } else { + sh += plugin.sittingHeightAdj; + } + + Location plocation = block.getLocation().clone(); + plocation.add(0.5D, (sh - 0.5D), 0.5D); + + // Rotate the player's view to the descending side of the block. + if (plugin.autoRotate && stairs != null) { + switch (stairs.getDescendingDirection()) { + case NORTH: + plocation.setYaw(180); + break; + case EAST: + plocation.setYaw(-90); + break; + case SOUTH: + plocation.setYaw(0); + break; + case WEST: + plocation.setYaw(90); + default: + ; + } + } else { + plocation.setYaw(playerYaw); + } + return plocation; + } + + + private void sitPlayer(Player player, Block block, Location sitlocation) + { + if (plugin.notifyplayer && !plugin.msgSitting.isEmpty()) { + player.sendMessage(plugin.msgSitting); + } + plugin.sitstopteleportloc.put(player.getName(), player.getLocation()); + player.teleport(sitlocation); + Entity arrow = block.getWorld().spawnArrow(block.getLocation().add(0.5, 0 , 0.5), new Vector(0, 0, 0), 0, 0); + arrow.setPassenger(player); + plugin.sit.put(player.getName(), arrow); + plugin.sitblock.put(block, player.getName()); + plugin.sitblockbr.put(player.getName(), block); + startReSitTask(player); + } + private void startReSitTask(final Player player) { int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() @@ -319,6 +324,28 @@ public class EventListener implements Listener { },1000,1000); plugin.sittask.put(player.getName(), task); } + + + public boolean isValidChair(Block block) { + for (ChairBlock cb : plugin.allowedBlocks) { + if (cb.getMat().equals(block.getType())) { + return true; + } + } + return false; + } + + + private boolean isSitting(Player player) { + if (plugin.sit.containsKey(player.getName())) { + if (player.isInsideVehicle()) { + return true; + } else { + plugin.unSit(player); + } + } + return false; + } private int getChairWidth(Block block, BlockFace face) { int width = 0; diff --git a/target/Chairs.jar b/target/Chairs.jar index 62a8828..2fd2943 100644 Binary files a/target/Chairs.jar and b/target/Chairs.jar differ