-
Notifications
You must be signed in to change notification settings - Fork 497
/
BptBase.java
347 lines (272 loc) · 7.78 KB
/
BptBase.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
/**
* Copyright (c) SpaceToad, 2011-2012
* 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 buildcraft.BuildCraftCore;
import buildcraft.core.Box;
import buildcraft.core.Version;
import buildcraft.core.proxy.CoreProxy;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public abstract class BptBase {
BptSlot contents[][][];
public int position;
public int anchorX, anchorY, anchorZ;
public int sizeX, sizeY, sizeZ;
protected String name;
public String author;
public File file;
protected String version = "";
public BptBase() {
}
public BptBase(int sizeX, int sizeY, int sizeZ) {
contents = new BptSlot[sizeX][sizeY][sizeZ];
this.sizeX = sizeX;
this.sizeY = sizeY;
this.sizeZ = sizeZ;
anchorX = 0;
anchorY = 0;
anchorZ = 0;
}
public void setBlockId(int x, int y, int z, int blockId) {
if (contents[x][y][z] == null) {
contents[x][y][z] = new BptSlot();
contents[x][y][z].x = x;
contents[x][y][z].y = y;
contents[x][y][z].z = z;
}
contents[x][y][z].blockId = blockId;
}
public void rotateLeft(BptContext context) {
BptSlot newContents[][][] = new BptSlot[sizeZ][sizeY][sizeX];
for (int x = 0; x < sizeZ; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeX; ++z) {
newContents[x][y][z] = contents[z][y][(sizeZ - 1) - x];
if (newContents[x][y][z] != null) {
try {
newContents[x][y][z].rotateLeft(context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BuildCraftCore.bcLog.throwing("BptBase", "rotateLeft", t);
}
}
}
}
}
int newAnchorX, newAnchorY, newAnchorZ;
newAnchorX = (sizeZ - 1) - anchorZ;
newAnchorY = anchorY;
newAnchorZ = anchorX;
contents = newContents;
int tmp = sizeX;
sizeX = sizeZ;
sizeZ = tmp;
anchorX = newAnchorX;
anchorY = newAnchorY;
anchorZ = newAnchorZ;
context.rotateLeft();
}
public File save() {
try {
File baseDir = new File("./");
baseDir.mkdir();
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream output = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "8859_1"));
writer.write("version:" + Version.VERSION);
writer.newLine();
if (this instanceof BptTemplate) {
writer.write("kind:template");
} else {
writer.write("kind:blueprint");
}
writer.newLine();
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();
writer.write("name:" + name);
writer.newLine();
if (author != null) {
writer.write("author:" + author);
writer.newLine();
}
saveAttributes(writer);
writer.newLine();
writer.flush();
output.close();
return file;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean equals(Object o) {
if (o == null || !(o.getClass() != getClass()))
return false;
BptBase bpt = (BptBase) o;
if (sizeX != bpt.sizeX || sizeY != bpt.sizeY || sizeZ != bpt.sizeZ || anchorX != bpt.anchorX || anchorY != bpt.anchorY || anchorZ != bpt.anchorZ)
return false;
for (int x = 0; x < contents.length; ++x) {
for (int y = 0; y < contents[0].length; ++y) {
for (int z = 0; z < contents[0][0].length; ++z) {
if (contents[x][y][z] != null && bpt.contents[x][y][z] == null)
return false;
if (contents[x][y][z] == null && bpt.contents[x][y][z] != null)
return false;
if (contents[x][y][z] == null && bpt.contents[x][y][z] == null) {
continue;
}
if (contents[x][y][z].blockId != bpt.contents[x][y][z].blockId)
return false;
}
}
}
return true;
}
public static BptBase loadBluePrint(File file, int position) {
BptBase result = null;
String version = null;
try {
FileInputStream input = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "8859_1"));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
String[] cpts = line.split(":");
String attr = cpts[0];
if (attr.equals("kind")) {
if (cpts[1].equals("template")) {
result = new BptTemplate();
} else if (cpts[1].equals("blueprint")) {
result = new BptBlueprint();
}
result.position = position;
result.version = version;
result.file = file;
} else if (attr.equals("sizeX")) {
result.sizeX = Integer.parseInt(cpts[1]);
} else if (attr.equals("sizeY")) {
result.sizeY = Integer.parseInt(cpts[1]);
} else if (attr.equals("sizeZ")) {
result.sizeZ = Integer.parseInt(cpts[1]);
} else if (attr.equals("anchorX")) {
result.anchorX = Integer.parseInt(cpts[1]);
} else if (attr.equals("anchorY")) {
result.anchorY = Integer.parseInt(cpts[1]);
} else if (attr.equals("anchorZ")) {
result.anchorZ = Integer.parseInt(cpts[1]);
} else if (attr.equals("name")) {
result.name = cpts[1];
} else if (attr.equals("version")) {
if (result != null) {
result.version = version;
} else {
version = cpts[1];
}
} else if (attr.equals("author")) {
result.author = cpts[1];
} else if (result != null)
if (cpts.length >= 2) {
result.loadAttribute(reader, attr, cpts[1]);
} else {
result.loadAttribute(reader, attr, "");
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return result;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
if (name == null)
return "#" + position;
else
return name;
}
@Override
public final BptBase clone() {
BptBase res = null;
try {
res = getClass().newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
res.anchorX = anchorX;
res.anchorY = anchorY;
res.anchorZ = anchorZ;
res.sizeX = sizeX;
res.sizeY = sizeY;
res.sizeZ = sizeZ;
res.position = position;
res.name = name;
res.author = author;
res.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)
if (contents[x][y][z] != null) {
res.contents[x][y][z] = contents[x][y][z].clone();
}
}
}
copyTo(res);
return res;
}
protected void copyTo(BptBase base) {
}
public Box getBoxForPos(int x, int y, int z) {
int xMin = x - anchorX;
int yMin = y - anchorY;
int zMin = z - anchorZ;
int xMax = x + sizeX - anchorX - 1;
int yMax = y + sizeY - anchorY - 1;
int zMax = z + sizeZ - anchorZ - 1;
Box res = new Box();
res.initialize(xMin, yMin, zMin, xMax, yMax, zMax);
res.reorder();
return res;
}
public abstract void loadAttribute(BufferedReader reader, String attr, String val) throws IOException, BptError;
public abstract void saveAttributes(BufferedWriter writer) throws IOException;
}