aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/item/base/BaseItemTickable.java
blob: 8c2473aafea861396390a95e3cd7a712498d39f9 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package gtPlusPlus.core.item.base;

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

import java.util.List;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.creative.AddToCreativeTab;
import gtPlusPlus.core.util.Utils;

public class BaseItemTickable extends CoreItem {

    public final String[] descriptionString;
    public final int itemColour;
    public final int maxTicks;
    public final boolean twoRenderPasses;
    public final boolean ticksInContainers;

    public IIcon[] mIcon = new IIcon[2];

    public BaseItemTickable(boolean twoPass, final String unlocalName, final int colour, final int maxTicks) {
        this(false, twoPass, unlocalName, colour, maxTicks, new String[] {});
    }

    public BaseItemTickable(boolean containerTick, boolean twoPass, final String unlocalName, final int colour,
        final int maxTicks) {
        this(containerTick, twoPass, unlocalName, colour, maxTicks, new String[] {});
    }

    public BaseItemTickable(boolean containerTick, boolean twoPass, final String unlocalName, final int colour,
        final int maxTicks, final String[] Description) {
        super(
            unlocalName,
            AddToCreativeTab.tabMisc,
            1,
            999999999,
            Description,
            EnumRarity.epic,
            EnumChatFormatting.DARK_RED,
            true,
            null);
        this.itemColour = colour;
        this.descriptionString = Description;
        this.maxTicks = maxTicks;
        this.twoRenderPasses = twoPass;
        this.ticksInContainers = containerTick;
        this.maxStackSize = 1;
        // setGregtechItemList();
    }

    @Override
    public void onUpdate(final ItemStack iStack, final World world, final Entity entityHolding, final int p_77663_4_,
        final boolean p_77663_5_) {
        if (world == null || iStack == null) {
            return;
        }
        if (world.isRemote) {
            return;
        }

        boolean active = isTicking(world, iStack);
        if (active) {
            tickItemTag(world, iStack);
        }
    }

    /*
     * private final boolean setGregtechItemList() { ItemList.Component_LavaFilter.set(this); return
     * ItemList.Component_LavaFilter.get(1) != null ? true : false; }
     */

    /**
     *
     * Handle Custom Rendering
     *
     */
    @Override
    @SideOnly(Side.CLIENT)
    public boolean requiresMultipleRenderPasses() {
        return this.twoRenderPasses;
    }

    @Override
    public int getColorFromItemStack(final ItemStack stack, final int renderPass) {
        if (renderPass == 1 && this.twoRenderPasses) {
            return Utils.rgbtoHexValue(255, 255, 255);
        }
        return this.itemColour;
    }

    @Override
    public IIcon getIconFromDamageForRenderPass(final int damage, final int pass) {
        if (this.twoRenderPasses) {
            if (pass == 0) {
                return this.mIcon[0];
            }
            return this.mIcon[1];
        }
        return this.mIcon[0];
    }

    @Override
    public void registerIcons(final IIconRegister i) {

        if (this.twoRenderPasses) {
            this.mIcon[0] = i.registerIcon(GTPlusPlus.ID + ":" + this.getUnlocalizedName());
            this.mIcon[1] = i.registerIcon(GTPlusPlus.ID + ":" + this.getUnlocalizedName() + "_OVERLAY");
        } else {
            this.mIcon[0] = i.registerIcon(GTPlusPlus.ID + ":" + this.getUnlocalizedName());
            // this.overlay = i.registerIcon(getCorrectTextures() + "_OVERLAY");
        }
    }

    protected int getMaxTicks(ItemStack aStack) {
        return maxTicks;
    }

    protected boolean createNBT(World world, ItemStack rStack) {
        final NBTTagCompound tagMain = new NBTTagCompound();
        final NBTTagCompound tagNBT = new NBTTagCompound();
        tagNBT.setLong("Tick", 0);
        tagNBT.setLong("maxTick", getMaxTicks(rStack));
        tagNBT.setBoolean("isActive", true);

        // Try set world time
        if (world != null) {
            // tagNBT.setLong("CreationDate", world.getTotalWorldTime());
        }

        tagMain.setTag("TickableItem", tagNBT);
        rStack.setTagCompound(tagMain);
        Logger.INFO("Created Tickable NBT data.");
        return true;
    }

