@Override public void placeBlock( GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) { super.placeBlock(player, state, face, holding, clickedLoc); MaterialData data = state.getData(); if (data instanceof Ladder) { if (face != BlockFace.DOWN && face != BlockFace.UP && isTargetOccluding(state, face.getOppositeFace())) { ((Ladder) data).setFacingDirection(face.getOppositeFace()); } else { if (isTargetOccluding(state, BlockFace.SOUTH)) { ((Ladder) data).setFacingDirection(BlockFace.SOUTH); } else if (isTargetOccluding(state, BlockFace.WEST)) { ((Ladder) data).setFacingDirection(BlockFace.WEST); } else if (isTargetOccluding(state, BlockFace.NORTH)) { ((Ladder) data).setFacingDirection(BlockFace.NORTH); } else if (isTargetOccluding(state, BlockFace.EAST)) { ((Ladder) data).setFacingDirection(BlockFace.EAST); } else { return; } } state.setData(data); } else { warnMaterialData(Ladder.class, data); } }
@EventHandler(priority = EventPriority.HIGHEST) public void onPlayerInteract(PlayerInteractEvent event) { if (event.getAction() == Action.PHYSICAL) { return; } int mode = 0; Player player = event.getPlayer(); Stick stick = null; stick = plugin.getStick(player); mode = stick.getMode(); if (event.getAction() == Action.LEFT_CLICK_BLOCK) { if (player.getGameMode() == GameMode.CREATIVE && stick.isEnabled()) { event.setCancelled(true); } return; } if (event.getAction() == Action.RIGHT_CLICK_BLOCK && stick.isThrowBuild() && player.getItemInHand().getType() != stick.getTool()) { return; } if (!stick.doRightClickModes() && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) { mode = Stick.REMOVE_MODE; } if (plugin.canUse(player, stick, mode)) { event.setCancelled(true); List<Block> targetBlocks = player.getLastTwoTargetBlocks(stick.getIgnore(), stick.getDistance()); // sanity check if (targetBlocks.size() != 2) { plugin.log(Level.WARNING, "Did not get two blocks to work with"); return; } Block targetedBlock = null; Block placedAgainstBlock = null; Item item = stick.getItem(); if (!item.isBlock()) { item = MaterialUtils.getPlaceableMaterial(item); if (!item.isBlock()) { if (!stick.isThrowBuild()) { MessageUtils.send(player, ChatColor.RED, "Invalid item usage."); } else { event.setCancelled(false); } return; } } BlockEvent actionEvent = null; if (mode == Stick.REPLACE_MODE || mode == Stick.REMOVE_MODE) { targetedBlock = targetBlocks.get(1); placedAgainstBlock = targetBlocks.get(0); } else if (mode == Stick.BUILD_MODE) { targetedBlock = targetBlocks.get(0); placedAgainstBlock = targetBlocks.get(1); } if (targetedBlock.getLocation().getBlockY() == 0 && stick.doProtectBottom()) { plugin.log(Level.WARNING, "Player " + player.getDisplayName() + " hit rock bottom!"); return; } if (LocationUtil.isSameLocation(player, targetedBlock)) { if (stick.isDebug()) { MessageUtils.send(player, "** boink **"); } return; } BlockState after = targetedBlock.getState(); after.setType(mode == Stick.REMOVE_MODE ? Material.AIR : item.getMaterial()); MaterialData data = null; if (mode == Stick.REMOVE_MODE) { data = Material.AIR.getNewData((byte) 0); } else if (MaterialUtils.isSameMaterial(item.getMaterial(), Material.LADDER)) { BlockFace face = LocationUtil.getFace(player, targetedBlock); if (stick.isDebug()) { MessageUtils.send(player, "clicked " + face + " face! (" + player.getEyeLocation() + ")"); } Ladder ladder = new Ladder(); ladder.setFacingDirection(face); data = ladder; } else { data = item.getData(); } if (data != null) after.setData(data); BlockState before = targetedBlock.getState(); if (mode == Stick.REMOVE_MODE) { stick.setDoItemSwitch(true); actionEvent = new BlockBreakEvent(targetedBlock, player); } else if (MaterialUtils.isSameMaterial(item.getMaterial(), Material.FIRE)) { actionEvent = new BlockIgniteEvent(targetedBlock, IgniteCause.FLINT_AND_STEEL, player); } else { actionEvent = new BlockPlaceEvent( after.getBlock(), before, placedAgainstBlock, new ItemStack(stick.getTool()), player, true); } plugin.getServer().getPluginManager().callEvent(actionEvent); if (!((Cancellable) actionEvent).isCancelled()) { plugin.takeAction(before, after, player); } } }