aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/xmod/gregtech/api/items/Gregtech_MetaItem.java
blob: 12b90c9a6ceaae14eb46cb46fbdc23c433b3b078 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package gtPlusPlus.xmod.gregtech.api.items;

import static gregtech.api.enums.GT_Values.D1;

import java.util.*;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import gregtech.api.GregTech_API;
import gregtech.api.enums.Materials;
import gregtech.api.enums.SubTag;
import gregtech.api.enums.TC_Aspects.TC_AspectStack;
import gregtech.api.interfaces.IFoodStat;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.IItemContainer;
import gregtech.api.objects.ItemData;
import gregtech.api.util.GT_LanguageManager;
import gregtech.api.util.GT_OreDictUnificator;
import gregtech.api.util.GT_Utility;

import gtPlusPlus.core.creative.AddToCreativeTab;
import gtPlusPlus.core.lib.CORE;
import gtPlusPlus.xmod.gregtech.api.interfaces.internal.Interface_ItemBehaviour;

public abstract class Gregtech_MetaItem extends Gregtech_MetaItem_Base {
	/**
	 * All instances of this Item Class are listed here.
	 * This gets used to register the Renderer to all Items of this Type, if useStandardMetaItemRenderer() returns true.
	 * <p/>
	 * You can also use the unlocalized Name gotten from getUnlocalizedName() as Key if you want to get a specific Item.
	 */
	public static final HashMap<String, Gregtech_MetaItem> sInstances = new HashMap<>();

	/* ---------- CONSTRUCTOR AND MEMBER VARIABLES ---------- */

	public final short mOffset, mItemAmount;
	public final BitSet mEnabledItems;
	public final BitSet mVisibleItems;
	public final IIcon[][] mIconList;

	public final HashMap<Short, IFoodStat> mFoodStats = new HashMap<>();
	public final HashMap<Short, Long[]> mElectricStats = new HashMap<>();
	public final HashMap<Short, Long[]> mFluidContainerStats = new HashMap<>();
	public final HashMap<Short, Short> mBurnValues = new HashMap<>();

	/**
	 * Creates the Item using these Parameters.
	 *
	 * @param aUnlocalized The Unlocalized Name of this Item.
	 */
	public Gregtech_MetaItem(final String aUnlocalized, final short aOffset, final short aItemAmount) {
		super(aUnlocalized);
		this.setCreativeTab(AddToCreativeTab.tabMachines);
		this.setHasSubtypes(true);
		this.setMaxDamage(0);
		this.mEnabledItems = new BitSet(aItemAmount);
		this.mVisibleItems = new BitSet(aItemAmount);

		this.mOffset = (short) Math.min(32766, aOffset);
		this.mItemAmount = (short) Math.min(aItemAmount, 32766 - this.mOffset);
		this.mIconList = new IIcon[aItemAmount][1];

		sInstances.put(this.getUnlocalizedName(), this);
	}

	/**
	 * This adds a Custom Item to the ending Range.
	 *
	 * @param aID           The Id of the assigned Item [0 - mItemAmount] (The MetaData gets auto-shifted by +mOffset)
	 * @param aEnglish      The Default Localized Name of the created Item
	 * @param aToolTip      The Default ToolTip of the created Item, you can also insert null for having no ToolTip
	 * @param aFoodBehavior The Food Value of this Item. Can be null aswell. Just a convenience thing.
	 * @param aRandomData   The OreDict Names you want to give the Item. Also used for TC Aspects and some other things.
	 * @return An ItemStack containing the newly created Item.
	 */
	public final ItemStack addItem(final int aID, final String aEnglish, String aToolTip, final Object... aRandomData) {
		if (aToolTip == null) {
			aToolTip = "";
		}
		if ((aID >= 0) && (aID < this.mItemAmount)) {
			final ItemStack rStack = new ItemStack(this, 1, this.mOffset + aID);
			this.mEnabledItems.set(aID);
			this.mVisibleItems.set(aID);
			GT_LanguageManager.addStringLocalization(this.getUnlocalizedName(rStack) + ".name", aEnglish);
			GT_LanguageManager.addStringLocalization(this.getUnlocalizedName(rStack) + ".tooltip", aToolTip);
			final List<TC_AspectStack> tAspects = new ArrayList<>();
			// Important Stuff to do first
			for (final Object tRandomData : aRandomData) {
				if (tRandomData instanceof SubTag) {
					if (tRandomData == SubTag.INVISIBLE) {
						this.mVisibleItems.set(aID, false);
						continue;
					}
					if (tRandomData == SubTag.NO_UNIFICATION) {
						GT_OreDictUnificator.addToBlacklist(rStack);
						continue;
					}
				}
			}
			// now check for the rest
			for (final Object tRandomData : aRandomData) {
				if (tRandomData != null) {
					boolean tUseOreDict = true;

					if (tRandomData instanceof Interface_ItemBehaviour) {
						this.addItemBehavior(this.mOffset + aID, (Interface_ItemBehaviour<Gregtech_MetaItem_Base>) tRandomData);
						tUseOreDict = false;
					}
					if (tRandomData instanceof IItemContainer) {
						((IItemContainer) tRandomData).set(rStack);
						tUseOreDict = false;
					}
					if (tRandomData instanceof SubTag) {
						continue;
					}
					if (tRandomData instanceof TC_AspectStack) {
						((TC_AspectStack) tRandomData).addToAspectList(tAspects);
						continue;
					}
					if (tRandomData instanceof ItemData) {
						if (GT_Utility.isStringValid(tRandomData)) {
							GT_OreDictUnificator.registerOre(tRandomData, rStack);
						} else {
							GT_OreDictUnificator.addItemData(rStack, (ItemData) tRandomData);
						}
						continue;
					}
					if (tUseOreDict) {
						GT_OreDictUnificator.registerOre(tRandomData, rStack);
						continue;
					}
				}
			}
			if (GregTech_API.sThaumcraftCompat != null) {
				GregTech_API.sThaumcraftCompat.registerThaumcraftAspectsToItem(rStack, tAspects, false);
			}
			return rStack;
		}
		return null;
	}

