Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 47 additions & 32 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.njol.skript.aliases;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemData.OldItemData;
import ch.njol.skript.bukkitutil.BukkitUnsafe;
import ch.njol.skript.bukkitutil.ItemUtils;
Expand All @@ -21,6 +22,7 @@
import ch.njol.yggdrasil.Fields;
import ch.njol.yggdrasil.Fields.FieldContext;
import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable;
import com.google.common.collect.Iterators;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
Expand Down Expand Up @@ -50,6 +52,8 @@
public class ItemType implements Unit, Iterable<ItemData>, Container<ItemStack>, YggdrasilExtendedSerializable,
AnyNamed, AnyAmount {

private static final boolean IS_RUNNING_1_21 = Skript.isRunningMinecraft(1, 21);

static {
// This handles updating ItemType and ItemData variable records
Variables.yggdrasil.registerFieldHandler(new FieldHandler() {
Expand Down Expand Up @@ -710,20 +714,18 @@ public static ItemStack[] getCopiedContents(Inventory invi) {
}

/**
* Gets copy of storage contents, i.e. ignores armor and off hand. This is due to Spigot 1.9
* added armor slots, and off hand to default inventory index.
* @param invi Inventory
* Gets copy of storage contents, i.e. ignores armor and off hand.
* This method simply calls {@link Inventory#getStorageContents()} and clones the items contained within the array.
* @param inventory The inventory to obtain contents from.
* @return Copied storage contents
*/
public static ItemStack[] getStorageContents(final Inventory invi) {
if (invi instanceof PlayerInventory) {
ItemStack[] buf = invi.getContents();
ItemStack[] tBuf = new ItemStack[36];
for (int i = 0; i < 36; i++)
if (buf[i] != null)
tBuf[i] = buf[i].clone();
return tBuf;
} else return getCopiedContents(invi);
public static ItemStack[] getStorageContents(Inventory inventory) {
ItemStack[] buf = inventory.getStorageContents();
for (int i = 0; i < buf.length; i++) {
if (buf[i] != null)
buf[i] = buf[i].clone();
}
return buf;
}

/**
Expand Down Expand Up @@ -969,33 +971,46 @@ public void addTo(final List<ItemStack> list) {
/**
* Tries to add this ItemType to the given inventory. Does not call updateInventory for players.
*
* @param invi
* @param inventory The inventory to add this the {@link ItemStack}(s) represented by this ItemType to.
* @return Whether everything could be added to the inventory
*/
public boolean addTo(final Inventory invi) {
// important: don't use inventory.add() - it ignores max stack sizes
ItemStack[] buf = invi.getContents();

ItemStack[] tBuf = buf.clone();
if (invi instanceof PlayerInventory) {
buf = new ItemStack[36];
for(int i = 0; i < 36; ++i) {
buf[i] = tBuf[i];
public boolean addTo(Inventory inventory) {
// TODO remove this when applicable
// On newer versions, such as 1.21.6, this legacy method of manually rewriting inventory content arrays risks
// accidental item deletion and fails to respect properties such as stack size.
// Thus, we switch to use the API methods. However, these API methods do not work properly on older versions
// such as 1.20.6. For those versions, we continue to use this legacy method.
// See https://github.com/SkriptLang/Skript/pull/7986
if (!IS_RUNNING_1_21) {
// important: don't use inventory.add() - it ignores max stack sizes
ItemStack[] buf = inventory.getContents();

ItemStack[] tBuf = buf.clone();
if (inventory instanceof PlayerInventory) {
buf = new ItemStack[36];
for(int i = 0; i < 36; ++i) {
buf[i] = tBuf[i];
}
}
}

final boolean b = addTo(buf);
final boolean b = addTo(buf);

if (invi instanceof PlayerInventory) {
buf = Arrays.copyOf(buf, tBuf.length);
for (int i = tBuf.length - 5; i < tBuf.length; ++i) {
buf[i] = tBuf[i];
if (inventory instanceof PlayerInventory) {
buf = Arrays.copyOf(buf, tBuf.length);
for (int i = tBuf.length - 5; i < tBuf.length; ++i) {
buf[i] = tBuf[i];
}
}
}

assert buf != null;
invi.setContents(buf);
return b;
assert buf != null;
inventory.setContents(buf);
return b;
}
if (!isAll()) {
ItemStack random = getItem().getRandom();
return random == null || inventory.addItem(random).isEmpty();
}
return inventory.addItem(Iterators.toArray(getItem().getAll().iterator(), ItemStack.class)).isEmpty();
}

private static boolean addTo(@Nullable ItemStack is, ItemStack[] buf) {
Expand Down