/** * Return a predicate that applies a Filter to the given InventoryHolder, * or null if the InventoryHolder is not something that we should be filling. */ private static @Nullable Predicate<Filter> passesFilter(InventoryHolder holder) { if(holder instanceof DoubleChest) { final DoubleChest doubleChest = (DoubleChest) holder; return filter -> !filter.denies((Chest) doubleChest.getLeftSide()) || !filter.denies((Chest) doubleChest.getRightSide()); } else if(holder instanceof BlockState) { return filter -> !filter.denies((BlockState) holder); } else if(holder instanceof Player) { // This happens with crafting inventories, and possibly other transient inventory types // Pretty sure we never want to fill an inventory held by the player return null; } else if(holder instanceof Entity) { return filter -> !filter.denies(new EntityQuery((Entity) holder)); } else { // If we're not sure what it is, don't fill it return null; } }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void checkWorth(InventoryOpenEvent event) { // Do nothing if a player did not open the inventory or if chest events // are disabled. if (!(event.getPlayer() instanceof Player) || plugin.getSettings().isDisableChestEvents()) { return; } Inventory inventory = event.getInventory(); // Set all default worth values for this chest. if (inventory.getHolder() instanceof DoubleChest) { DoubleChest chest = (DoubleChest) inventory.getHolder(); checkWorth((Chest) chest.getLeftSide()); checkWorth((Chest) chest.getRightSide()); } if (inventory.getHolder() instanceof Chest) { checkWorth((Chest) inventory.getHolder()); } }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void updateWorth(InventoryCloseEvent event) { // Do nothing if a player did not close the inventory or if chest // events are disabled. if (!(event.getPlayer() instanceof Player) || plugin.getSettings().isDisableChestEvents()) { return; } // Get cached values from when chest was opened and add the difference // to the worth manager. if (event.getInventory().getHolder() instanceof DoubleChest) { DoubleChest chest = (DoubleChest) event.getInventory().getHolder(); updateWorth((Chest) chest.getLeftSide()); updateWorth((Chest) chest.getRightSide()); } if (event.getInventory().getHolder() instanceof Chest) { updateWorth((Chest) event.getInventory().getHolder()); } }
@EventHandler public void onInventoryCloseEvent(InventoryCloseEvent event) { //long time=System.nanoTime(); if(event.getInventory().getType()==InventoryType.CHEST&&CleanShop.shopScan) { Vector<ProtectedRegion> regions=null; if(event.getInventory().getHolder() instanceof Chest) regions=ShopUtils.getRegions(((Chest)event.getInventory().getHolder()).getLocation()); if(event.getInventory().getHolder() instanceof DoubleChest) regions=ShopUtils.getRegions(((DoubleChest)event.getInventory().getHolder()).getLocation()); if(regions!=null) for(ProtectedRegion p:regions) { if(p!=null) if(ShopUtils.shopExists(p)) { //plugin.calculateShopStock(plugin.getShop(p)); if(event.getInventory().getHolder() instanceof Chest) ChestUtils.calculateChestStock((Chest)event.getInventory().getHolder(), ShopUtils.getShop(p)); else ChestUtils.calculateChestStock((DoubleChest)event.getInventory().getHolder(), ShopUtils.getShop(p)); FileHandler.saveShops(); } } } }
/** * 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); } }
/** Remove a shop * @param shop Shop to remove * @param removeFromDatabase Whether the shop should also be removed from the database * @param callback Callback that - if succeeded - returns null */ public void removeShop(Shop shop, boolean removeFromDatabase, Callback<Void> callback) { plugin.debug("Removing shop (#" + shop.getID() + ")"); InventoryHolder ih = shop.getInventoryHolder(); if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; Chest r = (Chest) dc.getRightSide(); Chest l = (Chest) dc.getLeftSide(); shopLocation.remove(r.getLocation()); shopLocation.remove(l.getLocation()); } else { shopLocation.remove(shop.getLocation()); } shop.removeItem(); shop.removeHologram(); if (removeFromDatabase) { plugin.getShopDatabase().removeShop(shop, callback); } else { if (callback != null) callback.callSyncResult(null); } }
/** * Get the amount of shops of a player * @param p Player, whose shops should be counted * @return The amount of a shops a player has (if {@link Config#exclude_admin_shops} is true, admin shops won't be counted) */ public int getShopAmount(OfflinePlayer p) { float shopCount = 0; for (Shop shop : getShops()) { if (shop.getVendor().equals(p)) { if (shop.getShopType() != Shop.ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) { shopCount++; InventoryHolder ih = shop.getInventoryHolder(); if (ih instanceof DoubleChest) shopCount -= 0.5; } } } return Math.round(shopCount); }
private void remove(final Shop shop, final Block b, final Player p) { if (shop.getInventoryHolder() instanceof DoubleChest) { DoubleChest dc = (DoubleChest) shop.getInventoryHolder(); final Chest l = (Chest) dc.getLeftSide(); final Chest r = (Chest) dc.getRightSide(); Location loc = (b.getLocation().equals(l.getLocation()) ? r.getLocation() : l.getLocation()); final Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), loc, shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); shopUtils.removeShop(shop, true, new Callback<Void>(plugin) { @Override public void onResult(Void result) { newShop.create(true); shopUtils.addShop(newShop, true); } }); } else { shopUtils.removeShop(shop, true); plugin.debug(String.format("%s broke %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID())); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED)); } }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onItemMove(InventoryMoveItemEvent e) { if (config.hopper_protection) { if ((e.getSource().getType().equals(InventoryType.CHEST)) && (!e.getInitiator().getType().equals(InventoryType.PLAYER))) { if (e.getSource().getHolder() instanceof DoubleChest) { DoubleChest dc = (DoubleChest) e.getSource().getHolder(); Chest r = (Chest) dc.getRightSide(); Chest l = (Chest) dc.getLeftSide(); if (shopUtils.isShop(r.getLocation()) || shopUtils.isShop(l.getLocation())) e.setCancelled(true); } else if (e.getSource().getHolder() instanceof Chest) { Chest c = (Chest) e.getSource().getHolder(); if (shopUtils.isShop(c.getLocation())) e.setCancelled(true); } } } }
public void setHolder(InventoryHolder holder) throws CivException { if (holder instanceof Player) { Player player = (Player)holder; playerName = player.getName(); blockLocation = null; return; } if (holder instanceof Chest) { Chest chest = (Chest)holder; playerName = null; blockLocation = chest.getLocation(); return; } if (holder instanceof DoubleChest) { DoubleChest dchest = (DoubleChest)holder; playerName = null; blockLocation = dchest.getLocation(); return; } throw new CivException("Invalid holder passed to set holder:"+holder.toString()); }
private Location getLocationForHolder(InventoryHolder holder) { if (holder instanceof Entity) { return ((Entity)holder).getLocation(); } else if (holder instanceof DoubleChest) { //((Chest)inventory.getLeftSide().getHolder()).getLocation() return ((DoubleChest)holder).getLocation(); //TODO get the correct chest } else if (holder instanceof BlockState) { return ((BlockState)holder).getLocation(); } if (holder != null) { this.module.getLog().debug("Unknown InventoryHolder: {}", holder.getClass().getName()); } return null; }
private Location getLocationForHolder(InventoryHolder holder) { if (holder instanceof Entity) { return ((Entity)holder).getLocation(); } else if (holder instanceof DoubleChest) { //((Chest)inventory.getLeftSide().getHolder()).getLocation() return ((DoubleChest)holder).getLocation(); //TODO get the correct chest } else if (holder instanceof BlockState) { return ((BlockState)holder).getLocation(); } if (holder != null) { this.module.getLog().warn("Unknown InventoryHolder: {}", holder.getClass().getName()); } return null; }
/** * Get the vanilla inventory for the given block. * * @param target the block containing the target inventory * @return the block's inventory, or null if the block does not have one */ public static Inventory getVanillaInventoryFor(Block target) { switch (target.getType()) { case CHEST: Chest c = (Chest) target.getState(); if (c.getInventory().getHolder() instanceof DoubleChest) { DoubleChest dc = (DoubleChest) c.getInventory().getHolder(); return dc.getInventory(); } else { return c.getBlockInventory(); } case DISPENSER: case HOPPER: case DROPPER: case FURNACE: case BREWING_STAND: case BURNING_FURNACE: return ((InventoryHolder) target.getState()).getInventory(); // any other vanilla inventory types ? default: return null; } }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInventoryOpen(final InventoryOpenEvent event) { final Inventory inventory = event.getInventory(); final InventoryHolder holder = inventory.getHolder(); if (holder instanceof Chest || holder instanceof DoubleChest) { final BlockState chest = (BlockState)(holder instanceof Chest ? holder : ((DoubleChest)holder).getLeftSide()); final Block block = chest.getBlock(); final NLocation loc = new NLocation(block.getLocation()); final List<Sign> signs = SignUtil.getSignsForChest(block); for (final Sign sign : signs) { if (sign.getLine(0).equals(ITEMNETWORK_EMITTER)) { this.feature.lock(loc); } } } }
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(); } } } } } }
private String getPhantomIdentifier(InventoryHolder holder) { if(holder instanceof DoubleChest) { return "Double Chest:" + ((DoubleChest)holder).getLocation().toString(); } else if(holder instanceof Chest) { return "Chest:" + ((Chest)holder).getLocation().toString(); } return null; }
static boolean isSortableChestInventory(Inventory inventory) { if(inventory == null) return false; InventoryType inventoryType = inventory.getType(); if(inventoryType != InventoryType.CHEST && inventoryType != InventoryType.ENDER_CHEST) return false; String name = inventory.getName(); if(name != null && name.contains("*")) return false; InventoryHolder holder = inventory.getHolder(); if(holder == null || !(holder instanceof Chest || holder instanceof DoubleChest || holder instanceof StorageMinecart)) return false; return true; }
private void registerWoolChest(InventoryHolder inventoryHolder) { if (inventoryHolder instanceof DoubleChest) { DoubleChest doubleChest = (DoubleChest) inventoryHolder; // register both blocks this.registerWoolChest(doubleChest.getLeftSide()); this.registerWoolChest(doubleChest.getRightSide()); return; } else if (!(inventoryHolder instanceof BlockState)) { return; } Inventory inventory = inventoryHolder.getInventory(); Block block = ((BlockState) inventoryHolder).getBlock(); ChestImage image = this.getChestImage(block); if (image != null) { // We have already seen this chest, don't register it. return; } this.chestImages.put(block, image = new ChestImage(WoolUtils.containsAny(inventory))); if (image.woolChest) { WoolChestRegisterEvent event = new WoolChestRegisterEvent(this.game.getPlugin(), block, inventory); this.game.getPlugin().getEventBus().publish(event); if (!event.isCanceled()) { for (int slot = 0; slot < inventory.getSize(); slot++) { ItemStack item = inventory.getItem(slot); if (item != null && WoolUtils.isWool(item)) { image.snapshot.put(slot, item.clone()); } } } } }
public static void calculateChestStock(DoubleChest c,Shop s) { ChestData data=s.getChestAt((int)c.getX(), (int)c.getY(), (int)c.getZ()); if(data==null) { data=new ChestData((int)c.getX(), (int)c.getY(), (int)c.getZ(),null); s.chestData.add(data); } invContents(c.getInventory(),data); }
@Override public Location getLocation() { InventoryHolder holder = getHolder(); // I think this covers all possible inventory holders if (holder instanceof BlockState) { return ((BlockState) holder).getLocation(); } if (holder instanceof Entity) { return ((Entity) holder).getLocation(); } if (holder instanceof DoubleChest) { return ((DoubleChest) holder).getLocation(); } return null; }
/** * Add a shop * @param shop Shop to add * @param addToDatabase Whether the shop should also be added to the database * @param callback Callback that - if succeeded - returns the ID the shop had or was given (as {@code int}) */ public void addShop(Shop shop, boolean addToDatabase, Callback<Integer> callback) { InventoryHolder ih = shop.getInventoryHolder(); plugin.debug("Adding shop... (#" + shop.getID() + ")"); if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; Chest r = (Chest) dc.getRightSide(); Chest l = (Chest) dc.getLeftSide(); plugin.debug("Added shop as double chest. (#" + shop.getID() + ")"); shopLocation.put(r.getLocation(), shop); shopLocation.put(l.getLocation(), shop); } else { plugin.debug("Added shop as single chest. (#" + shop.getID() + ")"); shopLocation.put(shop.getLocation(), shop); } if (addToDatabase) { plugin.getShopDatabase().addShop(shop, callback); } else { if (callback != null) callback.callSyncResult(shop.getID()); } }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInventoryClick(InventoryClickEvent e) { if (!plugin.getHologramFormat().isDynamic()) return; Inventory chestInv = e.getInventory(); if (!(chestInv.getHolder() instanceof Chest || chestInv.getHolder() instanceof DoubleChest)) { return; } Location loc = null; if (chestInv.getHolder() instanceof Chest) { loc = ((Chest) chestInv.getHolder()).getLocation(); } else if (chestInv.getHolder() instanceof DoubleChest) { loc = ((DoubleChest) chestInv.getHolder()).getLocation(); } final Shop shop = plugin.getShopUtils().getShop(loc); if (shop == null) return; new BukkitRunnable() { @Override public void run() { shop.updateHologramText(); } }.runTaskLater(plugin, 1L); }
/** * Creates the hologram of the shop */ private void createHologram() { plugin.debug("Creating hologram (#" + id + ")"); InventoryHolder ih = getInventoryHolder(); if (ih == null) return; Chest[] chests = new Chest[2]; boolean doubleChest; if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; Chest r = (Chest) dc.getRightSide(); Chest l = (Chest) dc.getLeftSide(); chests[0] = r; chests[1] = l; doubleChest = true; } else { chests[0] = (Chest) ih; doubleChest = false; } String[] holoText = getHologramText(); Location holoLocation = getHologramLocation(doubleChest, chests); hologram = new Hologram(plugin, holoText, holoLocation); }
/** * Gets the block the inventory is stored in, or null if the inventory is * not stored in a block. * * @param inventory * The inventory. * @return The block, or null. */ private Block getInventoryBlockOrNull(Inventory inventory) { InventoryHolder holder = inventory.getHolder(); if (holder instanceof BlockState) { return ((BlockState) holder).getBlock(); } if (holder instanceof DoubleChest) { InventoryHolder leftHolder = ((DoubleChest) holder).getLeftSide(); if (leftHolder instanceof BlockState) { return ((BlockState) leftHolder).getBlock(); } } return null; }
public Inventory getInventory(Location loc) { if (loc.getBlock().getState() instanceof MainChest) { return ((MainChest) loc.getBlock().getState()).getInventory(); } if (loc.getBlock().getState() instanceof RelatedChest) { return ((RelatedChest) loc.getBlock().getState()).getInventory(); } if (loc.getBlock().getState() instanceof Chest) { return ((Chest) loc.getBlock().getState()).getInventory(); } if (loc.getBlock().getState() instanceof DoubleChest) { return ((DoubleChest) loc.getBlock().getState()).getInventory(); } if (loc.getBlock().getState() instanceof BrewingStand) { return ((BrewingStand) loc.getBlock().getState()).getInventory(); } if (loc.getBlock().getState() instanceof Dispenser) { return ((Dispenser) loc.getBlock().getState()).getInventory(); } if (loc.getBlock().getState() instanceof Dropper) { return ((Dropper) loc.getBlock().getState()).getInventory(); } return null; }
public void fillDoubleChest(SGArena a, DoubleChest chest) { if (a.dLooted.contains(chest)) return; a.dLooted.add(chest); for (int i = 0; i < 54; i++) { if (r.nextInt(100) < slot) { if (r.nextInt(100) < lvlup) { if (r.nextInt(100) < lvlup) { if (r.nextInt(100) < lvlup) { if (r.nextInt(100) < lvlup) { chest.getInventory().setItem(i, getRandomItemStack(1, 5)); continue; } chest.getInventory().setItem(i, getRandomItemStack(1, 4)); continue; } chest.getInventory().setItem(i, getRandomItemStack(1, 3)); continue; } chest.getInventory().setItem(i, getRandomItemStack(1, 2)); continue; } chest.getInventory().setItem(i, getRandomItemStack(1, 1)); continue; } } }
/** * Prevent placing items in death chests if configured * @param event */ @EventHandler public void onInventoryDragEvent(InventoryDragEvent event) { Inventory inventory = event.getInventory(); // if inventory is a death chest inventory if (inventoryIsDeathChest(inventory)) { // if prevent-item-placement is configured false, do nothing and return if (!plugin.getConfig().getBoolean("prevent-item-placement")) { return; } // get set of slots dragged over Set<Integer> rawSlots = event.getRawSlots(); // if single chest set max slot to 27 int maxSlot = 27; // if double chest set max slot to 54 if (inventory.getHolder() instanceof DoubleChest) { maxSlot = 54; } // iterate over dragged slots and if any are above max slot, cancel event for (int slot : rawSlots) { if (slot < maxSlot) { // if player does not have allow-place permission, cancel event if (!event.getWhoClicked().hasPermission("deathchest.allow-place")) { event.setCancelled(true); } break; } } } }
@EventHandler(priority = EventPriority.NORMAL) public void onInventoryClose(final InventoryCloseEvent event) { if (event.getViewers().size() == 1) { final Inventory inventory = event.getInventory(); final InventoryHolder holder = inventory.getHolder(); if (holder instanceof Chest || holder instanceof DoubleChest) { final BlockState chest = (BlockState)(holder instanceof Chest ? holder : ((DoubleChest)holder).getLeftSide()); final Block block = chest.getBlock(); final NLocation loc = new NLocation(block.getLocation()); final List<Sign> signs = SignUtil.getSignsForChest(block); for (final Sign sign : signs) { if (sign.getLine(0).equals(ITEMNETWORK_EMITTER)) { final String networkName = ColorUtil.stripColorCodes(sign.getLine(1)); final ItemNetwork network = this.feature.getNetworks().get(networkName.toLowerCase()); if (network != null) { final List<ItemStack> items = new ArrayList<>(); for (final ItemStack is : inventory.getContents()) { if (is != null) { items.add(is); } } if (!items.isEmpty()) { network.queue(new InventoryContent(loc, items)); inventory.clear(); this.feature.lock(loc); return; } break; } else { sign.getBlock().breakNaturally(); } } } this.feature.unlock(loc); } } }
/** * Creates a new DPChest. * * @param dropParty The DropParty instance. * @param party The party of the DPChest. * @param chest The chest. */ public DPChest(DropParty dropParty, Party party, Chest chest) { this.dropParty = dropParty; this.party = party; this.chest = chest; this.inventory = chest.getInventory(); this.isDoubleChest = inventory.getHolder() instanceof DoubleChest; }
@EventHandler public void onInventoryOpenEvent(InventoryOpenEvent event) { InventoryType t = event.getInventory().getType(); if (InventoryType.CHEST.equals(t)) { ContentCraftPlugin.logger.info("Player [" + event.getPlayer().getName() + "] accessing [" + t + "]"); // either side of the chest will provide the required meta-data DoubleChest doubleChest = (DoubleChest)event.getInventory().getHolder(); Chest chest = (Chest)doubleChest.getLeftSide(); Block block = chest.getBlock(); if (BlockMetaData.hasMetadata(block, "folderId")) { // clear inventory event.getInventory().clear(); // get the folder String folderId = (String)BlockMetaData.getMetadata(block, "folderId"); Folder folder = (Folder)CMIS.getSession().getObject(folderId); ContentCraftPlugin.logger.info("Filling chest with documents from folder [" + folderId + "]"); // add the documents to the chest int i = 0; for (CmisObject cmisObject : folder.getChildren()) { if (cmisObject instanceof Document) { Document document = (Document)cmisObject; event.getInventory().setItem(i, getBook(document)); i++; } } } } }
public static int findAdjacentMatter(Location loc) { int matterFound = 0; Block[] blocks = new Block[4]; blocks[0] = loc.clone().add(1, 0, 0).getBlock(); blocks[1] = loc.clone().add(-1, 0, 0).getBlock(); blocks[2] = loc.clone().add(0, 0, 1).getBlock(); blocks[3] = loc.clone().add(0, 0, -1).getBlock(); for (int i = 0; i < blocks.length; i++) { Block block = blocks[i]; if (block == null) { continue; } if (!(block.getState() instanceof Chest || block.getState() instanceof DoubleChest)) { continue; } Chest chest = (Chest) block.getState(); HashMap<Integer, ? extends ItemStack> hash = chest .getBlockInventory().all(Material.STONE); Iterator<? extends ItemStack> it = hash.values().iterator(); while (it.hasNext()) { ItemStack next = it.next(); if (next != null && next.hasItemMeta() && next.getItemMeta().hasDisplayName() && next.getItemMeta() .getDisplayName() .equals(Matter.getAmount(1).getItemMeta() .getDisplayName())) { matterFound += next.getAmount(); } } } return matterFound; }
@Override public DoubleChest getHolder() { return new DoubleChest(this); }
/** * Paste this block at blockLoc * @param nms * @param blockLoc */ //@SuppressWarnings("deprecation") @SuppressWarnings("deprecation") public void paste(NMSAbstraction nms, Location blockLoc, boolean usePhysics, Biome biome) { // Only paste air if it is below the sea level and in the overworld Block block = new Location(blockLoc.getWorld(), x, y, z).add(blockLoc).getBlock(); block.setBiome(biome); block.getChunk().load(); nms.setBlockSuperFast(block, typeId, data, usePhysics); if (signText != null) { if (block.getTypeId() != typeId) { block.setTypeId(typeId); } // Sign Sign sign = (Sign) block.getState(); int index = 0; for (String line : signText) { sign.setLine(index++, line); } sign.update(); } else if (banner != null) { banner.set(block); } else if (spawnerBlockType != null) { if (block.getTypeId() != typeId) { block.setTypeId(typeId); } CreatureSpawner cs = (CreatureSpawner)block.getState(); cs.setSpawnedType(spawnerBlockType); } else if (!chestContents.isEmpty()) { if (block.getTypeId() != typeId) { block.setTypeId(typeId); } // Check if this is a double chest Chest chestBlock = (Chest) block.getState(); InventoryHolder iH = chestBlock.getInventory().getHolder(); if (iH instanceof DoubleChest) { //Bukkit.getLogger().info("DEBUG: double chest"); DoubleChest doubleChest = (DoubleChest) iH; for (ItemStack chestItem: chestContents.values()) { doubleChest.getInventory().addItem(chestItem); } } else { // Single chest for (Entry<Byte, ItemStack> en : chestContents.entrySet()) { chestBlock.getInventory().setItem(en.getKey(), en.getValue()); } } } }
public void spawnBonusChest(Player player) { //Get the location of the chest and set it to a chest. Location chestLocation = new Location(player.getWorld(), player.getLocation().getX() + 1, player.getLocation().getY() - 2, player.getLocation().getZ() + 1); Location chestTwo = new Location(player.getWorld(), player.getLocation().getX() + 2, player.getLocation().getY() - 2, player.getLocation().getZ() + 1); if((chestLocation.getY() + 3) > chestLocation.getWorld().getHighestBlockAt(chestLocation).getY() || (chestLocation.getY() - 3) < chestLocation.getWorld().getHighestBlockAt(chestLocation).getY()) { int chestHeight = chestLocation.getWorld().getHighestBlockAt(chestLocation).getY(); chestLocation.setY(chestHeight); chestTwo.setY(chestHeight); } Block chestBlock = chestLocation.getBlock(); chestBlock.setType(Material.CHEST); Location airLoc = chestLocation.add(0,1,0); airLoc.getBlock().setType(Material.AIR); //Get the Chest final Chest chest = (Chest) chestBlock.getState(); //Get the inventory of this chest. final Inventory chestInv = chest.getInventory(); int i = 0; List<ItemStack> itemsToAdd = itemsToAdd(player, player.getWorld()); List<Integer> singleChestShuffle = Arrays.asList(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26); Collections.shuffle(singleChestShuffle); List<Integer> doubleChestShuffle = Arrays.asList(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53); Collections.shuffle(doubleChestShuffle); Inventory doubleChestInventory = null; if(itemsToAdd.size() > 27) { Block doubleChest = chestTwo.getBlock(); doubleChest.setType(Material.CHEST); chestTwo.add(0,1,0).getBlock().setType(Material.AIR); Chest chest1 = (Chest) doubleChest.getState(); Inventory inventory = chest1.getInventory(); DoubleChest doubleChest1 = (DoubleChest) inventory.getHolder(); doubleChestInventory = doubleChest1.getInventory(); } String chestName = getBonusChestFileName(player.getWorld()); if(chestName != null) { chest.setCustomName(ChatColor.DARK_RED + chestName); } for(ItemStack item : itemsToAdd) { if(itemsToAdd.size() <= 27) { chestInv.setItem(singleChestShuffle.get(i), item); i++; } else if(itemsToAdd.size() <= 54) { doubleChestInventory.setItem(doubleChestShuffle.get(i), item); i++; } else { Bukkit.getServer().getLogger().log(Level.SEVERE, chestName + "contains too many items. Only 54 will be added."); } } }
private void restoreWoolChest(InventoryHolder inventoryHolder, GamePlayer player, List<GamePlayer> viewers) { if (inventoryHolder == null) { return; } Inventory inventory = inventoryHolder.getInventory(); if (!player.isParticipating()) { return; } else if (inventoryHolder instanceof DoubleChest) { DoubleChest doubleChest = (DoubleChest) inventoryHolder; // restore both blocks this.restoreWoolChest(doubleChest.getLeftSide(), player, viewers); this.restoreWoolChest(doubleChest.getRightSide(), player, viewers); return; } else if (!(inventoryHolder instanceof BlockState)) { return; } Block block = ((BlockState) inventoryHolder).getBlock(); ChestImage image = this.getChestImage(block); if (image == null || !image.woolChest || image.snapshot.isEmpty()) { return; } boolean restoreWools = true; // Don't retrace wools if there are players viewing this chest. for (GamePlayer viewer : viewers) { if (viewer.isParticipating() && !viewer.equals(player)) { restoreWools = false; } } if (restoreWools) { WoolChestRestoreEvent event = new WoolChestRestoreEvent(this.game.getPlugin(), block, inventory, player, viewers); this.game.getPlugin().getEventBus().publish(event); if (!event.isCanceled()) { for (int i = 0; i < inventory.getSize(); i++) { ItemStack snapshotItem = image.snapshot.get(i); if (snapshotItem == null) { continue; } // Copy the item to not edit the snapshot! inventory.setItem(i, snapshotItem.clone()); } for (Map.Entry<Integer, ItemStack> entry : image.snapshot.entrySet()) { inventory.setItem(entry.getKey(), entry.getValue().clone()); } } } }