/** * Detects whether the player right-clicked on a lever and starts machina * detection. */ @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void playerInteract(PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; Block block = event.getClickedBlock(); if (block.getType() != Material.LEVER) return; Lever lever = (Lever) block.getState().getData(); BlockFace attachedFace = lever.getAttachedFace(); if (attachedFace == null) { MachinaCore.log.warning("MachinaCore: Lever activated by " + event.getPlayer().getName() + " seems to be attached to nothing?"); return; } Block attachedTo = block.getRelative(attachedFace); plugin.onLever(event.getPlayer(), new BlockLocation(attachedTo), attachedFace.getOppositeFace(), event.getItem()); }
/** * Turns the lever attached to the anchor off and returns null * * @param anchor * The current location of the anchor * @return null */ private HeartBeatEvent stopPad(BlockLocation anchor) { BlockState leverBlockState; Block leverBlock; // Get the lever block leverBlock = anchor.getRelative(BlockFace.UP).getBlock(); // Make sure the lever is still there if (leverBlock.getType() == Material.LEVER) { // Get the block state leverBlockState = leverBlock.getState(); // Set the power state to false ((Lever) leverBlockState.getData()).setPowered(false); // Update the state leverBlockState.update(); } // Return null, stopping the pad if used with 'return stopPad( anchor )' // in the heartbeat event return null; }
@EventHandler(ignoreCancelled = true) public void onBlockPhysics(BlockPhysicsEvent event) { Block block = event.getBlock(); BaseSTBBlock item = LocationManager.getManager().get(block.getLocation()); if (item != null) { item.handlePhysicsEvent(event); } else { if (block.getType() == Material.LEVER) { Lever l = (Lever) block.getState().getData(); item = LocationManager.getManager().get(block.getRelative(l.getAttachedFace()).getLocation()); if (item != null) { event.setCancelled(true); } } } }
@Override public void run() { info.task = -1; // Make sure the block is still a lever. if (block.getType() != Material.LEVER) return; final BlockState bs = block.getState(); final Lever lever = (Lever) bs.getData(); if (lever.isPowered() == powered) return; lever.setPowered(powered); bs.setData(lever); bs.update(); }
/** * checks if this block would give a reaction if you click on it without * shifting, e.g. opening a chest or switching a lever */ public static boolean isInteractiveBlock(Block b) { if (b == null || b.getState() == null) { return false; } if (b.getType() == Material.WORKBENCH || b.getType() == Material.ENCHANTMENT_TABLE || b.getType() == Material.ANVIL || b.getType() == Material.BREWING_STAND || b.getState() instanceof InventoryHolder || b.getState() instanceof NoteBlock) { return true; } if (b.getState().getData() instanceof Button || b.getState().getData() instanceof Lever || b.getState().getData() instanceof Door || b.getState().getData() instanceof TrapDoor || b.getState().getData() instanceof Gate || b.getState().getData() instanceof Comparator) { if (b.getType() != Material.IRON_DOOR && b.getType() != Material.IRON_DOOR_BLOCK && b.getType() != Material.IRON_TRAPDOOR) { return true; } } return false; }
@EventHandler public void onLeverOrButton(PlayerInteractEvent event) { Block clickedBlock = event.getClickedBlock(); Player player = event.getPlayer(); if (clickedBlock == null) return; String chunk = clickedBlock.getLocation().getChunk().getX() + ";" + clickedBlock.getLocation().getChunk().getZ(); if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; if (!powerable.containsKey(clickedBlock.getWorld().getName())) return; if (powerable.get(clickedBlock.getWorld().getName()).getList(chunk).contains(clickedBlock.getLocation())) return; // We cancel; send smoke particles for button, and we just turn off the // lever (if it was, for some reason, on). if (clickedBlock.getType() == Material.STONE_BUTTON || clickedBlock.getType() == Material.WOOD_BUTTON) { clickedBlock.getWorld().spawnParticle(Particle.SMOKE_NORMAL, clickedBlock.getLocation().add(0.5, 1, 0.5), 7, 0, 0.2, 0, 0.03); player.sendMessage(MortuusTerraCore.NOTI_PREFIX + ChatColor.RED + " There is no generator in range!"); } else if (clickedBlock.getType() == Material.LEVER) { BlockState state = clickedBlock.getState(); Lever lever = (Lever) state.getData(); lever.setPowered(false); state.setData(lever); state.update(); clickedBlock.getWorld().spawnParticle(Particle.SMOKE_NORMAL, clickedBlock.getLocation().add(0.5, 1, 0.5), 7, 0, 0.2, 0, 0.03); player.sendMessage(MortuusTerraCore.NOTI_PREFIX + ChatColor.RED + " There is no generator in range!"); } }
@Override public Text translateAction(User user) { // TODO plural if (this.newBlock.as(Lever.class).isPowered()) { return user.getTranslation(POSITIVE, "{user} activated the lever", this.player.name); } return user.getTranslation(POSITIVE, "{user} deactivated the lever", this.player.name); }
public Lever() { }
@Deprecated public Lever(int type) { }
public Lever(Material type) { }
@Deprecated public Lever(int type, byte data) { }
@Deprecated public Lever(Material type, byte data) { }
public Lever clone() { return null; }
public void postTeleport(final ABPortal from, final ABPortal portal, final ModInfo info, final Entity entity) { final Block block = info.location.getBlock(); if (block.getType() != Material.LEVER) return; final ABPortal owner = info.getPortal(); if ( owner == null ) return; final Material m = info.item.getType(); final boolean player = entity instanceof Player; if ( owner.equals(portal) ) { // Entity Incoming if ((m == Material.STONE_PLATE && !player) || (m != Material.STONE_PLATE && m != Material.WOOD_PLATE)) return; } else { // Entity Outgoing if ((m == Material.STONE_BUTTON && !player) || (m != Material.STONE_BUTTON && m != Material.WOOD_BUTTON)) return; } final long duration = parseDuration(info.flags.get("duration")); final BukkitScheduler scheduler = portal.getPlugin().getServer().getScheduler(); final BlockState bs = block.getState(); final Lever lever = (Lever) bs.getData(); final boolean powered = lever.isPowered(); final Flipper flipper; if (info.task != -1) { flipper = new Flipper(info, block, !powered); scheduler.cancelTask(info.task); } else { flipper = new Flipper(info, block, powered); lever.setPowered(!powered); bs.setData(lever); bs.update(); } info.task = scheduler.scheduleSyncDelayedTask(portal.getPlugin(), flipper, duration); }