aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/item
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gregtech/api/util/item')
-rw-r--r--src/main/java/gregtech/api/util/item/ItemHolder.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/util/item/ItemHolder.java b/src/main/java/gregtech/api/util/item/ItemHolder.java
new file mode 100644
index 0000000000..4675d0ba0e
--- /dev/null
+++ b/src/main/java/gregtech/api/util/item/ItemHolder.java
@@ -0,0 +1,79 @@
+package gregtech.api.util.item;
+
+import static net.minecraftforge.oredict.OreDictionary.getOreIDs;
+
+import java.util.Arrays;
+
+import javax.annotation.Nonnull;
+
+import net.minecraft.init.Items;
+import net.minecraft.item.Item;
+import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.NBTTagCompound;
+
+import gregtech.api.util.GT_Utility;
+
+public class ItemHolder {
+
+ private final Item item;
+ private final int meta;
+ private final NBTTagCompound tag;
+ private final int[] oreIDs;
+
+ public ItemHolder(@Nonnull ItemStack item) {
+ this.item = item.getItem();
+ this.meta = Items.feather.getDamage(item);
+ this.tag = item.getTagCompound();
+ this.oreIDs = getOreIDs(item);
+ }
+
+ public Item getItem() {
+ return item;
+ }
+
+ public int getMeta() {
+ return meta;
+ }
+
+ public NBTTagCompound getNBT() {
+ return tag;
+ }
+
+ public int[] getOreDictTagIDs() {
+ return oreIDs;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (other == this) return true;
+ if (!(other instanceof ItemHolder otherIH)) return false;
+ if (Arrays.stream(oreIDs)
+ .anyMatch(id -> {
+ for (int i = 0; i < otherIH.getOreDictTagIDs().length; i++) {
+ if (id == otherIH.getOreDictTagIDs()[i]) return true;
+ }
+ return false;
+ })) {
+ return true;
+ }
+
+ if (item != otherIH.getItem() || meta != otherIH.getMeta()) {
+ return false;
+ }
+ if (this.tag == null && otherIH.getNBT() == null) return true;
+ if (this.tag == null || otherIH.getNBT() == null) return false;
+ return this.tag.equals(otherIH);
+ }
+
+ @Override
+ public int hashCode() {
+ return GT_Utility.stackToInt(toStack());
+ }
+
+ @Nonnull
+ private ItemStack toStack() {
+ ItemStack item = new ItemStack(this.item, 1, meta);
+ item.stackTagCompound = tag;
+ return item;
+ }
+}