-
Notifications
You must be signed in to change notification settings - Fork 497
/
SchematicPipe.java
288 lines (229 loc) · 10.3 KB
/
SchematicPipe.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
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* 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.schematics;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.blueprints.*;
import buildcraft.api.statements.IStatement;
import buildcraft.api.statements.IStatementParameter;
import buildcraft.api.statements.StatementManager;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.Gate;
import buildcraft.transport.Pipe;
import buildcraft.transport.TileGenericPipe.SideProperties;
public class SchematicPipe extends SchematicTile {
private BuildingPermission permission = BuildingPermission.ALL;
@Override
public boolean isAlreadyBuilt(IBuilderContext context, BlockPos pos) {
Pipe<?> pipe = BlockGenericPipe.getPipe(context.world(), pos);
if (BlockGenericPipe.isValid(pipe)) {
if (tileNBT.hasKey("pipeId", Constants.NBT.TAG_INT)) {
return pipe.item == Item.getItemById(tileNBT.getInteger("pipeId"));
} else {
return pipe.item == Item.getByNameOrId(tileNBT.getString("pipeId"));
}
} else {
return false;
}
}
@Override
public void rotateLeft(IBuilderContext context) {
SideProperties props = new SideProperties();
props.readFromNBT(tileNBT);
props.rotateLeft();
props.writeToNBT(tileNBT);
Item pipeItem;
if (tileNBT.hasKey("pipeId", Constants.NBT.TAG_INT)) {
pipeItem = Item.getItemById(tileNBT.getInteger("pipeId"));
} else {
pipeItem = Item.getByNameOrId(tileNBT.getString("pipeId"));
}
if (BptPipeExtension.contains(pipeItem)) {
BptPipeExtension.get(pipeItem).rotateLeft(this, context);
}
if (tileNBT.hasKey("Gate")) {
// This code is handling specifically BuildCraft 6.0 gates. Sided
// gates starting BuildCraft 6.1 work differently.
NBTTagCompound gateNBT = tileNBT.getCompoundTag("Gate");
rotateGateLeft(gateNBT);
} else {
// Post 6.1 treatment
NBTTagCompound[] gatesNBT = new NBTTagCompound[6];
for (int i = 0; i < 6; ++i) {
if (tileNBT.hasKey("Gate[" + i + "]")) {
gatesNBT[i] = tileNBT.getCompoundTag("Gate[" + i + "]");
}
}
for (int i = 0; i < 6; ++i) {
EnumFacing face = EnumFacing.VALUES[i];
if (face.getAxis() != Axis.Y) {
face = face.rotateY();
}
int newI = face.ordinal();
if (gatesNBT[i] != null) {
rotateGateLeft(gatesNBT[i]);
tileNBT.setTag("Gate[" + newI + "]", gatesNBT[i]);
} else {
tileNBT.removeTag("Gate[" + newI + "]");
}
}
}
}
private void rotateGateLeft(NBTTagCompound gateNBT) {
for (int i = 0; i < Gate.MAX_STATEMENTS; ++i) {
if (gateNBT.hasKey("trigger[" + i + "]")) {
IStatement t = StatementManager.statements.get(gateNBT.getString("trigger[" + i + "]"));
t = t.rotateLeft();
gateNBT.setString("trigger[" + i + "]", t.getUniqueTag());
}
if (gateNBT.hasKey("action[" + i + "]")) {
IStatement a = StatementManager.statements.get(gateNBT.getString("action[" + i + "]"));
a = a.rotateLeft();
gateNBT.setString("action[" + i + "]", a.getUniqueTag());
}
for (int j = 0; j < Gate.MAX_PARAMETERS; ++j) {
if (gateNBT.hasKey("triggerParameters[" + i + "][" + j + "]")) {
NBTTagCompound cpt = gateNBT.getCompoundTag("triggerParameters[" + i + "][" + j + "]");
IStatementParameter parameter = StatementManager.createParameter(cpt.getString("kind"));
parameter.readFromNBT(cpt);
parameter = parameter.rotateLeft();
parameter.writeToNBT(cpt);
gateNBT.setTag("triggerParameters[" + i + "][" + j + "]", cpt);
}
if (gateNBT.hasKey("actionParameters[" + i + "][" + j + "]")) {
NBTTagCompound cpt = gateNBT.getCompoundTag("actionParameters[" + i + "][" + j + "]");
IStatementParameter parameter = StatementManager.createParameter(cpt.getString("kind"));
parameter.readFromNBT(cpt);
parameter = parameter.rotateLeft();
parameter.writeToNBT(cpt);
gateNBT.setTag("actionParameters[" + i + "][" + j + "]", cpt);
}
}
}
if (gateNBT.hasKey("direction")) {
EnumFacing face = EnumFacing.VALUES[gateNBT.getInteger("direction")];
if (face.getAxis() != Axis.Y) {
face = face.rotateY();
}
gateNBT.setInteger("direction", face.ordinal());
}
}
@Override
public void placeInWorld(IBuilderContext context, BlockPos pos, List<ItemStack> stacks) {
tileNBT.setInteger("x", pos.getX());
tileNBT.setInteger("y", pos.getY());
tileNBT.setInteger("z", pos.getZ());
context.world().setBlockState(pos, state, 3);
TileEntity tile = context.world().getTileEntity(pos);
tile.setWorldObj(context.world());
tile.readFromNBT(tileNBT);
}
@Override
public void initializeFromObjectAt(IBuilderContext context, BlockPos pos) {
TileEntity tile = context.world().getTileEntity(pos);
Pipe<?> pipe = BlockGenericPipe.getPipe(context.world(), pos);
if (BlockGenericPipe.isValid(pipe)) {
tile.writeToNBT(tileNBT);
// remove all pipe contents
tileNBT.removeTag("travelingEntities");
for (EnumFacing direction : EnumFacing.values()) {
tileNBT.removeTag("tank[" + direction.ordinal() + "]");
tileNBT.removeTag("transferState[" + direction.ordinal() + "]");
}
for (int i = 0; i < 6; ++i) {
tileNBT.removeTag("powerQuery[" + i + "]");
tileNBT.removeTag("nextPowerQuery[" + i + "]");
tileNBT.removeTag("internalPower[" + i + "]");
tileNBT.removeTag("internalNextPower[" + i + "]");
}
}
}
@Override
public void storeRequirements(IBuilderContext context, BlockPos pos) {
Pipe<?> pipe = BlockGenericPipe.getPipe(context.world(), pos);
if (BlockGenericPipe.isValid(pipe)) {
ArrayList<ItemStack> items = pipe.computeItemDrop();
storedRequirements = new ItemStack[items.size() + 1];
items.toArray(storedRequirements);
storedRequirements[storedRequirements.length - 1] = new ItemStack(pipe.item, 1, pipe.container.getItemMetadata());
}
}
@Override
public void getRequirementsForPlacement(IBuilderContext context, List<ItemStack> requirements) {
}
@Override
public void postProcessing(IBuilderContext context, BlockPos pos) {
Item pipeItem = Item.getItemById(tileNBT.getInteger("pipeId"));
if (BptPipeExtension.contains(pipeItem)) {
BptPipeExtension.get(pipeItem).postProcessing(this, context);
}
}
@Override
public BuildingStage getBuildStage() {
return BuildingStage.STANDALONE;
}
@Override
public void idsToBlueprint(MappingRegistry registry) {
super.idsToBlueprint(registry);
if (tileNBT.hasKey("pipeId", Constants.NBT.TAG_INT)) {
Item item = Item.getItemById(tileNBT.getInteger("pipeId"));
tileNBT.setInteger("pipeId", registry.getIdForItem(item));
} else if (tileNBT.hasKey("pipeId")) {
Item item = Item.getByNameOrId(tileNBT.getString("pipeId"));
tileNBT.setInteger("pipeId", registry.getIdForItem(item));
}
}
@Override
public void idsToWorld(MappingRegistry registry) {
super.idsToWorld(registry);
if (tileNBT.hasKey("pipeId")) {
try {
Item item = registry.getItemForId(tileNBT.getInteger("pipeId"));
tileNBT.setString("pipeId", Item.itemRegistry.getNameForObject(item).toString());
} catch (MappingNotFoundException e) {
tileNBT.removeTag("pipeId");
}
}
}
@Override
public void writeSchematicToNBT(NBTTagCompound nbt, MappingRegistry registry) {
super.writeSchematicToNBT(nbt, registry);
nbt.setInteger("version", 2);
}
@Override
public void readSchematicFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
super.readSchematicFromNBT(nbt, registry);
if (!nbt.hasKey("version") || nbt.getInteger("version") < 2) {
// Schematics previous to the fixes in version 2 had item id
// translation badly broken. We need to flush out information that
// would be otherwise corrupted - that is the inventory (with the
// old formalism "items") and gate parameters.
tileNBT.removeTag("items");
if (tileNBT.hasKey("Gate")) {
NBTTagCompound gateNBT = tileNBT.getCompoundTag("Gate");
for (int i = 0; i < 8; ++i) {
if (gateNBT.hasKey("triggerParameters[" + i + "]")) {
NBTTagCompound parameterNBT = gateNBT.getCompoundTag("triggerParameters[" + i + "]");
if (parameterNBT.hasKey("stack")) {
parameterNBT.removeTag("stack");
}
}
}
}
}
}
@Override
public BuildingPermission getBuildingPermission() {
return permission;
}
}