Java Code Examples for org.bukkit.inventory.ItemStack#clone()

The following examples show how to use org.bukkit.inventory.ItemStack#clone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContainerShop.java    From QuickShop-Reremake with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Add an item to shops chest.
 *
 * @param item   The itemstack. The amount does not matter, just everything else
 * @param amount The amount to add to the shop.
 */
@Override
public void add(@NotNull ItemStack item, int amount) {
    if (this.unlimited) {
        return;
    }
    item = item.clone();
    int itemMaxStackSize = Util.getItemMaxStackSize(item.getType());
    Inventory inv = this.getInventory();
    int remains = amount;
    while (remains > 0) {
        int stackSize = Math.min(remains, itemMaxStackSize);
        item.setAmount(stackSize);
        Objects.requireNonNull(inv).addItem(item);
        remains -= stackSize;
    }
    this.setSignText();
}
 
Example 2
Source File: BlockContainer.java    From Transport-Pipes with MIT License 6 votes vote down vote up
/**
 * puts "put" into "before" and returns the result. if "put" can't be accumulated with "before", "before" is returned.
 * if "put" does not fit completely in "before", "put"'s amount decreases by how much does fit and the returned item will have an amount of the max stack size
 */
protected ItemStack accumulateItems(ItemStack before, ItemStack put) {
    if (put == null) {
        return before;
    }
    if (before == null) {
        ItemStack putCopy = put.clone();
        put.setAmount(0);
        return putCopy;
    }
    if (!before.isSimilar(put)) {
        return before;
    }
    int beforeAmount = before.getAmount();
    int putAmount = put.getAmount();
    int maxStackSize = before.getMaxStackSize();
    ItemStack returnItem = before.clone();
    returnItem.setAmount(Math.min(beforeAmount + putAmount, maxStackSize));
    put.setAmount(Math.max(putAmount - (maxStackSize - beforeAmount), 0));
    return returnItem;
}
 
Example 3
Source File: Trade.java    From TradePlus with GNU General Public License v3.0 6 votes vote down vote up
private static List<ItemStack> combine(ItemStack[] items) {
  List<ItemStack> result = new ArrayList<>();
  for (int i = 0; i < items.length; i++) {
    ItemStack item = items[i];
    if (item == null) continue;
    item = item.clone();
    for (int j = i + 1; j < items.length; j++) {
      ItemStack dupe = items[j];
      if (item.isSimilar(dupe)) {
        item.setAmount(item.getAmount() + dupe.getAmount());
        items[j] = null;
      }
    }
    result.add(item);
  }
  return result;
}
 
Example 4
Source File: ChestSlimefunGuide.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private void addDisplayRecipe(ChestMenu menu, PlayerProfile profile, List<ItemStack> recipes, int slot, int i, int page) {
    if ((i + (page * 18)) < recipes.size()) {
        ItemStack item = recipes.get(i + (page * 18));

        // We want to clone this item to avoid corrupting the original
        // but we wanna make sure no stupid addon creator sneaked some nulls in here
        if (item != null) {
            item = item.clone();
        }

        menu.replaceExistingItem(slot, item);

        if (page == 0) {
            menu.addMenuClickHandler(slot, (pl, s, itemstack, action) -> {
                displayItem(profile, itemstack, 0, true);
                return false;
            });
        }
    }
    else {
        menu.replaceExistingItem(slot, null);
        menu.addMenuClickHandler(slot, ChestMenuUtils.getEmptyClickHandler());
    }
}
 
Example 5
Source File: WoolMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
private void refillOneWoolPerContainer() {
  if (!PGM.get().getConfiguration().shouldRefillWool()) return;

  for (Entry<Inventory, Map<Integer, ItemStack>> chest : this.woolChests.entrySet()) {
    Inventory inv = chest.getKey();
    for (Entry<Integer, ItemStack> slotEntry : chest.getValue().entrySet()) {
      int slot = slotEntry.getKey();
      ItemStack wool = slotEntry.getValue();
      ItemStack stack = inv.getItem(slotEntry.getKey());

      if (stack == null) {
        stack = wool.clone();
        stack.setAmount(1);
        inv.setItem(slot, stack);
        break;
      } else if (stack.isSimilar(wool) && stack.getAmount() < wool.getAmount()) {
        stack.setAmount(stack.getAmount() + 1);
        inv.setItem(slot, stack);
        break;
      }
    }
  }
}
 
