public void fillChest(Chest chest, int level) { ArrayList<ItemStack> unused = new ArrayList<ItemStack>(); System.out.println("Unused is null: " + (unused == null)); System.out.println("inventory is null: " + (inventory.get(level) == null)); System.out.println("contents is null: " + (inventory.get(level).getContents() == null)); Collections.addAll(unused, inventory.get(level).getContents()); Collections.shuffle(unused); int min = minMap.get(level)[0], max = minMap.get(level)[1], itemsToGive = new Random().nextInt((max - min) + 1) + min; Inventory inventory = chest.getBlockInventory(); inventory.clear(); for (int i = 1; i <= itemsToGive; i++) { int nextSlot = -1; if (unused.isEmpty()) { chest.update(true); return; } while (nextSlot == -1) { nextSlot = new Random().nextInt(inventory.getSize()); if (inventory.getItem(nextSlot) != null && inventory.getItem(nextSlot).getType() != Material.AIR) nextSlot = -1; } int item = new Random().nextInt(unused.size()); inventory.setItem(nextSlot, unused.get(item)); unused.remove(item); } chest.update(true); }
public static Inventory cloneInventory(JavaPlugin plugin, Inventory inv) { Inventory inv2 = plugin.getServer().createInventory(null, inv.getSize()); for (int i = 0; i < inv.getSize(); i++) { inv2.setItem(i, inv.getItem(i) == null ? null : inv.getItem(i).clone()); } return inv2; }
public void spawnLootChest() { chest.getBlock().setType(Material.CHEST); ItemStack randomItem = getRandomItem(); Location l = chest.getBlock().getLocation(); double y = l.getY() - 1; Location lf = new Location(l.getWorld(), l.getBlockX(), y, l.getBlockZ()); Util.spawnFirework(lf); for (int i = 0; i < 5; i++) {} Chest c = (Chest) chest.getBlock().getState(); Inventory inv = c.getBlockInventory(); Random r = new Random(); inv.setItem(r.nextInt(inv.getSize()), randomItem); if (lootItems > inv.getSize() - 2) { lootItems = inv.getSize() - 2; } for (int i = 0; i < lootItems; i++) { int slot = r.nextInt(inv.getSize()); if (isEmpty(inv, slot)) { inv.setItem(slot, randomItem); } else { i--; } } for (int i = 0; i < ingots; i++) { int slot = r.nextInt(inv.getSize()); ItemStack stack = inv.getItem(slot); if (isEmpty(inv, slot)) { inv.setItem(slot, new ItemStack(Material.IRON_INGOT)); } else if (stack.getType() == Material.IRON_INGOT) { inv.getItem(slot).setAmount(stack.getAmount() + 1); } else { i--; } } }
@Override protected void saveEditor(Inventory inventory, Player player) { for (int i = 0; i < 8; i++) { ItemStack item = inventory.getItem(i); if (item != null && item.getType() != Material.AIR) { ItemStack cost1 = null, cost2 = null; ItemStack item1 = inventory.getItem(i + 9); ItemStack item2 = inventory.getItem(i + 18); if (item1 != null && item1.getType() != Material.AIR) { cost1 = item1; if (item2 != null && item2.getType() != Material.AIR) { cost2 = item2; } } else if (item2 != null && item2.getType() != Material.AIR) { cost1 = item2; } if (cost1 != null) { Cost cost = new Cost(); cost.amount = item.getAmount(); cost.item1 = cost1; cost.item2 = cost2; ItemStack saleItem = item.clone(); saleItem.setAmount(1); ((TradingPlayerShopkeeper) shopkeeper).costs.put(saleItem, cost); } else { ItemStack saleItem = item.clone(); saleItem.setAmount(1); ((TradingPlayerShopkeeper) shopkeeper).costs.remove(saleItem); } } } ((TradingPlayerShopkeeper) shopkeeper).clickedItem = null; }
@EventHandler(ignoreCancelled = true) public void onPrefsMenuClick(InventoryClickEvent event) { Inventory inv = event.getInventory(); String name = inv.getTitle(); if (name.equals("§4TARDIS Key Prefs Menu")) { Player p = (Player) event.getWhoClicked(); int slot = event.getRawSlot(); if (slot >= 0 && slot < 27) { switch (slot) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 10: case 12: case 14: case 16: event.setCancelled(true); // set display name of key in slot 18 ItemStack key = inv.getItem(18); if (key == null || !key.getType().equals(Material.GOLD_NUGGET) || !key.hasItemMeta()) { return; } // get Display name of selected key ItemStack choice = inv.getItem(slot); ItemMeta choice_im = choice.getItemMeta(); String choice_name = choice_im.getDisplayName(); ItemMeta sonic_im = key.getItemMeta(); sonic_im.setDisplayName(choice_name); key.setItemMeta(sonic_im); break; case 18: break; case 26: // close event.setCancelled(true); close(p); break; default: event.setCancelled(true); break; } } else { ClickType click = event.getClick(); if (click.equals(ClickType.SHIFT_RIGHT) || click.equals(ClickType.SHIFT_LEFT)) { event.setCancelled(true); } } } }
private void updatePlayerSlots() { if (dirtySlots != true) return; Debug.debug("Updating Player Slots"); Inventory inventory = player.getInventory(); for (int i = 0; i < 36; i++) { if (!playerSlots[i].getItem().equals(inventory.getItem(i))) { playerSlots[i].setItem(inventory.getItem(i)); } } dirtySlots = false; }
public static boolean removeItem(Inventory inv, Material mat, int data, int qty) { int remd = 0; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < inv.getSize(); i++) { ItemStack s = inv.getItem(i); if (s != null) { if (remd < qty) { if ((s.getType().equals(mat)) && ((data == -1) || (data == s.getData().getData()))) { int take = Math.min(s.getAmount(), qty - remd); map.put(Integer.valueOf(i), Integer.valueOf(take)); remd += take; if (take == s.getAmount()) { inv.setItem(i, null); } else { s.setAmount(s.getAmount() - take); } } } } } if (remd != qty) { return false; } return true; }
public static String InventoryToString(Inventory invInventory) { String serialization = invInventory.getSize() + ";"; for (int i = 0; i < invInventory.getSize(); i++) { ItemStack is = invInventory.getItem(i); if (is != null) { String serializedItemStack = new String(); String isType = String.valueOf(is.getType().getId()); serializedItemStack += "t@" + isType; if (is.getDurability() != 0) { String isDurability = String.valueOf(is.getDurability()); serializedItemStack += ":d@" + isDurability; } if (is.getAmount() != 1) { String isAmount = String.valueOf(is.getAmount()); serializedItemStack += ":a@" + isAmount; } Map<Enchantment, Integer> isEnch = is.getEnchantments(); if (isEnch.size() > 0) { for (Entry<Enchantment, Integer> ench : isEnch.entrySet()) { serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue(); } } serialization += i + "#" + serializedItemStack + ";"; } } return serialization; }
public boolean upgradePearl(Inventory inv, PrisonPearl pp) { final String prisoner = pp.getImprisonedName(); ItemStack is = new ItemStack(Material.ENDER_PEARL, 1, pp.getID()); int pearlslot = inv.first(is); if (pearlslot < 0) { // If the pearl has been converted, first won't return it here // as the metadata doesn't match. return false; } ItemStack existing_is = inv.getItem(pearlslot); if (existing_is != null) { ItemMeta existing_meta = existing_is.getItemMeta(); if (existing_meta != null) { String existing_name = existing_meta.getDisplayName(); if (existing_name != null && existing_name.compareTo(prisoner) == 0) { return true; } } } ItemMeta im = is.getItemMeta(); // Rename pearl to that of imprisoned player im.setDisplayName(prisoner); List<String> lore = new ArrayList<String>(); lore.add(prisoner + " is held within this pearl"); // Given enchantment effect // Durability used because it doesn't affect pearl behaviour im.addEnchant(Enchantment.DURABILITY, 1, true); im.setLore(lore); is.setItemMeta(im); is.removeEnchantment(Enchantment.DURABILITY); inv.clear(pearlslot); inv.setItem(pearlslot, is); return true; }
private void setLore(Inventory inv, int slot, String str) { List<String> lore = (str != null) ? Arrays.asList(str) : null; ItemStack is = inv.getItem(slot); ItemMeta im = is.getItemMeta(); im.setLore(lore); is.setItemMeta(im); }
private String getDisplay(Inventory inv, int slot) { ItemStack is = inv.getItem(slot); if (is != null) { ItemMeta im = is.getItemMeta(); return im.getDisplayName(); } else { return ""; } }
@SuppressWarnings("deprecation") public void refreshBank(Player player) { Bank bank = banks.get(player.getName()); Inventory inv = bank.getInv(); ItemStack goldtotal = inv.getItem(inv.getSize() - 2); ItemStack silvertotal = inv.getItem(inv.getSize() - 1); setItemText( goldtotal, ChatColor.GOLD + "" + getGoldBalance(player) + ChatColor.BOLD + " Gold Coin(s)", goldtotal.getItemMeta().getLore()); setItemText( silvertotal, ChatColor.WHITE + "" + getSilverBalance(player) + ChatColor.BOLD + " Silver Coin(s)", silvertotal.getItemMeta().getLore()); player.updateInventory(); }
@Override public Optional<ItemStack> getItem(Player player, Component component, int x, int y) { Position position = this.components.get(component); int slot = Position.toSlot(this, position.x + x, position.y + y); Inventory inv = getInventory(player); if (slot < 0 || slot > inv.getSize()) { return Optional.empty(); } return Optional.ofNullable(inv.getItem(slot)).filter(stack -> stack.getType() != Material.AIR); }
private static boolean isEmpty(Inventory inv, int slot) { ItemStack stack = inv.getItem(slot); if (stack == null) { return true; } if (stack.getType() == Material.AIR) { return true; } return false; }
private void saveEnderInventory(OfflinePlayer op, String world, Inventory i) { if (!Config.separateInv || !Config.separateEnder) return; World w = this.plugin.getServer().getWorld(world); String group = getWorldGroup(w); if (group == null) return; PlayerConfiguration pcm = PlayerConfigurationManager.getConfiguration(op); for (int slot = 0; slot < i.getSize(); slot++) { pcm.set("inventory." + group + ".ender.slot." + slot, i.getItem(slot)); } pcm.set("inventory." + group + ".ender.size", i.getSize()); }
private void saveEnderInventory(Player p, Inventory i) { if (!Config.separateInv || !Config.separateEnder) return; World w = p.getWorld(); String group = getWorldGroup(w); if (group == null) return; PlayerConfiguration pcm = PlayerConfigurationManager.getConfiguration(p); for (int slot = 0; slot < i.getSize(); slot++) { pcm.set("inventory." + group + ".ender.slot." + slot, i.getItem(slot)); } pcm.set("inventory." + group + ".ender.size", i.getSize()); }
private static boolean isEmpty(Inventory inv) { for (int i = 0; i < inv.getSize(); i++) { ItemStack item = inv.getItem(i); if (item == null) continue; if (item.equals(backShop)) continue; return false; } return true; }
@EventHandler public void onInventoryClick(InventoryClickEvent event) { Inventory inventory = event.getInventory(); if (inventory.getHolder() instanceof dNPC) { dNPC npc = (dNPC) inventory.getHolder(); npc.getInventory().setContents(inventory.getContents()); Equipment equipment = npc.getEquipmentTrait(); for (int i = 0; i < 5; i++) { equipment.set(i, inventory.getItem(i)); } } }
@EventHandler(priority = EventPriority.LOWEST) public void onPlayerJoin(PlayerJoinEvent e) { PermissionUser player = PermissionsEx.getUser(e.getPlayer()); e.setJoinMessage(CoreUtils.colorize(CoreUtils.getPlayerDisplayName(player) + "&7 has joined.")); if (!e.getPlayer().hasPlayedBefore()) { e.getPlayer().teleport(CoreUtils.getSpawn()); e.getPlayer().getInventory().clear(); for (int i = 0; i != hubinv.getSize(); i++) e.getPlayer().getInventory().setItem(i, hubinv.getItem(i)); e.getPlayer().getInventory().setChestplate(elytra); } }
@EventHandler(ignoreCancelled = true) public void onKeyMenuClose(InventoryCloseEvent event) { Inventory inv = event.getInventory(); String title = inv.getTitle(); if (!title.equals("§4TARDIS Key Prefs Menu")) { return; } ItemStack key = inv.getItem(18); if (key != null) { Player p = (Player) event.getPlayer(); Location loc = p.getLocation(); loc.getWorld().dropItemNaturally(loc, key); inv.setItem(18, new ItemStack(Material.AIR)); } }
private void blockItemUse(Player player) { Inventory inventory = player.getInventory(); ItemStack item = player.getItemInHand(); // int itemlocation = inventory. boolean hasreplaced = false; inventory.remove(item); for (int i = 0; i < inventory.getSize(); i++) { if (inventory.getItem(i) == null) { inventory.setItem(i, item); hasreplaced = true; } } if (!hasreplaced) player.getWorld().dropItem(player.getLocation(), item); }
public void setBankSize(Player player, int size) { bank = banks.get(player.getName()); Inventory inv = bank.getInv(); ItemStack gt = inv.getItem(inv.getSize() - 2); ItemStack st = inv.getItem(inv.getSize() - 1); inv.remove(gt); inv.remove(st); bank.setBankSize(size); Inventory in = Bukkit.createInventory(player, size, player.getName() + " - Bank Chest"); in.setContents(inv.getContents()); bank.setInv(in); in.setItem(in.getSize() - 1, st); in.setItem(in.getSize() - 2, gt); sql.updateInv(player.getName(), size, in); }
public void saveInventory(OfflinePlayer op, String world, Inventory i) { if (!Config.separateInv) return; String group = this.getWorldGroup(this.plugin.getServer().getWorld(world)); if (group == null) return; PlayerConfiguration pcm = PlayerConfigurationManager.getConfiguration(op); for (int slot = 0; slot < i.getSize(); slot++) { pcm.set("inventory." + group + ".slot." + slot, i.getItem(slot)); } if (i instanceof PlayerInventory) { PlayerInventory pi = (PlayerInventory) i; pcm.set("inventory." + group + ".slot.helm", pi.getHelmet()); pcm.set("inventory." + group + ".slot.chestplate", pi.getChestplate()); pcm.set("inventory." + group + ".slot.leggings", pi.getLeggings()); pcm.set("inventory." + group + ".slot.boots", pi.getBoots()); } pcm.set("inventory." + group + ".size", i.getSize()); }
@Override protected void saveEditor(Inventory inventory, Player player) { for (int column = 0; column < 8; column++) { ItemStack item = inventory.getItem(column); if (item != null && item.getType() == Material.WRITTEN_BOOK) { String title = getTitleOfBook(item); if (title != null) { int price = this.getPriceFromColumn(inventory, column); if (price > 0) { ((WrittenBookPlayerShopkeeper) shopkeeper).offers.put(title, price); } else { ((WrittenBookPlayerShopkeeper) shopkeeper).offers.remove(title); } } } } }
public int removeItemInInventory(Inventory inv, String istr) { String itemstr = istr; int left = 1; if (left <= 0) return -1; int id = -1; int data = -1; String name = ""; if (itemstr.contains("$")) { name = itemstr.substring(0, itemstr.indexOf("$")); itemstr = itemstr.substring(name.length() + 1); } String[] si = itemstr.split("\\*"); if (si.length == 0) return left; if ((si.length == 2) && si[1].matches("[1-9]+[0-9]*")) left = Integer.parseInt(si[1]); String ti[] = si[0].split(":"); if (ti.length > 0) { if (ti[0].matches("[0-9]*")) id = Integer.parseInt(ti[0]); else id = Material.getMaterial(ti[0]).getId(); if ((ti.length == 2) && (ti[1]).matches("[0-9]*")) data = Integer.parseInt(ti[1]); } if (id <= 0) return left; for (int i = 0; i < inv.getContents().length; i++) { ItemStack slot = inv.getItem(i); if (slot == null) continue; if (!compareItemName(slot, name)) continue; if (id != slot.getTypeId()) continue; if ((data > 0) && (data != slot.getDurability())) continue; int slotamount = slot.getAmount(); if (slotamount == 0) continue; if (slotamount <= left) { left = left - slotamount; inv.setItem(i, null); } else { slot.setAmount(slotamount - left); left = 0; } if (left == 0) return 0; } return left; }
@EventHandler public void onCloseInventory(InventoryCloseEvent ev) { if (ev.getInventory().getType() == InventoryType.HOPPER && ev.getInventory().getTitle().equalsIgnoreCase("Network Creator")) { Inventory inv = ev.getInventory(); if (inv.getItem(0) != null) ev.getPlayer().getWorld().dropItemNaturally(ev.getPlayer().getLocation(), inv.getItem(0)); if (inv.getItem(1) != null) ev.getPlayer().getWorld().dropItemNaturally(ev.getPlayer().getLocation(), inv.getItem(1)); if (inv.getItem(3) != null) ev.getPlayer().getWorld().dropItemNaturally(ev.getPlayer().getLocation(), inv.getItem(3)); } }
// Drag events by nature only matters when they affect the top inventory. // What you are holding in the cursor is already yours. // If you drag it into your own inventory you are not really taking anything. // If you drag into the top inventory however, you may both give and take. // You "take" by dragging over an existing item (since we don't do any math). protected static List<Entry<ItemStack, Integer>> getChangesDrag(InventoryDragEvent event) { // Create List<Entry<ItemStack, Integer>> ret = new MassiveList<>(); // Fill final Inventory inventory = event.getInventory(); for (Entry<Integer, ItemStack> entry : event.getNewItems().entrySet()) { int rawSlot = entry.getKey(); if (InventoryUtil.isBottomInventory(rawSlot, inventory)) continue; ItemStack take = inventory.getItem(rawSlot); if (isSomething(take)) ret.add(new SimpleEntry<ItemStack, Integer>(take, +take.getAmount())); ItemStack give = entry.getValue(); if (isSomething(give)) ret.add(new SimpleEntry<ItemStack, Integer>(give, -give.getAmount())); } // Return return ret; }
@Override public boolean onClick(InventoryClickEvent event) { if (event.getInventory() == null || event.getInventory().getTitle() == null || !stripFormatting(event.getInventory().getTitle()) .contains(stripFormatting(getTitle()))) { return false; } if (event.getSlotType() != InventoryType.SlotType.CONTAINER) { return true; } Player player = (Player) event.getWhoClicked(); Inventory menu = event.getInventory(); ItemStack returnItem = menu.getItem(getIndex(5, 0)); String configName = returnItem.getItemMeta().getLore().get(0); String path = returnItem.getItemMeta().getLore().get(1); int page = getPage(returnItem.getItemMeta().getLore().get(2)); int slot = event.getSlot(); int row = slot / 9; int col = slot % 9; ItemStack clickedItem = event.getCurrentItem(); if (slot >= getIndex(3, 0) && slot <= getIndex(3, 8)) { // increment buttons YmlConfiguration config = FileUtil.getYmlConfiguration(configName); int value = config.getInt(path, 0); int increment = getDisplayNameAsInt(clickedItem); if (event.getClick() == ClickType.LEFT) { value += increment; } else if (event.getClick() == ClickType.RIGHT) { value = increment; } config.set(path, value); config.set("dirty", true); } if (slot != getIndex(5, 0)) { player.openInventory(createEditMenu(configName, path, page)); } else { player.openInventory(parent.createEditMenu(configName, path, page)); } return true; }
private void saveInventory(Player p, Inventory i, World w) { if (!Config.separateInv) return; String group = getWorldGroup(w); if (group == null) return; PlayerConfiguration pcm = PlayerConfigurationManager.getConfiguration(p); for (int slot = 0; slot < i.getSize(); slot++) { pcm.set("inventory." + group + ".slot." + slot, i.getItem(slot)); } if (i instanceof PlayerInventory) { PlayerInventory pi = (PlayerInventory) i; pcm.set("inventory." + group + ".slot.helm", pi.getHelmet()); pcm.set("inventory." + group + ".slot.chestplate", pi.getChestplate()); pcm.set("inventory." + group + ".slot.leggings", pi.getLeggings()); pcm.set("inventory." + group + ".slot.boots", pi.getBoots()); } pcm.set("inventory." + group + ".size", i.getSize()); if (Config.separateXP) { pcm.set("inventory." + group + ".xp", p.getExp()); pcm.set("inventory." + group + ".xplevel", p.getLevel()); } }
@Override public byte[] getBytes(Inventory inv) { // Blank NBT tag net.minecraft.server.v1_5_R3.NBTTagCompound c = new net.minecraft.server.v1_5_R3.NBTTagCompound(); // Blank NBT list net.minecraft.server.v1_5_R3.NBTTagList list = new net.minecraft.server.v1_5_R3.NBTTagList(); // Iterate over the inventory contents for (int index = 0; index < inv.getContents().length; index++) { // Convert it to a craftitem stack org.bukkit.craftbukkit.v1_5_R3.inventory.CraftItemStack cis = (org.bukkit.craftbukkit.v1_5_R3.inventory.CraftItemStack) inv.getItem(index); if (cis != null) { // If cis == null, no item is there, ignore it. // Convert it to a NMS itemstack net.minecraft.server.v1_5_R3.ItemStack is = org.bukkit.craftbukkit.v1_5_R3.inventory.CraftItemStack.asNMSCopy(cis); // Save the NMS itemstack to a new NBT tag net.minecraft.server.v1_5_R3.NBTTagCompound itemCompound = new net.minecraft.server.v1_5_R3.NBTTagCompound(); itemCompound = is.save(itemCompound); // Set the position of the item in the chest under "index" itemCompound.set("index", new net.minecraft.server.v1_5_R3.NBTTagInt("index", index)); // Add the item to the NBT list of items list.add(itemCompound); } } // Put the list in the blank tag c.set("inventory", list); // Convert the NBT tag to a byte[] byte[] bytes = net.minecraft.server.v1_5_R3.NBTCompressedStreamTools.a(c); // Convert & escape the byte[] to a string return bytes; }