aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/item/ItemHolder.java
blob: 51215c93cbf216f5260a11f16403024a94d2fb82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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.GTUtility;

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 GTUtility.stackToInt(toStack());
    }

    @Nonnull
    private ItemStack toStack() {
        ItemStack item = new ItemStack(this.item, 1, meta);
        item.stackTagCompound = tag;
        return item;
    }
}