-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlueprintBase.java
322 lines (250 loc) · 8.07 KB
/
BlueprintBase.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
/**
* Copyright (c) 2011-2015, 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.util.ArrayList;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings.GameType;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.blueprints.BuildingPermission;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.api.blueprints.Translation;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.Position;
import buildcraft.core.Box;
import buildcraft.core.Version;
public abstract class BlueprintBase {
public ArrayList<NBTTagCompound> subBlueprintsNBT = new ArrayList<NBTTagCompound>();
public int anchorX, anchorY, anchorZ;
public int sizeX, sizeY, sizeZ;
public LibraryId id = new LibraryId();
public String author;
public boolean rotate = true;
public boolean excavate = true;
public BuildingPermission buildingPermission = BuildingPermission.ALL;
public boolean isComplete = true;
protected MappingRegistry mapping = new MappingRegistry();
protected SchematicBlockBase[] contents;
private NBTTagCompound nbt;
private ForgeDirection mainDir = ForgeDirection.EAST;
public BlueprintBase() {
}
public BlueprintBase(int sizeX, int sizeY, int sizeZ) {
contents = new SchematicBlockBase[sizeX * sizeY * sizeZ];
this.sizeX = sizeX;
this.sizeY = sizeY;
this.sizeZ = sizeZ;
anchorX = 0;
anchorY = 0;
anchorZ = 0;
}
private int toArrayPos(int x, int y, int z) {
return (y * sizeZ + z) * sizeX + x;
}
public SchematicBlockBase get(int x, int y, int z) {
return contents[(y * sizeZ + z) * sizeX + x];
}
public void put(int x, int y, int z, SchematicBlockBase s) {
contents[(y * sizeZ + z) * sizeX + x] = s;
}
public void translateToBlueprint(Translation transform) {
for (SchematicBlockBase content : contents) {
if (content != null) {
content.translateToBlueprint(transform);
}
}
}
public void translateToWorld(Translation transform) {
for (SchematicBlockBase content : contents) {
if (content != null) {
content.translateToWorld(transform);
}
}
}
public void rotateLeft(BptContext context) {
SchematicBlockBase[] newContents = new SchematicBlockBase[sizeZ * sizeY * sizeX];
for (int x = 0; x < sizeZ; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeX; ++z) {
int pos = (y * sizeX + z) * sizeZ + x;
newContents[pos] = contents[toArrayPos(z, y, (sizeZ - 1) - x)];
if (newContents[pos] != null) {
try {
newContents[pos].rotateLeft(context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing(t);
}
}
}
}
}
int newAnchorX, newAnchorY, newAnchorZ;
newAnchorX = (sizeZ - 1) - anchorZ;
newAnchorY = anchorY;
newAnchorZ = anchorX;
for (NBTTagCompound sub : subBlueprintsNBT) {
ForgeDirection dir = ForgeDirection.values()[sub.getByte("dir")];
dir = dir.getRotation(ForgeDirection.UP);
Position pos = new Position(sub.getInteger("x"), sub.getInteger("y"), sub.getInteger("z"));
Position np = context.rotatePositionLeft(pos);
sub.setInteger("x", (int) np.x);
sub.setInteger("z", (int) np.z);
sub.setByte("dir", (byte) dir.ordinal());
}
context.rotateLeft();
anchorX = newAnchorX;
anchorY = newAnchorY;
anchorZ = newAnchorZ;
contents = newContents;
int tmp = sizeX;
sizeX = sizeZ;
sizeZ = tmp;
mainDir = mainDir.getRotation(ForgeDirection.UP);
}
private void writeToNBTInternal(NBTTagCompound nbt) {
nbt.setString("version", Version.VERSION);
if (this instanceof Template) {
nbt.setString("kind", "template");
} else {
nbt.setString("kind", "blueprint");
}
nbt.setInteger("sizeX", sizeX);
nbt.setInteger("sizeY", sizeY);
nbt.setInteger("sizeZ", sizeZ);
nbt.setInteger("anchorX", anchorX);
nbt.setInteger("anchorY", anchorY);
nbt.setInteger("anchorZ", anchorZ);
nbt.setBoolean("rotate", rotate);
nbt.setBoolean("excavate", excavate);
if (author != null) {
nbt.setString("author", author);
}
saveContents(nbt);
NBTTagList subBptList = new NBTTagList();
for (NBTTagCompound subBpt : subBlueprintsNBT) {
subBptList.appendTag(subBpt);
}
nbt.setTag("subBpt", subBptList);
}
public static BlueprintBase loadBluePrint(NBTTagCompound nbt) {
String kind = nbt.getString("kind");
BlueprintBase bpt;
if ("template".equals(kind)) {
bpt = new Template();
} else {
bpt = new Blueprint();
}
bpt.readFromNBT(nbt);
return bpt;
}
public void readFromNBT (NBTTagCompound nbt) {
sizeX = nbt.getInteger("sizeX");
sizeY = nbt.getInteger("sizeY");
sizeZ = nbt.getInteger("sizeZ");
anchorX = nbt.getInteger("anchorX");
anchorY = nbt.getInteger("anchorY");
anchorZ = nbt.getInteger("anchorZ");
author = nbt.getString("author");
if (nbt.hasKey("rotate")) {
rotate = nbt.getBoolean("rotate");
} else {
rotate = true;
}
if (nbt.hasKey("excavate")) {
excavate = nbt.getBoolean("excavate");
} else {
excavate = true;
}
contents = new SchematicBlockBase[sizeX * sizeY * sizeZ];
try {
loadContents(nbt);
} catch (BptError e) {
e.printStackTrace();
}
if (nbt.hasKey("subBpt")) {
NBTTagList subBptList = nbt.getTagList("subBpt", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < subBptList.tagCount(); ++i) {
subBlueprintsNBT.add(subBptList.getCompoundTagAt(i));
}
}
}
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 BptContext getContext(World world, Box box) {
return new BptContext(world, box, mapping);
}
public void addSubBlueprint(BlueprintBase subBpt, int x, int y, int z, ForgeDirection dir) {
NBTTagCompound subNBT = new NBTTagCompound();
subNBT.setInteger("x", x);
subNBT.setInteger("y", y);
subNBT.setInteger("z", z);
subNBT.setByte("dir", (byte) dir.ordinal());
subNBT.setTag("bpt", subBpt.getNBT());
subBlueprintsNBT.add(subNBT);
}
public NBTTagCompound getNBT() {
if (nbt == null) {
nbt = new NBTTagCompound();
writeToNBTInternal(nbt);
}
return nbt;
}
public BlueprintBase adjustToWorld(World world, int x, int y, int z, ForgeDirection o) {
if (buildingPermission == BuildingPermission.NONE
|| (buildingPermission == BuildingPermission.CREATIVE_ONLY && world
.getWorldInfo().getGameType() != GameType.CREATIVE)) {
return null;
}
BptContext context = getContext(world, getBoxForPos(x, y, z));
if (rotate) {
if (o == ForgeDirection.EAST) {
// Do nothing
} else if (o == ForgeDirection.SOUTH) {
rotateLeft(context);
} else if (o == ForgeDirection.WEST) {
rotateLeft(context);
rotateLeft(context);
} else if (o == ForgeDirection.NORTH) {
rotateLeft(context);
rotateLeft(context);
rotateLeft(context);
}
}
Translation transform = new Translation();
transform.x = x - anchorX;
transform.y = y - anchorY;
transform.z = z - anchorZ;
translateToWorld(transform);
return this;
}
public abstract void loadContents(NBTTagCompound nbt) throws BptError;
public abstract void saveContents(NBTTagCompound nbt);
public abstract void readFromWorld(IBuilderContext context, TileEntity anchorTile, int x, int y, int z);
public abstract ItemStack getStack();
public void readEntitiesFromWorld(IBuilderContext context, TileEntity anchorTile) {
}
}