-
Notifications
You must be signed in to change notification settings - Fork 497
/
ItemGate.java
396 lines (329 loc) · 11.5 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* 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.gates.IStatement;
import buildcraft.api.gates.StatementManager;
import buildcraft.api.transport.IPipePluggable;
import buildcraft.api.transport.IPipeTile;
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 {
public static class GatePluggable implements IPipePluggable {
public GateMaterial material;
public GateLogic logic;
public IGateExpansion[] expansions;
public GatePluggable() {
}
public GatePluggable(Gate gate) {
this.material = gate.material;
this.logic = gate.logic;
Set<IGateExpansion> gateExpansions = gate.expansions.keySet();
this.expansions = gateExpansions.toArray(new IGateExpansion[gateExpansions.size()]);
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
nbt.setByte(NBT_TAG_MAT, (byte) material.ordinal());
nbt.setByte(NBT_TAG_LOGIC, (byte) logic.ordinal());
NBTTagList expansionsList = nbt.getTagList(NBT_TAG_EX, Constants.NBT.TAG_STRING);
for (IGateExpansion expansion : expansions) {
expansionsList.appendTag(new NBTTagString(expansion.getUniqueIdentifier()));
}
nbt.setTag(NBT_TAG_EX, expansionsList);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
material = GateMaterial.fromOrdinal(nbt.getByte(NBT_TAG_MAT));
logic = GateLogic.fromOrdinal(nbt.getByte(NBT_TAG_LOGIC));
NBTTagList expansionsList = nbt.getTagList(NBT_TAG_EX, Constants.NBT.TAG_STRING);
final int expansionsSize = expansionsList.tagCount();
expansions = new IGateExpansion[expansionsSize];
for (int i = 0; i < expansionsSize; i++) {
expansions[i] = GateExpansions.getExpansion(expansionsList.getStringTagAt(i));
}
}
public void writeToByteByf(ByteBuf buf) {
buf.writeByte(material.ordinal());
buf.writeByte(logic.ordinal());
final int expansionsSize = expansions.length;
buf.writeInt(expansionsSize);
for (IGateExpansion expansion : expansions) {
buf.writeByte(GateExpansions.getServerExpansionID(expansion.getUniqueIdentifier()));
}
}
public void readFromByteBuf(ByteBuf buf) {
material = GateMaterial.fromOrdinal(buf.readByte());
logic = GateLogic.fromOrdinal(buf.readByte());
final int expansionsSize = buf.readInt();
expansions = new IGateExpansion[expansionsSize];
for (int i = 0; i < expansionsSize; i++) {
expansions[i] = GateExpansions.getExpansionClient(buf.readByte());
}
}
@Override
public ItemStack[] getDropItems(IPipeTile pipe) {
ItemStack gate = makeGateItem(material, logic);
for (IGateExpansion expansion : expansions) {
addGateExpansion(gate, expansion);
}
return new ItemStack[] { gate };
}
@Override
public void onAttachedPipe(IPipeTile pipe, ForgeDirection direction) {
TileGenericPipe pipeReal = (TileGenericPipe) pipe;
if (!pipeReal.getWorld().isRemote) {
Gate gate = pipeReal.pipe.gates[direction.ordinal()];
if (gate == null || gate.material != material || gate.logic != logic) {
pipeReal.pipe.gates[direction.ordinal()] = GateFactory.makeGate(pipeReal.pipe, material, logic, direction);
pipeReal.scheduleRenderUpdate();
}
}
}
@Override
public void onDetachedPipe(IPipeTile pipe, ForgeDirection direction) {
TileGenericPipe pipeReal = (TileGenericPipe) pipe;
if (!pipeReal.getWorld().isRemote) {
Gate gate = pipeReal.pipe.gates[direction.ordinal()];
if (gate != null) {
gate.resetGate();
pipeReal.pipe.gates[direction.ordinal()] = null;
}
pipeReal.scheduleRenderUpdate();
}
}
@Override
public boolean blocking(IPipeTile pipe, ForgeDirection direction) {
return true;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof GatePluggable)) {
return false;
}
GatePluggable o = (GatePluggable) obj;
if (o.material.ordinal() != material.ordinal()) {
return false;
}
if (o.logic.ordinal() != logic.ordinal()) {
return false;
}
if (o.expansions.length != expansions.length) {
return false;
}
for (int i = 0; i < expansions.length; i++) {
if (o.expansions[i] != expansions[i]) {
return false;
}
}
return true;
}
}
private static final String NBT_TAG_MAT = "mat";
private static final String NBT_TAG_LOGIC = "logic";
private 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);
}
for (IStatement statement : StatementManager.statements.values()) {
statement.registerIcons(iconRegister);
}
}
}