@Override public boolean finalizeTransaction(TransactionInfo info) { String stock = info.getStock().name().toLowerCase(); Player player = info.getPlayerParticipant(); int amount = info.getAmount(); boolean result = false; int endAmount = amount * this.amount; ItemStack clone = is.clone(); clone.setAmount(endAmount); if (stock == "sell") { ItemStack[] contents = player.getInventory().getContents(); for (int i = 0; i < contents.length && endAmount > 0; ++i) { ItemStack nItem = contents[i]; if (nItem != null && nItem.isSimilar(clone)) { int diff = endAmount - nItem.getAmount(); if (diff < 0) nItem.setAmount(-diff); else player.getInventory().setItem(i, null); endAmount = diff; } } result = true; } else if (stock == "buy") { player.getInventory().addItem(clone); result = true; } return result; }
/** * Returns the ReinforcementType for a given ItemStack. * * @param The ItemStack that a player may be holding. * @return Returns null if no ReinforcementType is found. * @return Returns the ReinforcementType if found. */ public static ReinforcementType getReinforcementType(ItemStack stack) { for (ItemStack storedStack : types.keySet()) if (storedStack.isSimilar(stack)) { return types.get(storedStack); } return null; }
private Collection<ItemStack> getStackOnRecipeMatch(final Recipe recipe, final ItemStack stack) { final Collection<ItemStack> inputList; if (recipe instanceof ShapedRecipe) { ShapedRecipe sRecipe = (ShapedRecipe) recipe; inputList = sRecipe.getIngredientMap().values(); } else if (recipe instanceof ShapelessRecipe) { ShapelessRecipe slRecipe = (ShapelessRecipe) recipe; inputList = slRecipe.getIngredientList(); } else { return null; } boolean match = true; Iterator<ItemStack> iter = inputList.iterator(); while (iter.hasNext()) { ItemStack inputSlot = iter.next(); if (inputSlot == null) { iter.remove(); continue; } if (inputSlot.getDurability() == Short.MAX_VALUE) { inputSlot.setDurability((short) 0); } if (!inputSlot.isSimilar(stack)) { match = false; } } if (match) { return inputList; } return null; }
private int findSpace( ItemStack item, Map<Integer, StackWithAmount> lookaside, boolean findEmpty) { // 1.9.4 and later extend living entity slots beyond 36, but some of these are read-only. // We could use .getStorageContents(), but this breaks 1.8.8 compatibility int length = (inventory.getHolder() instanceof HumanEntity) ? 36 : inventory.getSize(); ItemStack[] stacks = Arrays.copyOfRange(inventory.getContents(), 0, length); if (item == null) { return -1; } for (int i = 0; i < stacks.length; i++) { boolean contains = lookaside.containsKey(i); if (findEmpty && !contains) { return i; } else if (!findEmpty && contains) { StackWithAmount compareWith = lookaside.get(i); if (compareWith != null) { ItemStack compareWithStack = compareWith.getStack(); if (compareWith.getAmount() < compareWithStack.getMaxStackSize() && compareWithStack.isSimilar(item)) { return i; } } } } return -1; }
public static List<ItemStack> getIngredients(Recipe recipe) { List<ItemStack> ingredients = new ArrayList<ItemStack>(); if ((recipe instanceof ShapedRecipe)) { ShapedRecipe sr = (ShapedRecipe) recipe; String[] shape = sr.getShape(); for (String row : shape) { for (int i = 0; i < row.length(); i++) { ItemStack stack = (ItemStack) sr.getIngredientMap().get(Character.valueOf(row.charAt(i))); for (ItemStack ing : ingredients) { int mss = ing.getType().getMaxStackSize(); if ((ing.isSimilar(stack)) && (ing.getAmount() < mss)) { int canAdd = mss - ing.getAmount(); int add = Math.min(canAdd, stack.getAmount()); ing.setAmount(ing.getAmount() + add); int remaining = stack.getAmount() - add; if (remaining >= 1) { stack.setAmount(remaining); } else { stack = null; break; } } } if ((stack != null) && (stack.getAmount() > 0)) ingredients.add(stack); } } } else if ((recipe instanceof ShapelessRecipe)) { for (ItemStack i : ((ShapelessRecipe) recipe).getIngredientList()) { for (ItemStack ing : ingredients) { int mss = ing.getType().getMaxStackSize(); if ((ing.isSimilar(i)) && (ing.getAmount() < mss)) { int canAdd = mss - ing.getAmount(); ing.setAmount(ing.getAmount() + Math.min(canAdd, i.getAmount())); int remaining = i.getAmount() - Math.min(canAdd, i.getAmount()); if (remaining < 1) break; i.setAmount(remaining); } } if (i.getAmount() > 0) { ingredients.add(i); } } } return ingredients; }
public boolean isMedicalIngredient(ItemStack ItemStack) { for (ItemStack Item : this.MedicalIngredientList.values()) { if (ItemStack.isSimilar(Item)) { return true; } } return false; }
/** * Counts how many of an item stack an inventory contains * * @param template the template to use, amount is ignored * @param inventory the inventory to check * @return the total number of items this inventory contains */ public static int getCount(ItemStack template, Inventory inventory) { if (inventory == null) throw new IllegalArgumentException(); int count = 0; for (ItemStack stack : inventory.getContents()) { if (stack != null && template.isSimilar(stack)) count += stack.getAmount(); } return count; }
private static ItemStack findSimilarItemStack(Collection<ItemStack> items, ItemStack item) { for (ItemStack citem : items) { if (item.isSimilar(citem)) { return citem; } } return null; }
public static int countSimilar(Inventory inventory, ItemStack itemStack) { int ret = 0; for (ItemStack item : inventory.getContents()) { if (item == null) continue; if (!item.isSimilar(itemStack)) continue; ret += item.getAmount(); } return ret; }
public MedicalIngredient getMedicalIngredient(ItemStack ItemStack) { for (Entry<MedicalIngredient, ItemStack> Pair : this.MedicalIngredientList.entrySet()) { if (Pair.getValue().hasItemMeta()) { if (ItemStack.isSimilar(Pair.getValue())) { return Pair.getKey(); } } } return null; }
/** * Retrieve how many of the given item resides within the inventory. * * @param inv inventory to check * @param item item to search for within the inventory * @return amount of the searched items in the given inventory. */ public static int getCount(Inventory inv, ItemStack item) { Map<Integer, ? extends ItemStack> matchingItems = inv.all(item); int count = 0; for (Map.Entry<Integer, ? extends ItemStack> itemEntry : matchingItems.entrySet()) { ItemStack invItem = itemEntry.getValue(); if (!item.isSimilar(invItem)) { continue; } count += invItem.getAmount(); } return count; }
private boolean condenseStack( final User user, final ItemStack stack, final boolean validateReverse) throws ChargeException, MaxMoneyException { final SimpleRecipe condenseType = getCondenseType(stack); if (condenseType != null) { final ItemStack input = condenseType.getInput(); final ItemStack result = condenseType.getResult(); if (validateReverse) { boolean pass = false; for (Recipe revRecipe : ess.getServer().getRecipesFor(input)) { if (getStackOnRecipeMatch(revRecipe, result) != null) { pass = true; break; } } if (!pass) { return false; } } int amount = 0; for (final ItemStack contents : user.getBase().getInventory().getContents()) { if (contents != null && contents.isSimilar(stack)) { amount += contents.getAmount(); } } int output = ((amount / input.getAmount()) * result.getAmount()); amount -= amount % input.getAmount(); if (amount > 0) { input.setAmount(amount); result.setAmount(output); final Trade remove = new Trade(input, ess); final Trade add = new Trade(result, ess); remove.charge(user); add.pay(user, OverflowType.DROP); return true; } } return false; }
@EventHandler public void onClickInventory(InventoryClickEvent ev) { if (ev.getInventory().getType() == InventoryType.HOPPER && ev.getInventory().getTitle().equalsIgnoreCase("Network Creator")) { if (ev.getCurrentItem() == null) { return; } if (ev.isShiftClick()) { ev.setCancelled(true); return; } if ((ev.getSlot() == 0 || ev.getSlot() == 1) && !CardNetworkListener.isModifier(ev.getCursor()) && ev.getCursor().getType() != Material.AIR) { ev.setCancelled(true); return; } if (ev.getSlot() == 3 && !pl.isCardNetwork(ev.getCursor()) && ev.getCursor().getType() != Material.AIR) { ev.setCancelled(true); return; } Player p = (Player) ev.getWhoClicked(); Inventory inv = ev.getInventory(); if (ev.getCurrentItem().getType() == Material.ENDER_PORTAL_FRAME && ev.getCurrentItem().hasItemMeta() && ev.getCurrentItem().getItemMeta().hasDisplayName() && ev.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§rDéassablage")) { ev.setCancelled(true); List<String> lore = ev.getCurrentItem().getItemMeta().getLore(); String[] cs = lore.get(0).split("/"); Block b = new Location( ev.getWhoClicked().getWorld(), Integer.parseInt(cs[0]), Integer.parseInt(cs[1]), Integer.parseInt(cs[2])) .getBlock(); b.setType(Material.AIR); pl.bs.remove(b); b.getWorld().dropItemNaturally(b.getLocation(), pl.nc); p.closeInventory(); return; } if (ev.getSlot() == 2 && ev.getCurrentItem().getType() == Material.STAINED_GLASS_PANE && ev.getCurrentItem().getDurability() == 15 && ev.getCurrentItem().hasItemMeta() && ev.getCurrentItem().getItemMeta().hasDisplayName() && ev.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§r--->")) { ev.setCancelled(true); if (inv.getItem(0) == null && inv.getItem(1) == null && inv.getItem(3) == null) return; if (CardNetworkListener.whatNetwork(inv.getItem(3)) != null) return; ItemStack item; ItemStack m1 = inv.getItem(0); ItemStack m2 = inv.getItem(1); if (m1 != null || m2 != null) { item = inv.getItem(3); ItemMeta im = item.getItemMeta(); List<String> lore = im.getLore(); lore.add(" "); lore.add(ChatColor.GRAY + "Modifier :"); if (m1 != null) { lore.add(ChatColor.GRAY + "- " + CardNetworkListener.whatModifier(m1)); inv.setItem(0, new ItemStack(Material.AIR)); } if (m2 != null && !m2.isSimilar(m1)) { lore.add(ChatColor.GRAY + "- " + CardNetworkListener.whatModifier(m2)); inv.setItem(1, new ItemStack(Material.AIR)); } im.setLore(lore); item.setItemMeta(im); inv.setItem(3, item); } p.playSound(p.getLocation(), Sound.WOOD_CLICK, 1, 1); Inventory icn = pl.getServer().createInventory(null, InventoryType.HOPPER, "NC"); icn.setContents(inv.getContents()); inv.clear(); new ChatInventory(pl, p, null, ChatEtat.CREATE_NETWORK, icn); p.closeInventory(); p.sendMessage(ChatColor.GRAY + "Entré le nom du réseau"); return; } } }
@Override public boolean execute(CommandSender sender, String[] args, Location location) { ArrayList<Inventory> inventoryList = new ArrayList<Inventory>(); if ((args[0].startsWith("@l[") && args[0].endsWith("]")) || inventorySubs.containsKey(args[0])) { Location invLocation = LogiBlocksMain.parseLocation(args[0], location); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { for (int z = -1; z <= 1; z++) { Block testBlock = invLocation.clone().add(x, y, z).getBlock(); if (testBlock.getState() instanceof InventoryHolder) { inventoryList.add(((InventoryHolder) testBlock.getState()).getInventory()); } } } } } else { Player player = Bukkit.getPlayer(args[0]); if (player == null) { return false; } inventoryList.add(player.getInventory()); } int subIndex = inventorySubs.containsKey(args[0]) ? 0 : 1; if (!inventorySubs.containsKey(args[subIndex])) { return false; } if (inventorySubs.get(args[subIndex]) + subIndex + 1 > args.length) { return false; } for (Inventory inventory : inventoryList) { switch (args[subIndex]) { case "add": for (int i = subIndex + 1; i < args.length; i++) { ItemStack item1 = LogiBlocksMain.parseItemStack(args[i]); if (item1 == null) { continue; } inventory.addItem(item1); } break; case "remove": for (int i = subIndex + 1; i < args.length; i++) { ItemStack removeItem = LogiBlocksMain.parseItemStack(args[i]); if (removeItem == null) { continue; } for (ItemStack targetItem : inventory.getContents()) { if (targetItem == null) { continue; } if (targetItem.isSimilar(removeItem)) { if (removeItem.getAmount() > targetItem.getAmount()) { removeItem.setAmount(removeItem.getAmount() - targetItem.getAmount()); inventory.remove(removeItem); } else { targetItem.setAmount(targetItem.getAmount() - removeItem.getAmount()); break; } } } } break; case "removeall": for (int i = subIndex + 1; i < args.length; i++) { ItemStack removeItem = LogiBlocksMain.parseItemStack(args[i]); for (int j = 1; j <= 64; j++) { removeItem.setAmount(j); inventory.remove(removeItem); } } break; case "clear": inventory.clear(); break; case "refill": for (ItemStack itemStack : inventory.getContents()) { if (itemStack == null) { continue; } itemStack.setAmount( (args.length > subIndex + 1 ? Boolean.parseBoolean(args[subIndex + 1]) ? 64 : itemStack.getMaxStackSize() : itemStack.getMaxStackSize())); } break; case "set": int slot = Integer.parseInt(args[subIndex + 1]); inventory.setItem( slot >= inventory.getSize() ? inventory.getSize() - 1 : slot, LogiBlocksMain.parseItemStack(args[subIndex + 2])); break; case "copy": Inventory targetInventory; if (args[subIndex + 1].startsWith("@l[") && args[subIndex + 1].endsWith("]")) { Block targetBlock = LogiBlocksMain.parseLocation(args[subIndex + 1], location).getBlock(); if (targetBlock.getState() instanceof InventoryHolder) { targetInventory = ((InventoryHolder) targetBlock.getState()).getInventory(); } else { return false; } } else { Player player = Bukkit.getPlayer(args[subIndex + 1]); if (player == null) { return false; } targetInventory = player.getInventory(); } for (int i = 0; i < inventory.getSize() && i < targetInventory.getSize(); i++) { targetInventory.setItem(i, inventory.getItem(i)); } break; case "show": Player player = Bukkit.getPlayer(args[subIndex + 1]); if (player == null) { return false; } if (args.length > subIndex + 2) { if (Boolean.parseBoolean(args[subIndex + 2])) { Inventory fakeInventory = Bukkit.createInventory(inventory.getHolder(), inventory.getType()); fakeInventory.setContents(inventory.getContents()); player.openInventory(fakeInventory); return true; } } else { player.openInventory(inventory); } break; } } return true; }