aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/util/minecraft/NBTUtils.java
blob: 51be886acd8d94f44694956b1ca0654a1a942c51 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gtPlusPlus.core.util.minecraft;

import static gtPlusPlus.core.item.ModItems.ZZZ_Empty;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

import gregtech.api.util.GTUtility;

public class NBTUtils {

    public static NBTTagCompound getNBT(ItemStack aStack) {
        NBTTagCompound rNBT = aStack.getTagCompound();
        return ((rNBT == null) ? new NBTTagCompound() : rNBT);
    }

    public static void setBookTitle(ItemStack aStack, String aTitle) {
        NBTTagCompound tNBT = getNBT(aStack);
        tNBT.setString("title", aTitle);
        GTUtility.ItemNBT.setNBT(aStack, tNBT);
    }

    public static ItemStack[] readItemsFromNBT(ItemStack itemstack) {
        NBTTagCompound tNBT = getNBT(itemstack);
        final NBTTagList list = tNBT.getTagList("Items", 10);
        ItemStack[] inventory = new ItemStack[list.tagCount()];
        for (int i = 0; i < list.tagCount(); i++) {
            final NBTTagCompound data = list.getCompoundTagAt(i);
            final int slot = data.getInteger("Slot");
            if ((slot >= 0) && (slot < list.tagCount())) {
                if (ItemStack.loadItemStackFromNBT(data) == ItemUtils.getSimpleStack(ZZZ_Empty)) {
                    inventory[slot] = null;
                } else {
                    inventory[slot] = ItemStack.loadItemStackFromNBT(data);
                }
            }
        }
        return inventory;
    }

    public static ItemStack writeItemsToNBT(ItemStack itemstack, ItemStack[] stored) {
        NBTTagCompound tNBT = getNBT(itemstack);
        final NBTTagList list = new NBTTagList();
        for (int i = 0; i < stored.length; i++) {
            final ItemStack stack = stored[i];
            if (stack != null) {
                final NBTTagCompound data = new NBTTagCompound();
                stack.writeToNBT(data);
                data.setInteger("Slot", i);
                list.appendTag(data);
            } else {
                final NBTTagCompound data = new NBTTagCompound();
                ItemStack nullstack = ItemUtils.getSimpleStack(ZZZ_Empty);
                nullstack.writeToNBT(data);
                data.setInteger("Slot", i);
                list.appendTag(data);
            }
        }
        tNBT.setTag("Items", list);
        itemstack.setTagCompound(tNBT);
        return itemstack;
    }

    public static ItemStack writeItemsToNBT(ItemStack itemstack, ItemStack[] stored, String customkey) {
        NBTTagCompound tNBT = getNBT(itemstack);
        final NBTTagList list = new NBTTagList();
        for (int i = 0; i < stored.length; i++) {
            final ItemStack stack = stored[i];
            if (stack != null) {
                final NBTTagCompound data = new NBTTagCompound();
                stack.writeToNBT(data);
                data.setInteger("Slot", i);
                list.appendTag(data);
            }
        }
        tNBT.setTag(customkey, list);
        itemstack.setTagCompound(tNBT);
        return itemstack;
    }

    public static void setBoolean(ItemStack aStack, String aTag, boolean aBoolean) {
        NBTTagCompound tNBT = getNBT(aStack);
        tNBT.setBoolean(aTag, aBoolean);
        GTUtility.ItemNBT.setNBT(aStack, tNBT);
    }

    public static boolean getBoolean(ItemStack aStack, String aTag) {
        NBTTagCompound tNBT = getNBT(aStack);
        return tNBT.getBoolean(aTag);
    }

    public static void setInteger(ItemStack aStack, String aTag, int aInt) {
        NBTTagCompound tNBT = getNBT(aStack);
        tNBT.setInteger(aTag, aInt);
        GTUtility.ItemNBT.setNBT(aStack, tNBT);
    }

    public static int getInteger(ItemStack aStack, String aTag) {
        NBTTagCompound tNBT = getNBT(aStack);
        return tNBT.getInteger(aTag);
    }

    public static void setLong(ItemStack aStack, String aTag, long aInt) {
        NBTTagCompound tNBT = getNBT(aStack);
        tNBT.setLong(aTag, aInt);
        GTUtility.ItemNBT.setNBT(aStack, tNBT);
    }

    public static long getLong(ItemStack aStack, String aTag) {
        NBTTagCompound tNBT = getNBT(aStack);
        return tNBT.getLong(aTag);
    }

    public static void setString(ItemStack aStack, String aTag, String aString) {
        NBTTagCompound tNBT = getNBT(aStack);
        tNBT.setString(aTag, aString);
        GTUtility.ItemNBT.setNBT(aStack, tNBT);
    }

    public static String getString(ItemStack aStack, String aTag) {
        NBTTagCompound tNBT = getNBT(aStack);
        return tNBT.getString(aTag);
    }

    public static boolean hasKey(ItemStack stack, String key) {
        final NBTTagCompound itemData = getNBT(stack);
        return itemData.hasKey(key);
    }

    public static boolean createIntegerTagCompound(ItemStack rStack, String tagName, String keyName, int keyValue) {
        final NBTTagCompound tagMain = getNBT(rStack);
        final NBTTagCompound tagNBT = new NBTTagCompound();
        tagNBT.setInteger(keyName, keyValue);
        tagMain.setTag(tagName, tagNBT);
        rStack.setTagCompound(tagMain);
        return true;
    }

    public static NBTTagCompound getTagCompound(ItemStack aStack, String tagName) {
        NBTTagCompound aNBT = getNBT(aStack);
        if (aNBT != null && hasKey(aStack, tagName)) {
            aNBT = aNBT.getCompoundTag(tagName);
            return aNBT;
        }
        return null;
    }

    public static boolean hasTagCompound(ItemStack aStack) {
        return aStack.hasTagCompound();
    }

    public static void createEmptyTagCompound(ItemStack aStack) {
        if (!hasTagCompound(aStack)) {
            NBTTagCompound aTag = new NBTTagCompound();
            aStack.setTagCompound(aTag);
        }
    }
}