aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/item/general/ItemBlueprint.java
blob: 1f1848a90c326a88ec82af45debbb1bed63712ea (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package gtPlusPlus.core.item.general;

import static gregtech.api.enums.Mods.GTPlusPlus;

import java.util.List;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

import cpw.mods.fml.common.registry.GameRegistry;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.creative.AddToCreativeTab;
import gtPlusPlus.core.interfaces.IItemBlueprint;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.core.util.minecraft.PlayerUtils;

public class ItemBlueprint extends Item implements IItemBlueprint {

    public ItemBlueprint(final String unlocalizedName) {
        this.setUnlocalizedName(unlocalizedName);
        this.setTextureName(GTPlusPlus.ID + ":" + unlocalizedName);
        this.setMaxStackSize(1);
        this.setCreativeTab(AddToCreativeTab.tabMachines);
        // this.bpID = MathUtils.randInt(0, 1000);
        GameRegistry.registerItem(this, unlocalizedName);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void addInformation(final ItemStack itemStack, final EntityPlayer aPlayer, final List list,
        final boolean bool) {
        // Create some NBT if it's not there, otherwise this does nothing.
        if (!itemStack.hasTagCompound()) {
            this.createNBT(itemStack);
        }
        // Set up some default variables.
        int id = -1;
        String name = "";
        boolean blueprint = false;
        // Get proper display vars from NBT if it's there
        if (itemStack.hasTagCompound()) {
            // Utils.LOG_WARNING("Found TagCompound");
            id = (int) this.getNBT(itemStack, "mID");
            name = (String) this.getNBT(itemStack, "mName");
            blueprint = (boolean) this.getNBT(itemStack, "mBlueprint");
        }
        // Write to tooltip list for each viable setting.
        if (itemStack.hasTagCompound()) {
            if (id != -1) {
                list.add(
                    EnumChatFormatting.GRAY + StatCollector
                        .translateToLocalFormatted(StatCollector.translateToLocal("item.itemBlueprint.tooltip.0"), id));
            }
            if (blueprint) {
                list.add(
                    EnumChatFormatting.BLUE + StatCollector.translateToLocalFormatted(
                        StatCollector.translateToLocal("item.itemBlueprint.tooltip.1"),
                        name));
            } else {
                list.add(EnumChatFormatting.RED + StatCollector.translateToLocal("item.itemBlueprint.tooltip.2"));
            }
        } else {
            list.add(EnumChatFormatting.RED + StatCollector.translateToLocal("item.itemBlueprint.tooltip.2"));
        }
        super.addInformation(itemStack, aPlayer, list, bool);
    }

    @Override
    public void onCreated(final ItemStack itemStack, final World world, final EntityPlayer player) {
        this.createNBT(itemStack);
    }

    @Override
    public void onUpdate(final ItemStack itemStack, final World par2World, final Entity par3Entity, final int par4,
        final boolean par5) {}

    @Override
    public ItemStack onItemRightClick(final ItemStack itemStack, final World world, final EntityPlayer par3Entity) {
        // Let the player know what blueprint is held
        if (itemStack.hasTagCompound()) {
            PlayerUtils.messagePlayer(
                par3Entity,
                "This Blueprint holds NBT data. " + "|"
                    + this.getNBT(itemStack, "mID")
                    + "|"
                    + this.getNBT(itemStack, "mBlueprint")
                    + "|"
                    + this.getNBT(itemStack, "mName")
                    + "|"
                    + ItemUtils.getArrayStackNames(this.readItemsFromNBT(itemStack)));
        } else {
            this.createNBT(itemStack);
            PlayerUtils.messagePlayer(par3Entity, "This is a placeholder. " + this.getNBT(itemStack, "mID"));
        }

        return super.onItemRightClick(itemStack, world, par3Entity);
    }

    public ItemStack[] readItemsFromNBT(final ItemStack itemStack) {
        ItemStack[] blueprint = new ItemStack[9];
        if (itemStack.hasTagCompound()) {
            final NBTTagCompound nbt = itemStack.getTagCompound();
            final NBTTagList list = nbt.getTagList("Inventory", 10);
            blueprint = new ItemStack[INV_SIZE];
            for (int i = 0; i < list.tagCount(); i++) {
                final NBTTagCompound data = list.getCompoundTagAt(i);
                final int slot = data.getInteger("Slot");
                if ((slot >= 0) && (slot < INV_SIZE)) {
                    blueprint[slot] = ItemStack.loadItemStackFromNBT(data);
                }
            }
            return blueprint;
        }
        return null;
    }

    public ItemStack writeItemsToNBT(final ItemStack itemStack, final ItemStack[] craftingGrid) {
        if (itemStack.hasTagCompound()) {
            final NBTTagCompound nbt = itemStack.getTagCompound();
            final NBTTagList list = new NBTTagList();
            for (int i = 0; i < INV_SIZE; i++) {
                final ItemStack stack = craftingGrid[i];
                if (stack != null) {
                    final NBTTagCompound data = new NBTTagCompound();
                    stack.writeToNBT(data);
                    data.setInteger("Slot", i);
                    list.appendTag(data);
                }
            }
            nbt.setTag("Inventory", list);
            itemStack.setTagCompound(nbt);
            return itemStack;
        }
        return null;
    }

    @Override
    public boolean isBlueprint(final ItemStack stack) {
        return true;
    }

    @Override
    public boolean setBlueprint(final ItemStack stack, final IInventory craftingTable, final ItemStack output) {
        boolean hasBP = false;
        ItemStack[] blueprint = new ItemStack[9];

        if (stack.hasTagCompound()) {
            hasBP = (boolean) this.getNBT(stack, "mBlueprint");
            blueprint = this.readItemsFromNBT(stack);
        }

        if (!hasBP) {
            try {
                for (int o = 0; o < craftingTable.getSizeInventory(); o++) {
                    blueprint[o] = craftingTable.getStackInSlot(o);
                    if (blueprint[0] != null) {
                        blueprint[0].stackSize = 0;
                    }
                }
                this.writeItemsToNBT(stack, blueprint);
                if (stack.hasTagCompound()) {
                    if (stack.getTagCompound()
                        .getCompoundTag("Items") != null) {
                        stack.stackTagCompound.setBoolean("mBlueprint", true);
                    } else {
                        // Invalid BP saved?
                    }
                    hasBP = (boolean) this.getNBT(stack, "mBlueprint");
                }

                if (output != null) {
                    this.setBlueprintName(stack, output.getDisplayName());
                    return true;
                }
                return false;
            } catch (final Throwable t) {
                return false;
            }
        }
        return false;
    }

    @Override
    public void setBlueprintName(final ItemStack stack, final String name) {
        stack.stackTagCompound.setString("mName", name);
    }

    @Override
    public boolean hasBlueprint(final ItemStack stack) {
        if (stack.hasTagCompound()) {
            return (boolean) this.getNBT(stack, "mBlueprint");
        }
        return false;
    }

    @Override
    public ItemStack[] getBlueprint(final ItemStack stack) {
        ItemStack[] blueprint = new ItemStack[9];
        if (stack.hasTagCompound()) {
            blueprint = this.readItemsFromNBT(stack);
        }
        try {
            final ItemStack[] returnStack = new ItemStack[9];
            for (int o = 0; o < blueprint.length; o++) {
                returnStack[o] = blueprint[o];
                if (returnStack[0] != null) {
                    returnStack[0].stackSize = 1;
                }
            }
            return returnStack;
        } catch (final Throwable t) {
            return null;
        }
    }

    public boolean createNBT(final ItemStack itemStack) {
        if (itemStack.hasTagCompound()) {
            if (!itemStack.stackTagCompound.getBoolean("mBlueprint") && !itemStack.stackTagCompound.getString("mName")
                .isEmpty()) {
                // No Blueprint and no name Set
                Logger.WARNING("No Blueprint and no name Set");
                return false;
            } else if (itemStack.stackTagCompound.getBoolean("mBlueprint")
                && !itemStack.stackTagCompound.getString("mName")
                    .isEmpty()) {
                        // Has Blueprint but invalid name set
                        Logger.WARNING("Has Blueprint but invalid name set");
                        return false;
                    } else
                if (!itemStack.stackTagCompound.getBoolean("mBlueprint")
                    && itemStack.stackTagCompound.getString("mName")
                        .isEmpty()) {
                            // Has no Blueprint, but strangely has a name
                            Logger.WARNING("Has no Blueprint, but strangely has a name");
                            return false;
                        }
            return false;
        } else if (!itemStack.hasTagCompound()) {
            final int bpID = MathUtils.randInt(0, 1000);
            final boolean hasRecipe = false;
            final String recipeName = "";
            Logger.WARNING("Creating Blueprint, setting up it's NBT data. " + bpID);
            itemStack.stackTagCompound = new NBTTagCompound();
            itemStack.stackTagCompound.setInteger("mID", bpID);
            itemStack.stackTagCompound.setBoolean("mBlueprint", hasRecipe);
            itemStack.stackTagCompound.setString("mName", recipeName);
            return true;
        } else {
            final int bpID = MathUtils.randInt(0, 1000);
            final boolean hasRecipe = false;
            final String recipeName = "";
            Logger.WARNING("Creating a Blueprint, setting up it's NBT data. " + bpID);
            itemStack.stackTagCompound = new NBTTagCompound();
            itemStack.stackTagCompound.setInteger("mID", bpID);
            itemStack.stackTagCompound.setBoolean("mBlueprint", hasRecipe);
            itemStack.stackTagCompound.setString("mName", recipeName);
            return true;
        }
    }

    public Object getNBT(final ItemStack itemStack, final String tagNBT) {
        if (!itemStack.hasTagCompound()) {
            return null;
        }
        Object o = null;
        switch (tagNBT) {
            case "mID":
                o = itemStack.stackTagCompound.getInteger(tagNBT);
                break;
            case "mBlueprint":
                o = itemStack.stackTagCompound.getBoolean(tagNBT);
                break;
            case "mName":
                o = itemStack.stackTagCompound.getString(tagNBT);
                break;
            case "":
                // For More Tag Support
                // o = itemStack.stackTagCompound.getInteger(tagNBT);
                break;
        }
        return o;
    }
}