@EventHandler(ignoreCancelled = true) // Handles special items entering hoppers. public void onHopperPickup(InventoryPickupItemEvent evt) { if (!(evt.getInventory().getHolder() instanceof Hopper)) return; // Verify the inventory the item is going to enter is a hopper. Hopper hp = (Hopper) evt.getInventory().getHolder(); Matcher mName = Pattern.compile("<Custom ID: (\\w+)>").matcher(hp.getInventory().getName()); if (!mName.find()) return; // If it doesn't have a Custom item ID defined, don't handle it. ItemWrapper iw = ItemManager.constructItem(evt.getItem().getItemStack()); evt.setCancelled(true); if (mName.group(1).equalsIgnoreCase(iw.getTagString("id"))) { // We've found the right item! Consume it. evt.getItem().remove(); hp.getBlock().getRelative(BlockFace.DOWN).setType(Material.REDSTONE_BLOCK); } else { // This item isn't acceptable, spit it back out. evt.getItem().setVelocity(new Vector(0, .15F, 0)); } }
/** * Updates the pearl holder * @param pearl The pearl to update * @param holder The pearl holder * @param event The event */ private void updatePearlHolder(ExilePearl pearl, InventoryHolder holder, Cancellable event) { if (holder instanceof Chest) { updatePearl(pearl, (Chest)holder); } else if (holder instanceof DoubleChest) { updatePearl(pearl, (Chest) ((DoubleChest) holder).getLeftSide()); } else if (holder instanceof Furnace) { updatePearl(pearl, (Furnace) holder); } else if (holder instanceof Dispenser) { updatePearl(pearl, (Dispenser) holder); } else if (holder instanceof Dropper) { updatePearl(pearl, (Dropper) holder); } else if (holder instanceof Hopper) { updatePearl(pearl, (Hopper) holder); } else if (holder instanceof BrewingStand) { updatePearl(pearl, (BrewingStand) holder); } else if (holder instanceof Player) { updatePearl(pearl, (Player) holder); }else { event.setCancelled(true); } }
@EventHandler public void onBlockBreak(BlockBreakEvent event){ if(event.getPlayer().getGameMode() == GameMode.CREATIVE){ return; } if (event.getBlock().getType() == Material.HOPPER){ Hopper hopper = (Hopper)event.getBlock().getState(); if(plugin.checkNames(hopper.getInventory().getName())){ ItemStack drop = plugin.getItem(); //Looks hacky event.setCancelled(true); event.getBlock().setType(Material.AIR); event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), drop); //-- Not working. Why? //event.getBlock().getDrops().clear(); //event.getBlock().getDrops().add(drop); } } }
public SyncHopper(String hopperPath, int ID, Location hopperLoc, Location source, Location dest) { this.hopperFile = new File(hopperPath + File.separator + ID + ".yml"); this.hopperConfig = YamlConfiguration.loadConfiguration(this.hopperFile); this.HopperID = ID; this.hopperLoc = hopperLoc.clone(); this.hopper = (Hopper) hopperLoc.getBlock().getState(); this.sourceLoc = source.clone(); this.destLoc = dest.clone(); if (this.destLoc.getWorld() != null && this.sourceLoc.getWorld() != null) { this.source = SyncManager.getManager().getInventory(this.sourceLoc); } this.inUse = false; this.item = null; save(); }
public SyncHopper(File hopperFile) { this.hopperFile = hopperFile; this.hopperConfig = YamlConfiguration.loadConfiguration(this.hopperFile); this.HopperID = this.hopperConfig.getInt("config.Hopper.ID"); this.hopperLoc = new Location(Bukkit.getWorld(UUID.fromString(this.hopperConfig.getString("config.Hopper.Location.world"))), this.hopperConfig.getInt("config.Hopper.Location.X"), this.hopperConfig.getInt("config.Hopper.Location.Y"), this.hopperConfig.getInt("config.Hopper.Location.Z")); this.destLoc = new Location(Bukkit.getWorld(UUID.fromString(this.hopperConfig.getString("config.Hopper.Location.Destination.world"))), this.hopperConfig.getInt("config.Hopper.Location.Destination.X"), this.hopperConfig.getInt("config.Hopper.Location.Destination.Y"), this.hopperConfig.getInt("config.Hopper.Location.Destination.Z")); this.sourceLoc = new Location(Bukkit.getWorld(UUID.fromString(this.hopperConfig.getString("config.Hopper.Location.Source.world"))), this.hopperConfig.getInt("config.Hopper.Location.Source.X"), this.hopperConfig.getInt("config.Hopper.Location.Source.Y"), this.hopperConfig.getInt("config.Hopper.Location.Source.Z")); if (this.hopperLoc.getWorld() != null) { this.hopper = (Hopper) this.hopperLoc.getBlock().getState(); } if (this.sourceLoc.getWorld() != null) { this.source = SyncManager.getManager().getInventory(this.sourceLoc); } }
@EventHandler public void onFurnaceInsertHopper(InventoryMoveItemEvent event) { if (event.getDestination().getType() != InventoryType.FURNACE) { return; } if (event.getSource().getHolder() instanceof Hopper) { Block b1 = ((BlockState) event.getSource().getHolder()).getBlock(); Block b2 = ((BlockState) event.getDestination().getHolder()).getBlock(); if (b1.getY() == b2.getY() + 1) { // hopper above the furnace - trying to insert items to be smelted if (!validateSmeltingIngredient(event.getItem())) { event.setCancelled(true); } } } }
public WorldInfo(World world) { this.worldName = world.getName(); this.totalOnline = world.getPlayers().size(); for (Entity entity : world.getEntities()) { this.totalEntity++; if (entity instanceof Animals) { this.totalAnimals++; } else if (entity instanceof Monster) { this.totalMonsters++; } else if (entity instanceof Item) { this.totalDropItem++; } } for (Chunk loadedChunk : world.getLoadedChunks()) { this.totalChunk++; for (BlockState tiles : loadedChunk.getTileEntities()) { this.totalTiles++; if (tiles instanceof Hopper) { this.totalHopper++; } else if (tiles instanceof Chest) { this.totalChest++; } else if (tiles instanceof Dispenser) { this.totalDispenser++; } else if (tiles instanceof Dropper) { this.totalDropper++; } else if (tiles instanceof BrewingStand) { this.totalBrewingStand++; } } } }
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onHopper(InventoryMoveItemEvent e) { if (cm.isAntiNetherHopper) { if (e.getInitiator().getHolder() instanceof Hopper) { Hopper hopper = (Hopper) e.getInitiator().getHolder(); if (hopper.getWorld().getEnvironment() == World.Environment.NETHER) { e.setCancelled(true); } } } }
public static void destroyThisBlock(Block blk, Town town) { WarRegen.saveBlock(blk, town.getName(), false); switch (blk.getType()) { case TRAPPED_CHEST: ((Chest)blk.getState()).getBlockInventory().clear(); break; case CHEST: ((Chest)blk.getState()).getBlockInventory().clear(); break; case DISPENSER: ((Dispenser)blk.getState()).getInventory().clear(); break; case BURNING_FURNACE: case FURNACE: ((Furnace)blk.getState()).getInventory().clear(); break; case DROPPER: ((Dropper)blk.getState()).getInventory().clear(); break; case HOPPER: ((Hopper)blk.getState()).getInventory().clear(); break; default: break; } ItemManager.setTypeId(blk, CivData.AIR); ItemManager.setData(blk, 0x0, true); }
/** * Listens to InventoryMoveItemEvent to handle IGNITION_CHAMBER. * @param e InventoryMoveItemEvent * @since 4.1.11 */ @EventHandler public void onIgnitionChamberItemMove(InventoryMoveItemEvent e) { if (e.getInitiator().getHolder() instanceof Hopper) { if (BlockStorage.check(((Hopper) e.getInitiator().getHolder()).getBlock(), "IGNITION_CHAMBER")) { e.setCancelled(true); } } }
private Block getHopperBlock(Hopper h) { int i = h.getData().toString().charAt(7) - '0'; if (i <= 5) { return h.getBlock().getRelative(hopperMap[i]); } else { //Something strange happened, I keep getting the number 8 when I look inside a chest above a hopper? return h.getBlock(); } }
/** * @return number of hoppers on the island */ public int getHopperCount() { tileEntityCount.clear(); int result = 0; for (int x = getMinProtectedX() /16; x <= (getMinProtectedX() + getProtectionSize() - 1)/16; x++) { for (int z = getMinProtectedZ() /16; z <= (getMinProtectedZ() + getProtectionSize() - 1)/16; z++) { for (BlockState holder : world.getChunkAt(x, z).getTileEntities()) { if (holder instanceof Hopper && onIsland(holder.getLocation())) { result++; } } } } return result; }
public SyncHopper getHopper(Inventory inventory) { if (inventory.getHolder() != null && inventory.getHolder() instanceof Hopper) { if (getHopper(((Hopper) inventory.getHolder()).getLocation()) != null) { return getHopper(((Hopper) inventory.getHolder()).getLocation()); } } return null; }
/** * Event handler to watch whenever a container tries to move an item to another container. * Hoppers for example are the only thing that triggers this so far. * If the item being moved is not allowed in the destination container, then the event will be canceled to prevent the move. * * @param event InventoryMoveItemEvent containing data about the event given by the system. We care about hoppers being the receiving end of the transfer. */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled=true) public void onInventoryMoveItemEvent(InventoryMoveItemEvent event) { //Get destination container InventoryHolder destHolder = event.getDestination().getHolder(); //since we only care about hoppers, then ignore everything else if (destHolder instanceof Hopper) { //Get the location of the hopper in question. Block hopperBlock = ((Hopper) destHolder).getBlock(); //Get the cache, if any, for the hopper location. String cache = plugin.knownHoppersCache_Get(hopperBlock); //get the itemStack that was moved and convert it to a String for comparison String eventItemInformation = plugin.GetItemInformationForInventory(event.getItem(), false); //debug if requested if (plugin.debugLevel_get() > 0) plugin.getLogger().info("Testing item (" + eventItemInformation + ") in hopper (" + hopperBlock.getLocation().toString() + ") against allowed:" + cache); //do the actual filtering if (cache.length() > 0) //we have a cache to compare against if (!cache.contains(eventItemInformation)) //the item is not allowed in the hopper event.setCancelled(true); } }
@EventHandler public void onIgnitionChamberItemMove(InventoryMoveItemEvent e) { if(BlockStorage.check(((Hopper) e.getInitiator().getHolder()).getBlock(), "IGNITION_CHAMBER")) { e.setCancelled(true); } }
@Test public void testOnInventoryMoveTnt() { when(player.getGameMode()).thenReturn(GameMode.SURVIVAL); ItemStack item = new ItemStack(Material.TNT, 1); Inventory top = mock(Inventory.class); when(top.getHolder()).thenReturn(mock(Dropper.class)); when(top.getSize()).thenReturn(9); Inventory bottom = mock(Inventory.class); when(bottom.getHolder()).thenReturn(mock(Player.class)); when(bottom.getSize()).thenReturn(45); InventoryView view = mock(InventoryView.class); when(view.getPlayer()).thenReturn(player); when(view.getTopInventory()).thenReturn(top); when(view.getBottomInventory()).thenReturn(bottom); when(view.getCursor()).thenReturn(item); when(view.getItem(eq(5))).thenReturn(item); when(view.getItem(eq(40))).thenReturn(item); InventoryClickEvent e = new InventoryClickEvent(view, null, 5, ClickType.LEFT, InventoryAction.PLACE_SOME); when(config.canPerform(ExileRule.PLACE_TNT)).thenReturn(true); dut.onInventoryClick(e); assertFalse(e.isCancelled()); e = new InventoryClickEvent(view, null, 5, ClickType.LEFT, InventoryAction.PLACE_SOME); when(config.canPerform(ExileRule.PLACE_TNT)).thenReturn(false); dut.onInventoryClick(e); assertFalse(e.isCancelled()); e = new InventoryClickEvent(view, null, 5, ClickType.LEFT, InventoryAction.PLACE_SOME); when(config.canPerform(ExileRule.PLACE_TNT)).thenReturn(true); when(pearlApi.isPlayerExiled(uid)).thenReturn(true); dut.onInventoryClick(e); assertFalse(e.isCancelled()); e = new InventoryClickEvent(view, null, 5, ClickType.LEFT, InventoryAction.PLACE_SOME); when(config.canPerform(ExileRule.PLACE_TNT)).thenReturn(false); dut.onInventoryClick(e); assertTrue(e.isCancelled()); e = new InventoryClickEvent(view, null, 5, ClickType.LEFT, InventoryAction.PLACE_SOME); when(top.getHolder()).thenReturn(mock(Chest.class)); dut.onInventoryClick(e); assertFalse(e.isCancelled()); e = new InventoryClickEvent(view, null, 5, ClickType.LEFT, InventoryAction.PLACE_ALL); when(top.getHolder()).thenReturn(mock(Hopper.class)); dut.onInventoryClick(e); assertTrue(e.isCancelled()); e = new InventoryClickEvent(view, null, 5, ClickType.LEFT, InventoryAction.PLACE_ONE); when(top.getHolder()).thenReturn(mock(Dispenser.class)); dut.onInventoryClick(e); assertTrue(e.isCancelled()); e = new InventoryClickEvent(view, null, 5, ClickType.SHIFT_LEFT, InventoryAction.MOVE_TO_OTHER_INVENTORY); dut.onInventoryClick(e); assertFalse(e.isCancelled()); e = new InventoryClickEvent(view, null, 40, ClickType.LEFT, InventoryAction.PLACE_SOME); dut.onInventoryClick(e); assertFalse(e.isCancelled()); e = new InventoryClickEvent(view, null, 40, ClickType.SHIFT_LEFT, InventoryAction.MOVE_TO_OTHER_INVENTORY); when(view.getType()).thenReturn(InventoryType.DISPENSER); assertEquals(e.getCurrentItem(), item); dut.onInventoryClick(e); assertTrue(e.isCancelled()); }
private static String blockToString(Block blk, boolean save_as_air) { if (save_as_air) { return blockAsAir(blk); } else { String str = blockBasicString(blk); Inventory inv = null; switch (blk.getType()) { case TRAPPED_CHEST: case CHEST: inv = ((Chest)blk.getState()).getBlockInventory(); str += blockInventoryString(inv); break; case DISPENSER: inv = ((Dispenser)blk.getState()).getInventory(); str += blockInventoryString(inv); break; case BURNING_FURNACE: case FURNACE: inv = ((Furnace)blk.getState()).getInventory(); str += blockInventoryString(inv); break; case DROPPER: inv = ((Dropper)blk.getState()).getInventory(); str += blockInventoryString(inv); break; case HOPPER: inv = ((Hopper)blk.getState()).getInventory(); str += blockInventoryString(inv); break; case SIGN: case SIGN_POST: case WALL_SIGN: Sign sign = (Sign)blk.getState(); str += blockSignString(sign); break; default: break; } return str; } }
private static void restoreBlockFromString(String line) { String[] split = line.split(":"); int type = Integer.valueOf(split[0]); byte data = Byte.valueOf(split[1]); int x = Integer.valueOf(split[2]); int y = Integer.valueOf(split[3]); int z = Integer.valueOf(split[4]); String world = split[5]; Block block = BukkitObjects.getWorld(world).getBlockAt(x,y,z); ItemManager.setTypeId(block, type); ItemManager.setData(block, data, false); // End of basic block info, try to get more now. Inventory inv = null; switch (block.getType()) { case TRAPPED_CHEST: inv = ((Chest)block.getState()).getBlockInventory(); InventorySerializer.StringToInventory(inv, split[6]); break; case CHEST: inv = ((Chest)block.getState()).getBlockInventory(); InventorySerializer.StringToInventory(inv, split[6]); break; case DISPENSER: inv = ((Dispenser)block.getState()).getInventory(); InventorySerializer.StringToInventory(inv, split[6]); break; case BURNING_FURNACE: case FURNACE: inv = ((Furnace)block.getState()).getInventory(); InventorySerializer.StringToInventory(inv, split[6]); break; case DROPPER: inv = ((Dropper)block.getState()).getInventory(); InventorySerializer.StringToInventory(inv, split[6]); break; case HOPPER: inv = ((Hopper)block.getState()).getInventory(); InventorySerializer.StringToInventory(inv, split[6]); break; case SIGN: case SIGN_POST: case WALL_SIGN: Sign sign = (Sign)block.getState(); String[] messages = split[6].split(","); for (int i = 0; i < 4; i++) { if (messages[i] != null) { sign.setLine(i, messages[i]); } } sign.update(); break; default: break; } }
@EventHandler public void onBlockBreak(BlockBreakEvent event) { Player eventPlayer = event.getPlayer(); MessageManager msg = MessageManager.getManager(); if (event.getPlayer().getItemInHand() != null) { if (event.getPlayer().getItemInHand().equals(SyncChest.connector)) { event.setCancelled(true); return; } } if (event.getBlock().getState() instanceof Chest) { Chest chest = (Chest) event.getBlock().getState(); if (chest.getInventory().getTitle().contains("MainChest") || chest.getInventory().getTitle().contains("RelatedChest")) { if (eventPlayer.hasPermission("sc.remove")) { boolean success = false; if (chest.getInventory().getTitle().contains("MainChest")) { success = SyncManager.getManager().removeChest(ChestType.MAIN, event.getBlock().getLocation()); } else if (chest.getInventory().getTitle().contains("RelatedChest")) { success = SyncManager.getManager().removeChest(ChestType.RELATED, event.getBlock().getLocation()); } if (success) { eventPlayer.sendMessage(msg.get_CHEST_REMOVED()); } else { eventPlayer.sendMessage(msg.ERROR_CHEST_REMOVE()); } return; } else { eventPlayer.sendMessage(msg.ERROR_NO_PERMISSIONS()); event.setCancelled(true); return; } } } if (event.getBlock().getState() instanceof Hopper) { if (SyncManager.getManager().getHopper(event.getBlock().getLocation()) != null) { if (eventPlayer.hasPermission("sc.remove")) { if (SyncManager.getManager().removeHopper(event.getBlock().getLocation()) == true) { eventPlayer.sendMessage(msg.get_HOPPER_REMOVED()); return; } else { eventPlayer.sendMessage(msg.ERROR_HOPPER_REMOVE()); return; } } else { eventPlayer.sendMessage(msg.ERROR_NO_PERMISSIONS()); event.setCancelled(true); return; } } } }
public Hopper getHopper() { return this.hopper; }
@EventHandler public void onInteraction(PlayerInteractEvent event) { if (testObserver(event.getPlayer())) { event.setCancelled(true); if ((event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && (event.getPlayer().getInventory().getItemInMainHand() != null && event.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.WRITTEN_BOOK))){ event.setUseInteractedBlock(Event.Result.DENY); event.setUseItemInHand(Event.Result.ALLOW); } if (event.getClickedBlock() != null && !event.getPlayer().isSneaking() && event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (event.getClickedBlock().getType().equals(Material.CHEST) || event.getClickedBlock().getType().equals(Material.TRAPPED_CHEST)) { Inventory chest = Bukkit.createInventory(null, ((Chest) event.getClickedBlock().getState()).getInventory().getSize()); for (int i = 0; i < ((Chest) event.getClickedBlock().getState()).getInventory().getSize(); i++) { chest.setItem(i, ((Chest) event.getClickedBlock().getState()).getInventory().getItem(i)); } event.getPlayer().openInventory(chest); } if (event.getClickedBlock().getType().equals(Material.FURNACE) || event.getClickedBlock().getType().equals(Material.BURNING_FURNACE)) { Inventory furnace = Bukkit.createInventory(null, InventoryType.FURNACE); for (int i = 0; i < ((Furnace) event.getClickedBlock().getState()).getInventory().getSize(); i++) { furnace.setItem(i, ((Furnace) event.getClickedBlock().getState()).getInventory().getItem(i)); } event.getPlayer().openInventory(furnace); } if (event.getClickedBlock().getType().equals(Material.DISPENSER)) { Inventory dispenser = Bukkit.createInventory(null, InventoryType.DISPENSER); for (int i = 0; i < ((Dispenser) event.getClickedBlock().getState()).getInventory().getSize(); i++) { dispenser.setItem(i, ((Dispenser) event.getClickedBlock().getState()).getInventory().getItem(i)); } event.getPlayer().openInventory(dispenser); } if (event.getClickedBlock().getType().equals(Material.DROPPER)) { Inventory dropper = Bukkit.createInventory(null, InventoryType.DROPPER); for (int i = 0; i < ((Dropper) event.getClickedBlock().getState()).getInventory().getSize(); i++) { dropper.setItem(i, ((Dropper) event.getClickedBlock().getState()).getInventory().getItem(i)); } event.getPlayer().openInventory(dropper); } if (event.getClickedBlock().getType().equals(Material.HOPPER)) { Inventory hopper = Bukkit.createInventory(null, InventoryType.HOPPER); for (int i = 0; i < ((Hopper) event.getClickedBlock().getState()).getInventory().getSize(); i++) { hopper.setItem(i, ((Hopper) event.getClickedBlock().getState()).getInventory().getItem(i)); } event.getPlayer().openInventory(hopper); } if (event.getClickedBlock().getType().equals(Material.BREWING_STAND)) { Inventory brewingStand = Bukkit.createInventory(null, InventoryType.BREWING); for (int i = 0; i < ((BrewingStand) event.getClickedBlock().getState()).getInventory().getSize(); i++) { brewingStand.setItem(i, ((BrewingStand) event.getClickedBlock().getState()).getInventory().getItem(i)); } event.getPlayer().openInventory(brewingStand); } if (event.getClickedBlock().getType().equals(Material.BEACON)) { Inventory beacon = Bukkit.createInventory(null, InventoryType.BEACON); for (int i = 0; i < ((Beacon) event.getClickedBlock().getState()).getInventory().getSize(); i++) { beacon.setItem(i, ((Beacon) event.getClickedBlock().getState()).getInventory().getItem(i)); } event.getPlayer().openInventory(beacon); } } } }