	/**
	 * Sets the Furnace Burn Value for the Item.
	 *
	 * @param aMetaValue the Meta Value of the Item you want to set it to. [0 - 32765]
	 * @param aValue     200 = 1 Burn Process = 500 EU, max = 32767 (that is 81917.5 EU)
	 * @return the Item itself for convenience in constructing.
	 */
	public final Gregtech_MetaItem setBurnValue(final int aMetaValue, final int aValue) {
		if ((aMetaValue < 0) || (aMetaValue >= (this.mOffset + this.mEnabledItems.length())) || (aValue < 0)) {
			return this;
		}
		if (aValue == 0) {
			this.mBurnValues.remove((short) aMetaValue);
		} else {
			this.mBurnValues.put((short) aMetaValue, aValue > Short.MAX_VALUE ? Short.MAX_VALUE : (short) aValue);
		}
		return this;
	}

	/**
	 * @param aMetaValue     the Meta Value of the Item you want to set it to. [0 - 32765]
	 * @param aMaxCharge     Maximum Charge. (if this is == 0 it will remove the Electric Behavior)
	 * @param aTransferLimit Transfer Limit.
	 * @param aTier          The electric Tier.
	 * @param aSpecialData   If this Item has a Fixed Charge, like a SingleUse Battery (if > 0).
	 *                       Use -1 if you want to make this Battery chargeable (the use and canUse Functions will still discharge if you just use this)
	 *                       Use -2 if you want to make this Battery dischargeable.
	 *                       Use -3 if you want to make this Battery charge/discharge-able.
	 * @return the Item itself for convenience in constructing.
	 */
	public final Gregtech_MetaItem setElectricStats(final int aMetaValue, final long aMaxCharge, final long aTransferLimit, final long aTier, final long aSpecialData, final boolean aUseAnimations) {
		if ((aMetaValue < 0) || (aMetaValue >= (this.mOffset + this.mEnabledItems.length()))) {
			return this;
		}
		if (aMaxCharge == 0) {
			this.mElectricStats.remove((short) aMetaValue);
		} else {
			this.mElectricStats.put((short) aMetaValue, new Long[]{aMaxCharge, Math.max(0, aTransferLimit), Math.max(-1, aTier), aSpecialData});
			if ((aMetaValue >= this.mOffset) && aUseAnimations) {
				this.mIconList[aMetaValue - this.mOffset] = Arrays.copyOf(this.mIconList[aMetaValue - this.mOffset], Math.max(9, this.mIconList[aMetaValue - this.mOffset].length));
			}
		}
		return this;
	}

	/**
	 * @param aMetaValue     the Meta Value of the Item you want to set it to. [0 - 32765]
	 * @param aMaxCharge     Maximum Charge. (if this is == 0 it will remove the Electric Behavior)
	 * @param aTransferLimit Transfer Limit.
	 * @param aTier          The electric Tier.
	 * @param aSpecialData   If this Item has a Fixed Charge, like a SingleUse Battery (if > 0).
	 *                       Use -1 if you want to make this Battery chargeable (the use and canUse Functions will still discharge if you just use this)
	 *                       Use -2 if you want to make this Battery dischargeable.
	 *                       Use -3 if you want to make this Battery charge/discharge-able.
	 * @return the Item itself for convenience in constructing.
	 */
	public final Gregtech_MetaItem setFluidContainerStats(final int aMetaValue, final long aCapacity, final long aStacksize) {
		if ((aMetaValue < 0) || (aMetaValue >= (this.mOffset + this.mEnabledItems.length()))) {
			return this;
		}
		if (aCapacity < 0) {
			this.mElectricStats.remove((short) aMetaValue);
		} else {
			this.mFluidContainerStats.put((short) aMetaValue, new Long[]{aCapacity, Math.max(1, aStacksize)});
		}
		return this;
	}

