public static void onPlayerDeath(final PlayerDeathEvent event) { final Player player = event.getEntity(); final Inventory enderChest = player.getEnderChest(); for (final ItemStack item : enderChest.getContents()) ItemUtil.dropItemNaturally(player, item); enderChest.clear(); }
public boolean execute(CommandSender sender, String[] args) { if (sender instanceof Player) { Player player = (Player) sender; if (args.length == 0) { if (player.hasPermission("kessentials.enderchest")) { Inventory inv = player.getEnderChest(); player.openInventory(inv); player.sendMessage(GREEN + "You are now viewing your enderchest."); } else { noPermissionsMessage(player); } } else if (args.length == 1) { if (player.hasPermission("kessentials.enderchest")) { Player tplayer = Bukkit.getServer().getPlayer(args[0]); if (tplayer != null) { Inventory inv = tplayer.getEnderChest(); player.openInventory(inv); player.sendMessage( GREEN + "You are now viewing " + YELLOW + tplayer.getName() + GREEN + "'s enderchest."); } else { player.sendMessage(RED + args[0] + " is not online."); } } else { noPermissionsMessage(player); } } else { incorrectUsage(player, "/enderchest [player]"); } } else { consoleError(); } return false; }
@Override public Inventory importInventory( ChestOwner chestOwner, WorldGroup worldGroup, BetterEnderChest plugin) throws IOException { // Cannot import vanilla chests if (chestOwner.isSpecialChest()) { return null; } Player player = chestOwner.getPlayer(); Inventory betterEnderInventory; if (player == null) { // Offline, load from file File playerDirectory = new File(Bukkit.getWorlds().get(0).getWorldFolder().getAbsolutePath() + "/playerdata"); File playerFile = new File(playerDirectory.getAbsolutePath() + "/" + chestOwner.getSaveFileName() + ".dat"); if (!playerFile.exists()) { return null; } // Load it from the file (mainworld/playerdata/playername.dat) betterEnderInventory = plugin .getNMSHandlers() .getSelectedRegistration() .loadNBTInventoryFromFile(playerFile, chestOwner, worldGroup, "EnderItems"); if (betterEnderInventory == null) { // Cannot load the inventory from that file, most likely because // it is empty return null; } } else { // Online, load now Inventory vanillaInventory = player.getEnderChest(); int inventoryRows = plugin.getEmptyInventoryProvider().getInventoryRows(chestOwner, vanillaInventory); betterEnderInventory = plugin .getEmptyInventoryProvider() .loadEmptyInventory( chestOwner, worldGroup, inventoryRows, plugin.getChestSizes().getDisabledSlots(player)); // Copy all items ListIterator<ItemStack> copyIterator = vanillaInventory.iterator(); while (copyIterator.hasNext()) { int slot = copyIterator.nextIndex(); ItemStack stack = copyIterator.next(); if (slot < betterEnderInventory.getSize()) { betterEnderInventory.setItem(slot, stack); } } } // Check if the inventory is empty if (BetterEnderUtils.isInventoryEmpty(betterEnderInventory)) { // Empty inventory, return null return null; } else { // Return the inventory return betterEnderInventory; } }
@Override public boolean activateContainer(Player p, boolean silentchest, org.bukkit.block.Block b) { EntityPlayer player = ((CraftPlayer) p).getHandle(); // Silent ender chest is pretty much API-only if (silentchest && b.getType() == Material.ENDER_CHEST) { p.openInventory(p.getEnderChest()); player.b(StatisticList.X); return true; } final World world = player.world; final BlockPosition blockPosition = new BlockPosition(b.getX(), b.getY(), b.getZ()); Object tile = world.getTileEntity(blockPosition); if (tile == null) { return false; } if (tile instanceof TileEntityEnderChest) { // Anychest ender chest. See net.minecraft.server.BlockEnderChest InventoryEnderChest enderChest = player.getEnderChest(); enderChest.a((TileEntityEnderChest) tile); player.openContainer(enderChest); player.b(StatisticList.X); return true; } if (!(tile instanceof IInventory)) { return false; } Block block = world.getType(blockPosition).getBlock(); Container container = null; if (block instanceof BlockChest) { BlockChest blockChest = (BlockChest) block; for (EnumDirection localEnumDirection : EnumDirection.EnumDirectionLimit.HORIZONTAL) { BlockPosition localBlockPosition = blockPosition.shift(localEnumDirection); Block localBlock = world.getType(localBlockPosition).getBlock(); if (localBlock != block) { continue; } TileEntity localTileEntity = world.getTileEntity(localBlockPosition); if (!(localTileEntity instanceof TileEntityChest)) { continue; } if ((localEnumDirection == EnumDirection.WEST) || (localEnumDirection == EnumDirection.NORTH)) { tile = new InventoryLargeChest( "container.chestDouble", (TileEntityChest) localTileEntity, (ITileInventory) tile); } else { tile = new InventoryLargeChest( "container.chestDouble", (ITileInventory) tile, (TileEntityChest) localTileEntity); } break; } if (blockChest.g == Type.BASIC) { player.b(StatisticList.ac); } else if (blockChest.g == Type.TRAP) { player.b(StatisticList.W); } if (silentchest) { container = new SilentContainerChest(player.inventory, ((IInventory) tile), player); } } if (block instanceof BlockShulkerBox) { player.b(StatisticList.ae); if (silentchest && tile instanceof TileEntityShulkerBox) { // Set value to current + 1. Ensures consistency later when resetting. SilentContainerShulkerBox.setOpenValue( (TileEntityShulkerBox) tile, SilentContainerShulkerBox.getOpenValue((TileEntityShulkerBox) tile) + 1); container = new SilentContainerShulkerBox(player.inventory, (IInventory) tile, player); } } boolean returnValue = false; final IInventory iInventory = (IInventory) tile; if (!silentchest || container == null) { player.openContainer(iInventory); returnValue = true; } else { try { int windowId = player.nextContainerCounter(); player.playerConnection.sendPacket( new PacketPlayOutOpenWindow( windowId, iInventory.getName(), iInventory.getScoreboardDisplayName(), iInventory.getSize())); player.activeContainer = container; player.activeContainer.windowId = windowId; player.activeContainer.addSlotListener(player); returnValue = true; if (tile instanceof TileEntityShulkerBox) { new BukkitRunnable() { @Override public void run() { // TODO hacky Object tile = world.getTileEntity(blockPosition); if (!(tile instanceof TileEntityShulkerBox)) { return; } TileEntityShulkerBox box = (TileEntityShulkerBox) tile; // Reset back - we added 1, and calling TileEntityShulkerBox#startOpen adds 1 more. SilentContainerShulkerBox.setOpenValue( box, SilentContainerShulkerBox.getOpenValue((TileEntityShulkerBox) tile) - 2); } }.runTaskLater(Bukkit.getPluginManager().getPlugin("OpenInv"), 2); } } catch (Exception e) { e.printStackTrace(); p.sendMessage(ChatColor.RED + "Error while sending silent chest."); } } return returnValue; }