    public final long getTicks(World world, final ItemStack aStack) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                return aNBT.getLong("Tick");
            }
        } else {
            createNBT(world, aStack);
        }
        return 0L;
    }

    public final boolean setTicks(World world, final ItemStack aStack, final long aDamage) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                aNBT.setLong("Tick", aDamage);
                return true;
            }
        } else {
            createNBT(world, aStack);
        }
        return false;
    }

    public final boolean isTicking(World world, final ItemStack aStack) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                return aNBT.getBoolean("isActive");
            }
        } else {
            return createNBT(world, aStack);
        }
        return true;
    }

    public final boolean setTicking(World world, final ItemStack aStack, final boolean active) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                aNBT.setBoolean("isActive", active);
                return true;
            }
        } else {
            createNBT(world, aStack);
        }
        return false;
    }

    public final boolean getTicksInContainer(World world, final ItemStack aStack) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                return aNBT.getBoolean("ticksInContainer");
            }
        } else {
            createNBT(world, aStack);
        }
        return false;
    }

    public final boolean setTicksInContainer(World world, final ItemStack aStack, final boolean active) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                aNBT.setBoolean("ticksInContainer", active);
                return true;
            }
        } else {
            createNBT(world, aStack);
        }
        return false;
    }

    public final long getDifferenceInWorldTimeToCreationTime(World world, final ItemStack aStack) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                return (world.getTotalWorldTime() - aNBT.getLong("CreationDate"));
            }
        } else {
            createNBT(world, aStack);
        }
        return 0L;
    }

    public final boolean setItemStackCreationTime(final ItemStack aStack, World world) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("TickableItem");
            if (aNBT != null) {
                aNBT.setLong("CreationDate", world.getTotalWorldTime());
                return true;
            }
        } else {
            createNBT(world, aStack);
        }
        return false;
    }

    public final boolean tickItemTag(World world, ItemStack aStack) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            if (aNBT.hasKey("TickableItem")) {
                aNBT = aNBT.getCompoundTag("TickableItem");
                // Done Ticking
                if (getMaxTicks(aStack) - getTicks(world, aStack) <= 0) {
                    setTicking(world, aStack, false);
                    return false;
                }
                if (isTicking(world, aStack)) {
                    if (aNBT != null) {
                        aNBT.setLong("Tick", getTicks(world, aStack) + 1);
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        return createNBT(world, aStack);
    }

    @Override
    public double getDurabilityForDisplay(ItemStack stack) {
        if (stack.getTagCompound() == null) {
            // createNBT(null, stack);
            return 0;
        }
        double currentDamage = getTicks(null, stack);
        return currentDamage / getMaxTicks(stack);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, @SuppressWarnings("rawtypes") List list,
        boolean bool) {
        World world = player.getEntityWorld();
        if (this.descriptionString.length > 0) {
            list.add(EnumChatFormatting.GRAY + this.descriptionString[0]);
        }
        long maxTicks = getMaxTicks(stack);
        long ticks = 0;
        if (stack.hasTagCompound()) {
            ticks = getTicks(world, stack);
        }
        EnumChatFormatting durability = EnumChatFormatting.GRAY;
        if (maxTicks - ticks > (maxTicks * 0.8)) {
            durability = EnumChatFormatting.GRAY;
        } else if (maxTicks - ticks > (maxTicks * 0.6)) {
            durability = EnumChatFormatting.GREEN;
        } else if (maxTicks - ticks > (maxTicks * 0.4)) {
            durability = EnumChatFormatting.YELLOW;
        } else if (maxTicks - ticks > (maxTicks * 0.2)) {
            durability = EnumChatFormatting.GOLD;
        } else if (maxTicks - ticks > 0) {
            durability = EnumChatFormatting.RED;
        }
        list.add(durability + "" + ((maxTicks - ticks) / 20) + EnumChatFormatting.GRAY + " seconds until decay");

        if (this.descriptionString.length > 1) {
            for (int h = 1; h < this.descriptionString.length; h++) {
                list.add(EnumChatFormatting.GRAY + this.descriptionString[h]);
            }
        }

        // super.addInformation(stack, player, list, bool);
    }

    @Override
    public boolean showDurabilityBar(ItemStack stack) {
        return false;
    }
}