Example 6
Source File: KillRewardMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
private void giveRewards(MatchPlayer killer, Collection<KillReward> rewards) {
  for (KillReward reward : rewards) {
    List<ItemStack> items = new ArrayList<>(reward.items);

    // Apply kit first so it can not override reward items
    reward.kit.apply(killer, false, items);

    for (ItemStack stack : items) {
      ItemStack clone = stack.clone();
      PlayerItemTransferEvent event =
          new PlayerItemTransferEvent(
              null,
              PlayerItemTransferEvent.Type.PLUGIN,
              killer.getBukkit(),
              null,
              null,
              killer.getBukkit().getInventory(),
              null,
              clone,
              null,
              clone.getAmount(),
              null);
      match.callEvent(event);
      if (!event.isCancelled() && event.getQuantity() > 0) {
        // BEWARE: addItem modifies its argument.. send in the clone!
        clone.setAmount(event.getQuantity());
        killer.getBukkit().getInventory().addItem(clone);
      }
    }
  }
}
 
Example 7
Source File: SentinelItemHelper.java    From Sentinel with MIT License 5 votes vote down vote up
/**
 * Processes weapon redirection for an item, returning the redirected item (or an unchanged one).
 */
public ItemStack autoRedirect(ItemStack stack) {
    if (stack == null) {
        return null;
    }
    String redirect = sentinel.weaponRedirects.get(stack.getType().name().toLowerCase());
    if (redirect == null) {
        return stack;
    }
    Material mat = Material.valueOf(redirect.toUpperCase());
    ItemStack newStack = stack.clone();
    newStack.setType(mat);
    return newStack;
}
 
Example 8
Source File: SentinelItemHelper.java    From Sentinel with MIT License 5 votes vote down vote up
/**
 * Swaps the NPC to a ranged weapon if possible.
 */
public void swapToRanged() {
    if (!getNPC().isSpawned() || !getNPC().hasTrait(Inventory.class)) {
        return;
    }
    Inventory inv = getNPC().getTrait(Inventory.class);
    ItemStack[] items = inv.getContents();
    ItemStack held = items[0] == null ? null : items[0].clone();
    if (isRanged(held)) {
        return;
    }
    for (int i = 0; i < items.length; i++) {
        if (sentinel.getLivingEntity() instanceof Player && i >= 36 && i <= 39) {
            // Patch for armor, which is "in the inventory" but not really tracked through it
            continue;
        }
        if (items[i] != null && items[i].getType() != Material.AIR && isRanged(items[i])) {
            items[0] = items[i].clone();
            items[i] = held == null ? null : held.clone();
            inv.setContents(items);
            if (sentinel.getLivingEntity() instanceof Player && i == 40) {
                // Patch for offhand, which is "in the inventory" but not really tracked through it
                sentinel.getLivingEntity().getEquipment().setItemInOffHand(items[i]);
            }
            return;
        }
    }
}
 
Example 9
Source File: ItemBuilder.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public ItemStack removeLore(ItemStack i){
	ItemStack b = i.clone();
	ItemMeta m = b.getItemMeta();
	m.setLore(null);
	b.setItemMeta(m);
	return b;
}
 
Example 10
Source File: OreCrusher.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispBlock = b.getRelative(BlockFace.DOWN);
    Dispenser disp = (Dispenser) dispBlock.getState();
    Inventory inv = disp.getInventory();

    for (ItemStack current : inv.getContents()) {
        for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
            if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
                ItemStack adding = RecipeType.getRecipeOutput(this, convert);
                Inventory outputInv = findOutputInventory(adding, dispBlock, inv);
                if (outputInv != null) {
                    ItemStack removing = current.clone();
                    removing.setAmount(convert.getAmount());
                    inv.removeItem(removing);
                    outputInv.addItem(adding);
                    p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, 1);
                }
                else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);

                return;
            }
        }
    }

    SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example 11
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack getItemStack(ItemStack item, List<String> lore, String message) {
	ItemStack addItem = item.clone();
       ItemMeta addItemMeta = addItem.getItemMeta();
       addItemMeta.setDisplayName(message);
       addItemMeta.setLore(lore);
       addItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
       addItemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
       addItemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
       addItemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
       addItemMeta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
       addItemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
       addItem.setItemMeta(addItemMeta);
       return addItem;
}
 
