public void previewInventory(Player viewer, Inventory realInventory) { if(viewer == null) { return; } if(realInventory instanceof PlayerInventory) { previewPlayerInventory(viewer, (PlayerInventory) realInventory); }else { Inventory fakeInventory; if(realInventory instanceof DoubleChestInventory) { if(realInventory.hasCustomName()) { fakeInventory = Bukkit.createInventory(viewer, realInventory.getSize(), realInventory.getName()); } else { fakeInventory = Bukkit.createInventory(viewer, realInventory.getSize()); } } else { if(realInventory.hasCustomName()) { fakeInventory = Bukkit.createInventory(viewer, realInventory.getType(), realInventory.getName()); } else { fakeInventory = Bukkit.createInventory(viewer, realInventory.getType()); } } fakeInventory.setContents(realInventory.contents()); this.showInventoryPreview(viewer, realInventory, fakeInventory); } }
/** * Gets the attached {@link Sign}s on a {@link Block}. * * @param block * the {@link Block} to get for * @return collection of attached {@link Sign}s */ public Collection<Sign> getAttachedSigns(Block block) { Set<Sign> results = new HashSet<>(); getSignsAround(block, results); BlockState state = block.getState(); if (state instanceof Chest) { Inventory chestInventory = ((Chest) state).getInventory(); if (chestInventory instanceof DoubleChestInventory) { DoubleChest doubleChest = ((DoubleChestInventory) chestInventory).getHolder(); Block left = ((Chest) doubleChest.getLeftSide()).getBlock(); Block right = ((Chest) doubleChest.getRightSide()).getBlock(); getSignsAround(left.equals(block) ? right : left, results); } } return results; }
/** * Gets the single block inventory for a potentially double chest. * Handles people who have an old version of Bukkit. * This should be replaced with {@link org.bukkit.block.Chest#getBlockInventory()} * in a few months (now = March 2012) // note from future dev - lol * * @param chest The chest to get a single block inventory for * @return The chest's inventory */ private Inventory getBlockInventory(Chest chest) { try { return chest.getBlockInventory(); } catch (Throwable t) { if (chest.getInventory() instanceof DoubleChestInventory) { DoubleChestInventory inven = (DoubleChestInventory) chest.getInventory(); if (inven.getLeftSide().getHolder().equals(chest)) { return inven.getLeftSide(); } else if (inven.getRightSide().getHolder().equals(chest)) { return inven.getRightSide(); } else { return inven; } } else { return chest.getInventory(); } } }
/** * Connected chests that comprise the inventory of this account chest. * @return chest blocks connected to this chest, if any */ private Chest[] connectedChests() { Inventory inv = inventory(); if (inv == null) return new Chest[0]; if (inv instanceof DoubleChestInventory) { DoubleChestInventory dinv = (DoubleChestInventory)inv; Chest left = (Chest)(dinv.getLeftSide().getHolder()); Chest right = (Chest)(dinv.getRightSide().getHolder()); return new Chest[] {left, right}; } else { InventoryHolder invHolder = inv.getHolder(); if (invHolder instanceof Chest) return new Chest[] {(Chest)(inv.getHolder())}; } return new Chest[0]; }
/** * Determine whether the chest of another AccountChest would be connected to this chest. * @param chest another AccountChest * @return whether the chest of another AccountChest would be connected to this chest */ public boolean connected(AccountChest chest) { // no valid account chest anymore -> no connection if (! updateValid()) return false; if (chestLocation().equals(chest.chestLocation())) return true; // no double chest -> no further connection possible if (! (inventory() instanceof DoubleChestInventory)) return false; Location myLoc = chestLocation(); for (Chest c : chest.connectedChests()) if (c.getLocation().equals(myLoc)) return true; return false; }
@EventHandler(priority = EventPriority.HIGHEST) public void onInventoryOpenEvent(InventoryOpenEvent event) { if (event.getInventory() instanceof DoubleChestInventory) { DoubleChestInventory doubleInv = (DoubleChestInventory)event.getInventory(); Chest leftChest = (Chest)doubleInv.getHolder().getLeftSide(); /*Generate a new player 'switch' event for the left and right chests. */ PlayerInteractEvent interactLeft = new PlayerInteractEvent((Player)event.getPlayer(), Action.RIGHT_CLICK_BLOCK, null, leftChest.getBlock(), null); BlockListener.OnPlayerSwitchEvent(interactLeft); if (interactLeft.isCancelled()) { event.setCancelled(true); return; } Chest rightChest = (Chest)doubleInv.getHolder().getRightSide(); PlayerInteractEvent interactRight = new PlayerInteractEvent((Player)event.getPlayer(), Action.RIGHT_CLICK_BLOCK, null, rightChest.getBlock(), null); BlockListener.OnPlayerSwitchEvent(interactRight); if (interactRight.isCancelled()) { event.setCancelled(true); return; } } }
private void updateOtherDoubleChestBlocks() { if (cachedInv.getHolder() instanceof DoubleChest) { Material chestMaterial = block.getType(); Location otherChestLoc = null; for (WrappedDirection pd : WrappedDirection.values()) { if (pd.isSide()) { if (block.getRelative(pd.getX(), pd.getY(), pd.getZ()).getType() == chestMaterial) { otherChestLoc = block.getRelative(pd.getX(), pd.getY(), pd.getZ()).getLocation(); } } } Map<BlockLoc, TransportPipesContainer> containerMap = TransportPipes.instance .getContainerMap(block.getWorld()); if (containerMap != null) { BlockLoc bl = BlockLoc.convertBlockLoc(otherChestLoc); if (containerMap.containsKey(bl)) { TransportPipesContainer tpc = containerMap.get(bl); if (tpc instanceof SimpleInventoryContainer) { SimpleInventoryContainer sic = (SimpleInventoryContainer) tpc; if (!(sic.cachedInv instanceof DoubleChestInventory)) { sic.updateBlock(); } } } } } }
/** * Returns the inventory belonging only to the given block, filtering out * double inventories. Does not check whether the given block has an * inventory. * * @return The inventory for the given block. */ public static Inventory getSafeInventory(Block block) { Inventory inventory = ((InventoryHolder) block.getState()).getInventory(); if (inventory instanceof DoubleChestInventory) { DoubleChestInventory doubleChest = (DoubleChestInventory) inventory; Inventory left = doubleChest.getLeftSide(); if (((BlockState) doubleChest.getLeftSide().getHolder()).getBlock().equals(block)) { return left; } else { return doubleChest.getRightSide(); } } else { return inventory; } }
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL) public void onPlayerInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); Action action = event.getAction(); ItemStack stack = event.getItem(); // Keys can only be used by right clicking blocks. if (action != Action.RIGHT_CLICK_BLOCK) return; Key key = plugin.getKeyManager().getKey(stack); // No keys were used in the making of this video. if (key == null) return; Block block = event.getClickedBlock(); BlockState state = block.getState(); if (key instanceof EventKey && state instanceof Chest) { EventKey eventKey = (EventKey) key; EventKey.EventKeyData eventKeyData = eventKey.getData(stack.getItemMeta().getLore()); EventType eventType = eventKeyData.getEventType(); List<Inventory> inventories = eventKey.getInventories(eventType); int inventoryNumber = eventKeyData.getInventoryNumber(); if (inventories.size() < inventoryNumber) { player.sendMessage(ChatColor.RED + "This key is for " + eventType.getDisplayName() + ChatColor.RED + " loottable " + inventoryNumber + ", whilst there are only " + inventories.size() + " possible. Please inform an admin."); return; } Inventory inventory = inventories.get(inventoryNumber - 1); ItemStack[] contents = inventory.getContents(); Chest chest = (Chest) state; InventoryHolder inventoryHolder = chest.getInventory().getHolder(); if (inventoryHolder instanceof DoubleChestInventory) { inventoryHolder = ((DoubleChestInventory) inventoryHolder).getHolder(); } if (contents.length > chest.getInventory().getSize()) { player.sendMessage(ChatColor.RED + "This single chest is too small to fit the contents of this key."); return; } Inventory chestInventory = inventoryHolder.getInventory(); if (!InventoryUtils.isEmpty(chestInventory)) { player.sendMessage(ChatColor.RED + "This chest is not empty."); return; } chestInventory.setContents(inventory.getContents()); decrementHand(player); event.setCancelled(true); player.openInventory(chestInventory); player.sendMessage(ChatColor.YELLOW + "Your " + ChatColor.AQUA + eventType.getDisplayName() + ' ' + eventKey.getDisplayName() + ChatColor.YELLOW + " key has transferred loot " + inventoryNumber + ChatColor.YELLOW + " to the chest."); } }
public DoubleChest(DoubleChestInventory chest) { inventory = chest; }
public void addInventory (DoubleChestInventory inv) { invs.add(inv.getLeftSide()); invs.add(inv.getRightSide()); }
public DoubleChest(DoubleChestInventory chest) { }