	/**
	 * @return if this MetaGenerated Item should use my Default Renderer System.
	 */
	public boolean useStandardMetaItemRenderer() {
		return true;
	}

	/**
	 * @return the Color Modulation the Material is going to be rendered with.
	 */
	public short[] getRGBa(final ItemStack aStack) {
		return Materials._NULL.getRGBA();
	}

	/**
	 * @return the Icon the Material is going to be rendered with.
	 */
	public IIconContainer getIconContainer(final int aMetaData) {
		return null;
	}

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

	@Override
	public ItemStack onItemRightClick(final ItemStack aStack, final World aWorld, final EntityPlayer aPlayer) {
		return super.onItemRightClick(aStack, aWorld, aPlayer);
	}

	@Override
	public int getMaxItemUseDuration(final ItemStack aStack) {
		return this.mFoodStats.get((short) this.getDamage(aStack)) == null ? 0 : 32;
	}

	@Override
	public EnumAction getItemUseAction(final ItemStack aStack) {
		return  EnumAction.none;
	}

	@Override
	public final ItemStack onEaten(final ItemStack aStack, final World aWorld, final EntityPlayer aPlayer) {
		final IFoodStat tStat = this.mFoodStats.get((short) this.getDamage(aStack));
		if (tStat != null) {

		}
		return aStack;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void getSubItems(final Item var1, final CreativeTabs aCreativeTab, final List aList) {
		for (int i = 0, j = this.mEnabledItems.length(); i < j; i++) {
			if (this.mVisibleItems.get(i) || (D1 && this.mEnabledItems.get(i))) {
				final Long[] tStats = this.mElectricStats.get((short) (this.mOffset + i));
				if ((tStats != null) && (tStats[3] < 0)) {
					final ItemStack tStack = new ItemStack(this, 1, this.mOffset + i);
					this.setCharge(tStack, Math.abs(tStats[0]));
					this.isItemStackUsable(tStack);
					aList.add(tStack);
				}
				if ((tStats == null) || (tStats[3] != -2)) {
					final ItemStack tStack = new ItemStack(this, 1, this.mOffset + i);
					this.isItemStackUsable(tStack);
					aList.add(tStack);
				}
			}
		}
	}

	@Override
	@SideOnly(Side.CLIENT)
	public final void registerIcons(final IIconRegister aIconRegister) {
		for (short i = 0, j = (short) this.mEnabledItems.length(); i < j; i++) {
			if (this.mEnabledItems.get(i)) {
				for (byte k = 1; k < this.mIconList[i].length; k++) {
					this.mIconList[i][k] = aIconRegister.registerIcon(CORE.MODID+":"+this.getUnlocalizedName() + "/" + i + "/" + k);
				}
				this.mIconList[i][0] = aIconRegister.registerIcon(CORE.MODID+":"+this.getUnlocalizedName() + "/" + i);
			}
		}
	}

	@Override
	public final Long[] getElectricStats(final ItemStack aStack) {
		return this.mElectricStats.get((short) aStack.getItemDamage());
	}

	@Override
	public final Long[] getFluidContainerStats(final ItemStack aStack) {
		return this.mFluidContainerStats.get((short) aStack.getItemDamage());
	}

	@Override
	public int getItemEnchantability() {
		return 0;
	}

	@Override
	public boolean isBookEnchantable(final ItemStack aStack, final ItemStack aBook) {
		return false;
	}

	@Override
	public boolean getIsRepairable(final ItemStack aStack, final ItemStack aMaterial) {
		return false;
	}

	@Override
	public int getColorFromItemStack(final ItemStack stack, int HEX_OxFFFFFF) {
		if (stack.getDisplayName().contains("LuV")){
			HEX_OxFFFFFF = 0xffffcc;
		}
		else if (stack.getDisplayName().contains("ZPM")){
			HEX_OxFFFFFF = 0xace600;
		}
		else if (stack.getDisplayName().contains("UV")){
			HEX_OxFFFFFF = 0xffff00;
		}
		else if (stack.getDisplayName().contains("MAX")){
			HEX_OxFFFFFF = 0xff0000;
		}
		else {
			HEX_OxFFFFFF = 0xffffff;
		}
		return HEX_OxFFFFFF;
	}
}