Example 12
Source File: RecipeFurnace.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public boolean match(ItemStack input){
	if(input.getAmount() < getSubtractAmount()){
		return false;
	}
	ItemStack i = input.clone();
	i.setAmount(1);
	ItemStack compare = toBurn.clone();
	compare.setAmount(1);
	return ItemUtils.itemToStringBlob(i).equals(ItemUtils.itemToStringBlob(compare));
}
 
Example 13
Source File: NMSHandler.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack getItemStack(ItemStack item, List<String> lore, String message) {
	ItemStack addItem = item.clone();
       ItemMeta addItemMeta = addItem.getItemMeta();
       addItemMeta.setDisplayName(message);
       addItemMeta.setLore(lore);
       addItem.setItemMeta(addItemMeta);
       return addItem;
}
 
Example 14
Source File: Main.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static ItemStack applyColor(TeamColor color, ItemStack itemStack, boolean clone) {
    org.screamingsandals.bedwars.api.TeamColor teamColor = color.toApiColor();
    if (clone) {
        itemStack = itemStack.clone();
    }
    return instance.getColorChanger().applyColor(teamColor, itemStack);
}
 
Example 15
Source File: ItemBuilder.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public ItemStack setDisplayName(String s, ItemStack i){
	ItemStack t = i.clone();
	s = ChatColor.translateAlternateColorCodes('&', s);
	ItemMeta m = i.getItemMeta();
	m.setDisplayName(s);
	t.setItemMeta(m);
	return t;
	
}
 
Example 16
Source File: AutoDrier.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void tick(Block b) {
    BlockMenu menu = BlockStorage.getInventory(b);

    if (isProcessing(b)) {
        int timeleft = progress.get(b);
        if (timeleft > 0) {
            ChestMenuUtils.updateProgressbar(menu, 22, timeleft, processing.get(b).getTicks(), getProgressBar());

            if (ChargableBlock.isChargable(b)) {
                if (ChargableBlock.getCharge(b) < getEnergyConsumption()) return;
                ChargableBlock.addCharge(b, -getEnergyConsumption());
                progress.put(b, timeleft - 1);
            }
            else progress.put(b, timeleft - 1);
        }
        else {
            menu.replaceExistingItem(22, new CustomItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " "));
            menu.pushItem(processing.get(b).getOutput()[0], getOutputSlots());

            progress.remove(b);
            processing.remove(b);
        }
    }
    else {
        MachineRecipe r = null;
        int inputSlot = -1;

        for (int slot : getInputSlots()) {
            ItemStack item = menu.getItemInSlot(slot);
            if (item != null) {
                ItemStack output = getOutput(item);

                if (output != null) {
                    r = new MachineRecipe(6, new ItemStack[] { item }, new ItemStack[] { output.clone() });
                    inputSlot = slot;
                    break;
                }
            }
        }

        if (r != null) {
            if (inputSlot == -1) return;
            if (!menu.fits(r.getOutput()[0], getOutputSlots())) return;

            menu.consumeItem(inputSlot);
            processing.put(b, r);
            progress.put(b, r.getTicks());
        }
    }
}
 
Example 17
Source File: PriceOffer.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
public PriceOffer(ItemStack item, int price) {
	Validate.isTrue(!Utils.isEmpty(item), "Item cannot be empty!");
	this.item = item.clone();
	this.setPrice(price);
}
 
Example 18
Source File: ItemCost.java    From GiantTrees with GNU General Public License v3.0 4 votes vote down vote up
public ItemCost(ItemStack stack) {
    super(stack.getAmount());
    this.matchData = stack.getDurability() != 32767;
    this.matchMeta = true;
    this.toMatch = stack.clone();
}
 
Example 19
Source File: ItemLine.java    From Holograms with MIT License 4 votes vote down vote up
@Override
public void setItem(ItemStack item) {
    this.item = item.clone();
    entity.setItem(this.item);
}
 
Example 20
Source File: FuelItem.java    From NyaaUtils with MIT License 4 votes vote down vote up
public void setItem(ItemStack item) {
    this.item = item.clone();
}