-
Notifications
You must be signed in to change notification settings - Fork 497
/
BptBlueprint.java
353 lines (277 loc) · 9.02 KB
/
BptBlueprint.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
/**
* 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.core.blueprints;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.TreeSet;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import buildcraft.api.blueprints.IBptContext;
import buildcraft.core.IBptContributor;
import buildcraft.core.utils.BCLog;
public class BptBlueprint extends BptBase {
//private int[] idMapping = new int[Item.itemsList.length];
TreeSet<Integer> idsToMap = new TreeSet<Integer>();
public BptBlueprint() {
//for (int i = 0; i < idMapping.length; ++i) {
// idMapping[i] = i;
//}
}
public BptBlueprint(int sizeX, int sizeY, int sizeZ) {
super(sizeX, sizeY, sizeZ);
//for (int i = 0; i < idMapping.length; ++i) {
// idMapping[i] = i;
//}
}
public void readFromWorld(IBptContext context, TileEntity anchorTile, int x, int y, int z) {
BptSlot slot = new BptSlot();
slot.x = (int) (x - context.surroundingBox().pMin().x);
slot.y = (int) (y - context.surroundingBox().pMin().y);
slot.z = (int) (z - context.surroundingBox().pMin().z);
slot.block = anchorTile.getWorldObj().getBlock(x, y, z);
slot.meta = anchorTile.getWorldObj().getBlockMetadata(x, y, z);
if (slot.block instanceof BlockContainer) {
TileEntity tile = anchorTile.getWorldObj().getTileEntity(x, y, z);
if (tile != null && tile instanceof IBptContributor) {
IBptContributor contributor = (IBptContributor) tile;
contributor.saveToBluePrint(anchorTile, this, slot);
}
}
try {
slot.initializeFromWorld(context, x, y, z);
contents[slot.x][slot.y][slot.z] = slot;
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBlueprint", "readFromWorld", t);
}
}
@Override
public void saveAttributes(BufferedWriter writer) throws IOException {
/*writer.write("sizeX:" + sizeX);
writer.newLine();
writer.write("sizeY:" + sizeY);
writer.newLine();
writer.write("sizeZ:" + sizeZ);
writer.newLine();
writer.write("anchorX:" + anchorX);
writer.newLine();
writer.write("anchorY:" + anchorY);
writer.newLine();
writer.write("anchorZ:" + anchorZ);
writer.newLine();
boolean[] idsUsed = new boolean[Item.itemsList.length];
for (int i = 1; i < idsUsed.length; ++i) {
idsUsed[i] = false;
}
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
BptSlotInfo slot = contents[x][y][z];
storeId(slot.blockId);
for (ItemStack stack : slot.storedRequirements) {
storeId(stack.itemID);
}
}
}
}
writer.write("idMap:");
writer.newLine();
for (Integer id : idsToMap) {
if (id < Block.blocksList.length && Block.blocksList[id] != null) {
writer.write(BlueprintManager.getBlockSignature(Block.blocksList[id]) + "=" + id);
} else {
writer.write(BlueprintManager.getItemSignature(Item.itemsList[id]) + "=" + id);
}
writer.newLine();
}
writer.write(":idMap");
writer.newLine();
writer.write("contents:");
writer.newLine();
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
BptSlotInfo slot = contents[x][y][z];
if (slot != null && slot.blockId != 0) {
slot.cpt.setInteger("bId", slot.blockId);
if (slot.meta != 0) {
slot.cpt.setInteger("meta", slot.meta);
}
NBTBase.writeNamedTag(slot.cpt, new BptDataStream(writer));
writer.newLine();
} else {
writer.newLine();
}
}
}
}
writer.write(":contents");
writer.newLine();
writer.write("requirements:");
writer.newLine();
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
BptSlotInfo slot = contents[x][y][z];
if (slot != null && slot.blockId != 0 && slot.storedRequirements.size() > 0) {
NBTTagList list = new NBTTagList();
for (ItemStack stack : slot.storedRequirements) {
NBTTagCompound sub = new NBTTagCompound();
stack.writeToNBT(sub);
list.appendTag(sub);
}
NBTBase.writeNamedTag(list, new BptDataStream(writer));
writer.newLine();
} else {
writer.newLine();
}
}
}
}
writer.write(":requirements");
writer.newLine();*/
}
@Override
public void loadAttribute(BufferedReader reader, String attr, String val) throws IOException, BptError {
/*if ("3.1.0".equals(version))
throw new BptError("Blueprint format 3.1.0 is not supported anymore, can't load " + file);
// blockMap is still tested for being able to load pre 3.1.2 bpts
if (attr.equals("blockMap") || attr.equals("idMap")) {
while (true) {
String mapStr = reader.readLine();
if (mapStr == null) {
break;
}
mapStr = mapStr.replaceAll("\n", "");
if (mapStr.equals(":blockMap") || mapStr.equals(":idMap"))
return;
String[] parts = mapStr.split("=");
int blockId = Integer.parseInt(parts[1]);
if (parts[0].startsWith("#I")) {
ItemSignature sig = new ItemSignature(parts[0]);
int itemId = Integer.parseInt(parts[1]);
if (!itemMatch(sig, Item.itemsList[itemId])) {
boolean found = false;
for (int i = 256; i < Item.itemsList.length; ++i) {
// Items between 256 and Block.blocksList.length may
// be item or block
if (i < Block.blocksList.length && Block.blocksList[i] != null) {
continue;
}
if (itemMatch(sig, Item.itemsList[i])) {
found = true;
idMapping[itemId] = i;
break;
}
}
if (!found)
throw new BptError("BLUEPRINT ERROR: can't find item of signature " + sig + " for " + name);
}
} else {
BlockSignature bptSignature = new BlockSignature(parts[0]);
BptBlock defaultBlock = BlueprintManager.blockBptProps[0];
BptBlock handlingBlock = BlueprintManager.blockBptProps[blockId];
if (handlingBlock == null) {
handlingBlock = defaultBlock;
}
if (!handlingBlock.match(Block.blocksList[blockId], bptSignature)) {
boolean found = false;
for (int i = 0; i < Block.blocksList.length; ++i)
if (Block.blocksList[i] != null) {
handlingBlock = BlueprintManager.blockBptProps[i];
if (handlingBlock == null) {
handlingBlock = defaultBlock;
}
if (handlingBlock.match(Block.blocksList[i], bptSignature)) {
idMapping[blockId] = i;
found = true;
}
}
if (!found)
throw new BptError("BLUEPRINT ERROR: can't find block of signature " + bptSignature + " for " + name);
}
}
}
} else if (attr.equals("contents")) {
contents = new BptSlot[sizeX][sizeY][sizeZ];
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
String slotStr = reader.readLine().replaceAll("\n", "");
if (slotStr.equals(":contents"))
return;
if (!slotStr.equals("")) {
BptSlot slot = new BptSlot();
slot.x = x;
slot.y = y;
slot.z = z;
slot.cpt = (NBTTagCompound) NBTBase.readNamedTag(new BptDataStream(new StringReader(slotStr)));
slot.blockId = mapWorldId(slot.cpt.getInteger("bId"));
if (slot.cpt.hasKey("meta")) {
slot.meta = slot.cpt.getInteger("meta");
}
contents[x][y][z] = slot;
}
}
}
}
} else if (attr.equals("requirements")) {
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
String reqStr = reader.readLine().replaceAll("\n", "");
if (reqStr.equals(":requirements"))
return;
if (!reqStr.equals("")) {
NBTTagList list = (NBTTagList) NBTBase.readNamedTag(new BptDataStream(new StringReader(reqStr)));
for (int i = 0; i < list.tagCount(); ++i) {
ItemStack stk = mapItemStack(ItemStack.loadItemStackFromNBT((NBTTagCompound) list.tagAt(i)));
contents[x][y][z].storedRequirements.add(stk);
}
}
}
}
}
}*/
}
@Override
public String getName() {
if (name == null)
return "Blueprint #" + position;
else
return name;
}
public ItemStack mapItemStack(ItemStack bptItemStack) {
//ItemStack newStack = bptItemStack.copy();
//newStack.itemID = idMapping[newStack.itemID];
//return newStack;
return null;
}
public int mapWorldId(int bptWorldId) {
//return idMapping[bptWorldId];
return 0;
}
public void storeId(int worldId) {
if (worldId != 0) {
idsToMap.add(worldId);
}
}
@Override
public void setBlock(int x, int y, int z, Block block) {
super.setBlock(x, y, z, block);
}
@Override
protected void copyTo(BptBase bpt) {
//((BptBlueprint) bpt).idMapping = idMapping.clone();
}
}