diff options
Diffstat (limited to 'src/main/java/gregtech/api/objects')
-rw-r--r-- | src/main/java/gregtech/api/objects/GT_ItemStack.java | 38 | ||||
-rw-r--r-- | src/main/java/gregtech/api/objects/GT_ItemStack2.java | 3 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/objects/GT_ItemStack.java b/src/main/java/gregtech/api/objects/GT_ItemStack.java index 210f807c5d..492655740d 100644 --- a/src/main/java/gregtech/api/objects/GT_ItemStack.java +++ b/src/main/java/gregtech/api/objects/GT_ItemStack.java @@ -7,6 +7,7 @@ import net.minecraft.item.ItemStack; import gregtech.api.enums.GT_Values; import gregtech.api.util.GT_Utility; import gregtech.api.util.item.ItemHolder; +import it.unimi.dsi.fastutil.Hash; /** * An optimization of {@link ItemStack} to have a better {@code hashcode} and {@code equals} in order to improve @@ -14,6 +15,25 @@ import gregtech.api.util.item.ItemHolder; */ public class GT_ItemStack extends ItemHolder { + /** + * A better {@link Hash.Strategy} for {@link ItemStack}. Implementation originally from {@code GT_ItemStack2}. + */ + public static final Hash.Strategy<ItemStack> ITEMSTACK_HASH_STRATEGY2 = new Hash.Strategy<>() { + + @Override + public int hashCode(ItemStack o) { + return o.getItem() + .hashCode() * 38197 + Items.feather.getDamage(o); + } + + @Override + public boolean equals(ItemStack a, ItemStack b) { + if (a == b) return true; + if (a == null || b == null) return false; + return a.getItem() == b.getItem() && Items.feather.getDamage(a) == Items.feather.getDamage(b); + } + }; + public final Item mItem; public final byte mStackSize; public final short mMetaData; @@ -66,4 +86,22 @@ public class GT_ItemStack extends ItemHolder { public int hashCode() { return GT_Utility.stackToInt(toStack()); } + + /** + * @see #internalCopyStack(ItemStack, boolean) + */ + public static ItemStack internalCopyStack(ItemStack aStack) { + return internalCopyStack(aStack, false); + } + + /** + * Replicates the copy behavior of {@link #toStack()} but for normal {@link ItemStack}s. + * + * @param aStack the stack to copy + * @param wildcard whether to use wildcard damage value + * @return a copy of the stack with stack size 1 and no NBT + */ + public static ItemStack internalCopyStack(ItemStack aStack, boolean wildcard) { + return new ItemStack(aStack.getItem(), 1, wildcard ? GT_Values.W : Items.feather.getDamage(aStack)); + } } diff --git a/src/main/java/gregtech/api/objects/GT_ItemStack2.java b/src/main/java/gregtech/api/objects/GT_ItemStack2.java index b137e78829..aa93876830 100644 --- a/src/main/java/gregtech/api/objects/GT_ItemStack2.java +++ b/src/main/java/gregtech/api/objects/GT_ItemStack2.java @@ -7,7 +7,10 @@ import net.minecraft.item.ItemStack; * GT_ItemStack, but with a better hashCode(). Due to this change, it should not be placed in the same hash based data * structure with GT_ItemStack. It also shouldn't be used to construct search query into a hash based data structure * that contains GT_ItemStack. + * + * @deprecated See {@link GT_ItemStack#ITEMSTACK_HASH_STRATEGY2} */ +@Deprecated public class GT_ItemStack2 extends GT_ItemStack { public GT_ItemStack2(Item aItem, long aStackSize, long aMetaData) { |