aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/xmod/gregtech/api/items/GTMetaItemX32.java
blob: 102a3cc0b7c686645e84dc9fdf6ff6047c259a19 (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
package gtPlusPlus.xmod.gregtech.api.items;

import java.util.Arrays;
import java.util.List;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTechAPI;
import gregtech.api.enums.GTValues;
import gregtech.api.enums.Materials;
import gregtech.api.enums.OrePrefixes;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.util.GTLanguageManager;
import gregtech.api.util.GTOreDictUnificator;
import gregtech.api.util.GTUtility;
import gtPlusPlus.core.util.Utils;
import gtPlusPlus.core.util.math.MathUtils;

/**
 * @author Gregorius Techneticies
 *         <p/>
 *         One Item for everything!
 *         <p/>
 *         This brilliant Item Class is used for automatically generating all possible variations of Material Items,
 *         like Dusts, Ingots, Gems, Plates and similar. It saves me a ton of work, when adding Items, because I always
 *         have to make a new Item SubType for each OreDict Prefix, when adding a new Material.
 *         <p/>
 *         As you can see, up to 32766 Items can be generated using this Class. And the last 766 Items can be custom
 *         defined, just to save space and MetaData.
 *         <p/>
 *         These Items can also have special RightClick abilities, electric Charge or even be set to become a Food alike
 *         Item.
 */
public abstract class GTMetaItemX32 extends GTMetaItem {

    protected final OrePrefixes[] mGeneratedPrefixList;

    /**
     * Creates the Item using these Parameters.
     *
     * @param aUnlocalized         The Unlocalized Name of this Item.
     * @param aGeneratedPrefixList The OreDict Prefixes you want to have generated.
     */
    public GTMetaItemX32(final String aUnlocalized, final OrePrefixes... aGeneratedPrefixList) {
        super(aUnlocalized, (short) 32000, (short) 766);
        this.mGeneratedPrefixList = Arrays.copyOf(aGeneratedPrefixList, 32);

        for (int i = 0; i < 32000; i++) {
            final OrePrefixes tPrefix = this.mGeneratedPrefixList[i / 1000];
            if (tPrefix == null) {
                continue;
            }
            final Materials tMaterial = GregTechAPI.sGeneratedMaterials[i % 1000];
            if (tMaterial == null) {
                continue;
            }
            if (this.doesMaterialAllowGeneration(tPrefix, tMaterial)) {
                final ItemStack tStack = new ItemStack(this, 1, i);
                GTLanguageManager.addStringLocalization(
                    this.getUnlocalizedName(tStack) + ".name",
                    this.getDefaultLocalization(tPrefix, tMaterial, i));
                GTLanguageManager.addStringLocalization(
                    this.getUnlocalizedName(tStack) + ".tooltip",
                    tMaterial.getToolTip(tPrefix.mMaterialAmount / GTValues.M));
                if (tPrefix.mIsUnificatable) {
                    GTOreDictUnificator.set(tPrefix, tMaterial, tStack);
                } else {
                    GTOreDictUnificator.registerOre(tPrefix.get(tMaterial), tStack);
                }
                if (((tPrefix == OrePrefixes.stick) || (tPrefix == OrePrefixes.wireFine))
                    && ((tMaterial == Materials.Lead) || (tMaterial == Materials.Tin)
                        || (tMaterial == Materials.SolderingAlloy))) {
                    GregTechAPI.sSolderingMetalList.add(tStack);
                }
            }
        }
    }

    /* ---------- OVERRIDEABLE FUNCTIONS ---------- */

    /**
     * @return the Color Modulation the Material is going to be rendered with.
     */
    @Override
    public short[] getRGBa(final ItemStack aStack) {
        final Materials tMaterial = GregTechAPI.sGeneratedMaterials[this.getDamage(aStack) % 1000];
        return tMaterial == null ? Materials._NULL.mRGBa : tMaterial.mRGBa;
    }

    /**
     * @param aPrefix   this can be null, you have to return false in that case
     * @param aMaterial this can be null, you have to return false in that case
     * @return if this Item should be generated and visible.
     */
    public boolean doesMaterialAllowGeneration(final OrePrefixes aPrefix, final Materials aMaterial) {
        // You have to check for at least these Conditions in every Case! So add a super Call like the following for
        // this before executing your Code:
        // if (!super.doesMaterialAllowGeneration(aPrefix, aMaterial)) return false;
        return aPrefix != null && aPrefix.doGenerateItem(aMaterial);
    }

    /* ---------- OVERRIDEABLE FUNCTIONS ---------- */

    /**
     * @param aPrefix   the OreDict Prefix
     * @param aMaterial the Material
     * @param aMetaData a Index from [0 - 31999]
     * @return the Localized Name when default LangFiles are used.
     */
    public String getDefaultLocalization(final OrePrefixes aPrefix, final Materials aMaterial, final int aMetaData) {
        return aPrefix.getDefaultLocalNameForItem(aMaterial);
    }

    /**
     * @param aMetaData a Index from [0 - 31999]
     * @param aMaterial the Material
     * @return an Icon Container for the Item Display.
     */
    public final IIconContainer getIconContainer(final int aMetaData, final Materials aMaterial) {
        return (this.mGeneratedPrefixList[aMetaData / 1000] != null)
            && (this.mGeneratedPrefixList[aMetaData / 1000].mTextureIndex >= 0)
                ? aMaterial.mIconSet.mTextures[this.mGeneratedPrefixList[aMetaData / 1000].mTextureIndex]
                : null;
    }

    /**
     * @param aPrefix         always != null
     * @param aMaterial       always != null
     * @param aDoShowAllItems this is the Configuration Setting of the User, if he wants to see all the Stuff like Tiny
     *                        Dusts or Crushed Ores as well.
     * @return if this Item should be visible in NEI or Creative
     */
    public boolean doesShowInCreative(final OrePrefixes aPrefix, final Materials aMaterial,
        final boolean aDoShowAllItems) {
        return true;
    }

    /* ---------- INTERNAL OVERRIDES ---------- */

    @Override
    public ItemStack getContainerItem(final ItemStack aStack) {
        final int aDamage = aStack.getItemDamage();
        if ((aDamage < 32000) && (aDamage >= 0)) {
            final Materials aMaterial = GregTechAPI.sGeneratedMaterials[aDamage % 1000];
            if ((aMaterial != null) && (aMaterial != Materials.Empty) && (aMaterial != Materials._NULL)) {
                final OrePrefixes aPrefix = this.mGeneratedPrefixList[aDamage / 1000];
                if (aPrefix != null) {
                    return GTUtility.copyAmount(1, aPrefix.mContainerItem);
                }
            }
        }
        return null;
    }

    @Override
    public final IIconContainer getIconContainer(final int aMetaData) {
        return GregTechAPI.sGeneratedMaterials[aMetaData % 1000] == null ? null
            : this.getIconContainer(aMetaData, GregTechAPI.sGeneratedMaterials[aMetaData % 1000]);
    }

    @Override
    @SideOnly(Side.CLIENT)
    public final void getSubItems(final Item var1, final CreativeTabs aCreativeTab, final List aList) {
        for (int i = 0; i < 32000; i++) {
            if (this.doesMaterialAllowGeneration(
                this.mGeneratedPrefixList[i / 1000],
                GregTechAPI.sGeneratedMaterials[i % 1000])
                && this.doesShowInCreative(
                    this.mGeneratedPrefixList[i / 1000],
                    GregTechAPI.sGeneratedMaterials[i % 1000],
                    GregTechAPI.sDoShowAllItemsInCreative)) {
                final ItemStack tStack = new ItemStack(this, 1, i);
                this.isItemStackUsable(tStack);
                aList.add(tStack);
            }
        }
        super.getSubItems(var1, aCreativeTab, aList);
    }

    @Override
    public final IIcon getIconFromDamage(final int aMetaData) {
        if (aMetaData < 0) {
            return null;
        }
        if (aMetaData < 32000) {
            final Materials tMaterial = GregTechAPI.sGeneratedMaterials[aMetaData % 1000];
            if (tMaterial == null) {
                return null;
            }
            final IIconContainer tIcon = this.getIconContainer(aMetaData, tMaterial);
            if (tIcon != null) {
                return tIcon.getIcon();
            }
            return null;
        }
        return (aMetaData - 32000) < this.mIconList.length ? this.mIconList[aMetaData - 32000][0] : null;
    }

    @Override
    public int getItemStackLimit(final ItemStack aStack) {
        final int tDamage = this.getDamage(aStack);
        if ((tDamage < 32000) && (this.mGeneratedPrefixList[tDamage / 1000] != null)) {
            return Math
                .min(super.getItemStackLimit(aStack), this.mGeneratedPrefixList[tDamage / 1000].mDefaultStackSize);
        }
        return super.getItemStackLimit(aStack);
    }

    @Override
    public int getColorFromItemStack(final ItemStack stack, int HEX_OxFFFFFF) {

        int aMeta = stack.getItemDamage();
        if (stack.getDisplayName()
            .contains("Sodium")) {
            HEX_OxFFFFFF = Utils.rgbtoHexValue(90, 90, 255);
        } else if (stack.getDisplayName()
            .contains("Cadmium")) {
                HEX_OxFFFFFF = Utils.rgbtoHexValue(150, 150, 80);
            } else if (stack.getDisplayName()
                .contains("Lithium")) {
                    HEX_OxFFFFFF = Utils.rgbtoHexValue(225, 220, 255);
                } else if (stack.getDisplayName()
                    .contains("Wrought")) {
                        HEX_OxFFFFFF = Utils.rgbtoHexValue(200, 180, 180);
                    } else if (stack.getDisplayName()
                        .contains("Bronze")) {
                            HEX_OxFFFFFF = Utils.rgbtoHexValue(255, 128, 0);
                        } else if (stack.getDisplayName()
                            .contains("Brass")) {
                                HEX_OxFFFFFF = Utils.rgbtoHexValue(255, 180, 0);
                            } else if (stack.getDisplayName()
                                .contains("Invar")) {
                                    HEX_OxFFFFFF = Utils.rgbtoHexValue(180, 180, 120);
                                } else {
                                    if (aMeta > 50 && aMeta != 150) {
                                        HEX_OxFFFFFF = 0xffffff;
                                    } else if (stack.getDisplayName()
                                        .contains("ULV")) {
                                            HEX_OxFFFFFF = Utils.rgbtoHexValue(200, 180, 180);
                                        } else if (stack.getDisplayName()
                                            .contains("LuV")) {
                                                HEX_OxFFFFFF = 0xffffcc;
                                            } else if (stack.getDisplayName()
                                                .contains("ZPM")) {
                                                    HEX_OxFFFFFF = 0xffe600;
                                                } else if (stack.getDisplayName()
                                                    .contains("UV")) {
                                                        HEX_OxFFFFFF = 0xffb300;
                                                    } else if (stack.getDisplayName()
                                                        .contains("MAX")) {
                                                            HEX_OxFFFFFF = Utils.rgbtoHexValue(
                                                                MathUtils.randInt(220, 250),
                                                                MathUtils.randInt(221, 251),
                                                                MathUtils.randInt(220, 250));
                                                        } else {
                                                            HEX_OxFFFFFF = 0xffffff;
                                                        }
                                }
        return HEX_OxFFFFFF;
    }
}