-
Notifications
You must be signed in to change notification settings - Fork 497
/
ItemGate.java
executable file
·262 lines (214 loc) · 7.56 KB
/
ItemGate.java
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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.gates;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftTransport;
import buildcraft.api.gates.GateExpansions;
import buildcraft.api.gates.IGateExpansion;
import buildcraft.api.pipes.IPipePluggableRenderer;
import buildcraft.api.statements.StatementManager;
import buildcraft.api.pipes.IPipeContainer;
import buildcraft.core.ItemBuildCraft;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.utils.StringUtils;
import buildcraft.transport.Gate;
import buildcraft.transport.TileGenericPipe;
import buildcraft.transport.gates.GateDefinition.GateLogic;
import buildcraft.transport.gates.GateDefinition.GateMaterial;
public class ItemGate extends ItemBuildCraft {
protected static final String NBT_TAG_MAT = "mat";
protected static final String NBT_TAG_LOGIC = "logic";
protected static final String NBT_TAG_EX = "ex";
public ItemGate() {
super();
setHasSubtypes(false);
setMaxDamage(0);
setPassSneakClick(true);
}
private static NBTTagCompound getNBT(ItemStack stack) {
if (stack == null || !(stack.getItem() instanceof ItemGate)) {
return null;
} else {
return InvUtils.getItemData(stack);
}
}
public static void setMaterial(ItemStack stack, GateMaterial material) {
NBTTagCompound nbt = InvUtils.getItemData(stack);
nbt.setByte(NBT_TAG_MAT, (byte) material.ordinal());
}
public static GateMaterial getMaterial(ItemStack stack) {
NBTTagCompound nbt = getNBT(stack);
if (nbt == null) {
return GateMaterial.REDSTONE;
} else {
return GateMaterial.fromOrdinal(nbt.getByte(NBT_TAG_MAT));
}
}
public static GateLogic getLogic(ItemStack stack) {
NBTTagCompound nbt = getNBT(stack);
if (nbt == null) {
return GateLogic.AND;
} else {
return GateLogic.fromOrdinal(nbt.getByte(NBT_TAG_LOGIC));
}
}
public static void setLogic(ItemStack stack, GateLogic logic) {
NBTTagCompound nbt = InvUtils.getItemData(stack);
nbt.setByte(NBT_TAG_LOGIC, (byte) logic.ordinal());
}
public static void addGateExpansion(ItemStack stack, IGateExpansion expansion) {
NBTTagCompound nbt = getNBT(stack);
if (nbt == null) {
return;
}
NBTTagList expansionList = nbt.getTagList(NBT_TAG_EX, Constants.NBT.TAG_STRING);
expansionList.appendTag(new NBTTagString(expansion.getUniqueIdentifier()));
nbt.setTag(NBT_TAG_EX, expansionList);
}
public static boolean hasGateExpansion(ItemStack stack, IGateExpansion expansion) {
NBTTagCompound nbt = getNBT(stack);
if (nbt == null) {
return false;
}
try {
NBTTagList expansionList = nbt.getTagList(NBT_TAG_EX, Constants.NBT.TAG_STRING);
for (int i = 0; i < expansionList.tagCount(); i++) {
String ex = expansionList.getStringTagAt(i);
if (ex.equals(expansion.getUniqueIdentifier())) {
return true;
}
}
} catch (RuntimeException error) {
}
return false;
}
public static Set<IGateExpansion> getInstalledExpansions(ItemStack stack) {
Set<IGateExpansion> expansions = new HashSet<IGateExpansion>();
NBTTagCompound nbt = getNBT(stack);
if (nbt == null) {
return expansions;
}
try {
NBTTagList expansionList = nbt.getTagList(NBT_TAG_EX, Constants.NBT.TAG_STRING);
for (int i = 0; i < expansionList.tagCount(); i++) {
String exTag = expansionList.getStringTagAt(i);
IGateExpansion ex = GateExpansions.getExpansion(exTag);
if (ex != null) {
expansions.add(ex);
}
}
} catch (RuntimeException error) {
}
return expansions;
}
public static ItemStack makeGateItem(GateMaterial material, GateLogic logic) {
ItemStack stack = new ItemStack(BuildCraftTransport.pipeGate);
NBTTagCompound nbt = InvUtils.getItemData(stack);
nbt.setByte(NBT_TAG_MAT, (byte) material.ordinal());
nbt.setByte(NBT_TAG_LOGIC, (byte) logic.ordinal());
return stack;
}
public static ItemStack makeGateItem(Gate gate) {
ItemStack stack = new ItemStack(BuildCraftTransport.pipeGate);
NBTTagCompound nbt = InvUtils.getItemData(stack);
nbt.setByte(NBT_TAG_MAT, (byte) gate.material.ordinal());
nbt.setByte(NBT_TAG_LOGIC, (byte) gate.logic.ordinal());
for (IGateExpansion expansion : gate.expansions.keySet()) {
addGateExpansion(stack, expansion);
}
return stack;
}
@Override
public String getUnlocalizedName(ItemStack stack) {
return GateDefinition.getLocalizedName(getMaterial(stack), getLogic(stack));
}
@Override
public String getItemStackDisplayName(ItemStack stack) {
return ("" + StatCollector.translateToLocal(this.getUnlocalizedNameInefficiently(stack))).trim();
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs tab, List itemList) {
for (GateMaterial material : GateMaterial.VALUES) {
for (GateLogic logic : GateLogic.VALUES) {
if (material == GateMaterial.REDSTONE && logic == GateLogic.OR) {
continue;
}
itemList.add(makeGateItem(material, logic));
for (IGateExpansion exp : GateExpansions.getExpansions()) {
ItemStack stackExpansion = makeGateItem(material, logic);
addGateExpansion(stackExpansion, exp);
itemList.add(stackExpansion);
}
}
}
}
public static ItemStack[] getGateVarients() {
ArrayList<ItemStack> gates = new ArrayList<ItemStack>();
for (GateMaterial material : GateMaterial.VALUES) {
for (GateLogic logic : GateLogic.VALUES) {
if (material == GateMaterial.REDSTONE && logic == GateLogic.OR) {
continue;
}
gates.add(makeGateItem(material, logic));
}
}
return gates.toArray(new ItemStack[gates.size()]);
}
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean adv) {
super.addInformation(stack, player, list, adv);
list.add(StringUtils.localize("tip.gate.wires"));
list.add(StringUtils.localize("tip.gate.wires." + getMaterial(stack).getTag()));
Set<IGateExpansion> expansions = getInstalledExpansions(stack);
if (!expansions.isEmpty()) {
list.add(StringUtils.localize("tip.gate.expansions"));
for (IGateExpansion expansion : expansions) {
list.add(expansion.getDisplayName());
}
}
}
@Override
public IIcon getIconIndex(ItemStack stack) {
return getLogic(stack).getIconItem();
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iconRegister) {
for (GateDefinition.GateMaterial material : GateDefinition.GateMaterial.VALUES) {
material.registerItemIcon(iconRegister);
}
for (GateDefinition.GateLogic logic : GateDefinition.GateLogic.VALUES) {
logic.registerItemIcon(iconRegister);
}
for (IGateExpansion expansion : GateExpansions.getExpansions()) {
expansion.registerItemOverlay(iconRegister);
}
StatementManager.registerIcons(iconRegister);
}
}