aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/item/base/BaseItemDamageable.java
blob: fb24fbf2f12ea3eb4851fc1d955067acea0b0f76 (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
package gtPlusPlus.core.item.base;

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

import java.util.List;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gtPlusPlus.api.objects.Logger;

public class BaseItemDamageable extends Item {

    private final EnumRarity rarity;
    private final String itemDescription;
    protected String itemName;
    private final boolean hasEffect;

    public BaseItemDamageable(final String unlocalizedName, final CreativeTabs creativeTab, final int stackSize,
        final int maxDmg, final String description, final EnumRarity regRarity, final EnumChatFormatting colour,
        final boolean Effect, final ItemStack OverrideItem) {
        this.setUnlocalizedName(unlocalizedName);
        this.setTextureName(GTPlusPlus.ID + ":" + unlocalizedName);
        this.setCreativeTab(creativeTab);
        this.setMaxStackSize(1);
        this.setMaxDamage(251);
        this.setNoRepair();
        this.rarity = regRarity;
        this.itemDescription = description;
        this.hasEffect = Effect;
        GameRegistry.registerItem(this, unlocalizedName);
    }

    public String getItemDescription() {
        return this.itemDescription;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void addInformation(final ItemStack stack, final EntityPlayer aPlayer, final List list, final boolean bool) {
        int dmg = (int) getItemDamage(stack);
        if (dmg <= 3) {
            list.add(EnumChatFormatting.GRAY + this.itemDescription);
        }
        if (dmg > 3 && dmg <= 25) {
            list.add(
                EnumChatFormatting.GRAY
                    + "You have discovered that smashing this against valuable stones has some function..");
        } else if (dmg > 0) {
            int maxDamage = 250;
            list.add(
                EnumChatFormatting.GRAY + ""
                    + (maxDamage - getItemDamage(stack))
                    + "/"
                    + maxDamage
                    + " gems remaining.");
        }
    }

    @Override
    @SideOnly(Side.CLIENT)
    public EnumRarity getRarity(final ItemStack par1ItemStack) {
        int dmg = (int) getItemDamage(par1ItemStack);
        if (dmg > 200) {
            return EnumRarity.epic;
        }
        return this.rarity;
    }

    @Override
    public boolean hasEffect(final ItemStack par1ItemStack, final int pass) {
        int dmg = (int) getItemDamage(par1ItemStack);
        if (dmg > 200) {
            return true;
        }
        return this.hasEffect;
    }

    @Override
    public String getItemStackDisplayName(final ItemStack tItem) {
        if ((this.itemName == null) || this.itemName.isEmpty()) {
            return super.getItemStackDisplayName(tItem);
        }
        return this.itemName;
    }

    private static boolean createNBT(ItemStack rStack) {
        final NBTTagCompound tagMain = new NBTTagCompound();
        final NBTTagCompound tagNBT = new NBTTagCompound();
        tagNBT.setLong("Value", 0);
        tagMain.setTag("Damage", tagNBT);
        rStack.setTagCompound(tagMain);
        return true;
    }

    public static long getItemDamage(final ItemStack aStack) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("Damage");
            if (aNBT != null) {
                return aNBT.getLong("Value");
            }
        } else {
            createNBT(aStack);
        }
        return 0L;
    }

    public static boolean setItemDamage(final ItemStack aStack, final long aDamage) {
        NBTTagCompound aNBT = aStack.getTagCompound();
        if (aNBT != null) {
            aNBT = aNBT.getCompoundTag("Damage");
            if (aNBT != null) {
                aNBT.setLong("Value", aDamage);
                return true;
            }
        } else {
            createNBT(aStack);
        }
        return false;
    }

    @Override
    public double getDurabilityForDisplay(ItemStack stack) {
        if (stack.getTagCompound() == null) {
            createNBT(stack);
        }
        double currentDamage = getItemDamage(stack);
        double durabilitypercent = currentDamage / 100;
        double inverse = (100 - durabilitypercent);
        return durabilitypercent;
    }

    @Override
    public boolean showDurabilityBar(ItemStack stack) {
        int dmg = (int) getItemDamage(stack);
        return dmg > 20;
    }

    public static ItemStack damageItem(ItemStack item) {
        if (item != null) {
            long currentUse = BaseItemDamageable.getItemDamage(item);
            if (currentUse >= 0 && currentUse <= 250) {
                BaseItemDamageable.setItemDamage(item, currentUse + 1);
            }
            return item;
        }
        return null;
    }

    @Override
    public boolean doesContainerItemLeaveCraftingGrid(ItemStack stack) {
        Logger.INFO("Does Leave Table? " + stack.getDisplayName());
        return true;
    }

    @Override
    public boolean getShareTag() {
        return true;
    }

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

    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        ItemStack stack = itemStack.copy();
        // stack.setItemDamage(stack.getItemDamage() + 1);
        damageItem(stack);
        stack.stackSize = 1;
        return stack;
    }

    @Override
    public int getDamage(ItemStack stack) {
        return (int) getItemDamage(stack);
    }
}