-
Notifications
You must be signed in to change notification settings - Fork 497
/
GateFactory.java
120 lines (103 loc) · 3.15 KB
/
GateFactory.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
/**
* 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 net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.gates.GateExpansionController;
import buildcraft.api.gates.GateExpansions;
import buildcraft.api.gates.IGateExpansion;
import buildcraft.transport.Gate;
import buildcraft.transport.Pipe;
import buildcraft.transport.gates.GateDefinition.GateLogic;
import buildcraft.transport.gates.GateDefinition.GateMaterial;
public final class GateFactory {
/**
* Deactivate constructor
*/
private GateFactory() {
}
public static Gate makeGate(Pipe pipe, GateMaterial material, GateLogic logic) {
return new Gate(pipe, material, logic);
}
public static Gate makeGate(Pipe pipe, ItemStack stack) {
if (stack == null || stack.stackSize <= 0 || !(stack.getItem() instanceof ItemGate)) {
return null;
}
Gate gate = makeGate(pipe, ItemGate.getMaterial(stack), ItemGate.getLogic(stack));
for (IGateExpansion expansion : ItemGate.getInstalledExpansions(stack)) {
gate.addGateExpansion(expansion);
}
return gate;
}
public static Gate makeGate(Pipe pipe, NBTTagCompound nbt) {
GateMaterial material = GateMaterial.REDSTONE;
GateLogic logic = GateLogic.AND;
// Legacy Support
if (nbt.hasKey("Kind")) {
int kind = nbt.getInteger("Kind");
switch (kind) {
case 1:
case 2:
material = GateMaterial.IRON;
break;
case 3:
case 4:
material = GateMaterial.GOLD;
break;
case 5:
case 6:
material = GateMaterial.DIAMOND;
break;
}
switch (kind) {
case 2:
case 4:
case 6:
logic = GateLogic.OR;
break;
}
}
if (nbt.hasKey("material")) {
try {
material = GateMaterial.valueOf(nbt.getString("material"));
} catch (IllegalArgumentException ex) {
return null;
}
}
if (nbt.hasKey("logic")) {
try {
logic = GateLogic.valueOf(nbt.getString("logic"));
} catch (IllegalArgumentException ex) {
return null;
}
}
Gate gate = makeGate(pipe, material, logic);
gate.readFromNBT(nbt);
// Legacy support
if (nbt.hasKey("Pulser")) {
NBTTagCompound pulsarTag = nbt.getCompoundTag("Pulser");
GateExpansionController pulsarCon = GateExpansionPulsar.INSTANCE.makeController(pipe.container);
pulsarCon.readFromNBT(pulsarTag);
gate.expansions.put(GateExpansionPulsar.INSTANCE, pulsarCon);
}
NBTTagList exList = nbt.getTagList("expansions", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < exList.tagCount(); i++) {
NBTTagCompound conNBT = exList.getCompoundTagAt(i);
IGateExpansion ex = GateExpansions.getExpansion(conNBT.getString("type"));
if (ex != null) {
GateExpansionController con = ex.makeController(pipe.container);
con.readFromNBT(conNBT.getCompoundTag("data"));
gate.expansions.put(ex, con);
}
}
return gate;
}
}