-
Notifications
You must be signed in to change notification settings - Fork 497
/
Blueprint.java
313 lines (259 loc) · 8.26 KB
/
Blueprint.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
/**
* 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.LinkedList;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.blueprints.BuildingPermission;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingNotFoundException;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicBlockBase;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.Translation;
import buildcraft.api.core.BCLog;
import buildcraft.core.lib.utils.NBTUtils;
public class Blueprint extends BlueprintBase {
public LinkedList<SchematicEntity> entities = new LinkedList<SchematicEntity>();
public Blueprint() {
super ();
id.extension = "bpt";
}
public Blueprint(int sizeX, int sizeY, int sizeZ) {
super(sizeX, sizeY, sizeZ);
id.extension = "bpt";
}
@Override
public void rotateLeft(BptContext context) {
for (SchematicEntity e : entities) {
e.rotateLeft(context);
}
super.rotateLeft(context);
}
@Override
public void translateToBlueprint(Translation transform) {
super.translateToBlueprint(transform);
for (SchematicEntity e : entities) {
e.translateToBlueprint(transform);
}
}
@Override
public void translateToWorld(Translation transform) {
super.translateToWorld(transform);
for (SchematicEntity e : entities) {
e.translateToWorld(transform);
}
}
@Override
public void readFromWorld(IBuilderContext context, TileEntity anchorTile, int x, int y, int z) {
BptContext bptContext = (BptContext) context;
Block block = anchorTile.getWorldObj().getBlock(x, y, z);
int meta = anchorTile.getWorldObj().getBlockMetadata(x, y, z);
if (context.world().isAirBlock(x, y, z)) {
// Although no schematic will be returned for the block "air" by
// the registry, there can be other blocks considered as air. This
// will make sure that they don't get recorded.
return;
}
SchematicBlock slot = SchematicRegistry.INSTANCE.createSchematicBlock(block, meta);
if (slot == null) {
return;
}
int posX = (int) (x - context.surroundingBox().pMin().x);
int posY = (int) (y - context.surroundingBox().pMin().y);
int posZ = (int) (z - context.surroundingBox().pMin().z);
slot.block = block;
slot.meta = meta;
if (!SchematicRegistry.INSTANCE.isSupported(block, meta)) {
return;
}
try {
slot.initializeFromObjectAt(context, x, y, z);
slot.storeRequirements(context, x, y, z);
put(posX, posY, posZ, slot);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing(t);
}
switch (slot.getBuildingPermission()) {
case ALL:
break;
case CREATIVE_ONLY:
if (bptContext.readConfiguration.allowCreative) {
if (buildingPermission == BuildingPermission.ALL) {
buildingPermission = BuildingPermission.CREATIVE_ONLY;
}
} else {
put(posX, posY, posZ, null);
}
break;
case NONE:
buildingPermission = BuildingPermission.NONE;
break;
}
}
@Override
public void readEntitiesFromWorld(IBuilderContext context, TileEntity anchorTile) {
BptContext bptContext = (BptContext) context;
Translation transform = new Translation();
transform.x = -context.surroundingBox().pMin().x;
transform.y = -context.surroundingBox().pMin().y;
transform.z = -context.surroundingBox().pMin().z;
for (Object o : context.world().loadedEntityList) {
Entity e = (Entity) o;
if (context.surroundingBox().contains(e.posX, e.posY, e.posZ)) {
SchematicEntity s = SchematicRegistry.INSTANCE.createSchematicEntity(e.getClass());
if (s != null) {
s.readFromWorld(context, e);
switch (s.getBuildingPermission()) {
case ALL:
entities.add(s);
break;
case CREATIVE_ONLY:
if (bptContext.readConfiguration.allowCreative) {
if (buildingPermission == BuildingPermission.ALL) {
buildingPermission = BuildingPermission.CREATIVE_ONLY;
}
entities.add(s);
}
break;
case NONE:
buildingPermission = BuildingPermission.NONE;
break;
}
}
}
}
}
@Override
public void saveContents(NBTTagCompound nbt) {
NBTTagList nbtContents = new NBTTagList();
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
SchematicBlockBase schematic = get(x, y, z);
NBTTagCompound cpt = new NBTTagCompound();
if (schematic != null) {
schematic.idsToBlueprint(mapping);
schematic.writeSchematicToNBT(cpt, mapping);
}
nbtContents.appendTag(cpt);
}
}
}
nbt.setTag("contents", nbtContents);
NBTTagList entitiesNBT = new NBTTagList();
for (SchematicEntity s : entities) {
NBTTagCompound subNBT = new NBTTagCompound();
s.idsToBlueprint(mapping);
s.writeSchematicToNBT(subNBT, mapping);
entitiesNBT.appendTag(subNBT);
}
nbt.setTag("entities", entitiesNBT);
NBTTagCompound contextNBT = new NBTTagCompound();
mapping.write (contextNBT);
nbt.setTag("idMapping", contextNBT);
}
@Override
public void loadContents(NBTTagCompound nbt) throws BptError {
mapping.read (nbt.getCompoundTag("idMapping"));
NBTTagList nbtContents = nbt.getTagList("contents",
Constants.NBT.TAG_COMPOUND);
int index = 0;
for (int x = 0; x < sizeX; ++x) {
for (int y = 0; y < sizeY; ++y) {
for (int z = 0; z < sizeZ; ++z) {
NBTTagCompound cpt = nbtContents.getCompoundTagAt(index);
index++;
if (cpt.hasKey("blockId")) {
Block block;
try {
block = mapping.getBlockForId(cpt.getInteger("blockId"));
} catch (MappingNotFoundException e) {
block = null;
isComplete = false;
}
if (block != null) {
int meta = cpt.getInteger("blockMeta");
SchematicBlockBase schematic = SchematicRegistry.INSTANCE.createSchematicBlock(block, meta);
if (schematic != null) {
schematic.readSchematicFromNBT(cpt, mapping);
if (!schematic.doNotUse()) {
schematic.idsToWorld(mapping);
switch (schematic.getBuildingPermission()) {
case ALL:
break;
case CREATIVE_ONLY:
if (buildingPermission == BuildingPermission.ALL) {
buildingPermission = BuildingPermission.CREATIVE_ONLY;
}
break;
case NONE:
buildingPermission = BuildingPermission.NONE;
break;
}
} else {
schematic = null;
isComplete = false;
}
}
put(x, y, z, schematic);
} else {
put(x, y, z, null);
isComplete = false;
}
} else {
put(x, y, z, null);
}
}
}
}
NBTTagList entitiesNBT = nbt.getTagList("entities",
Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < entitiesNBT.tagCount(); ++i) {
NBTTagCompound cpt = entitiesNBT.getCompoundTagAt(i);
if (cpt.hasKey("entityId")) {
Class<? extends Entity> entity;
try {
entity = mapping.getEntityForId(cpt.getInteger("entityId"));
} catch (MappingNotFoundException e) {
entity = null;
isComplete = false;
}
if (entity != null) {
SchematicEntity s = SchematicRegistry.INSTANCE.createSchematicEntity(entity);
s.readSchematicFromNBT(cpt, mapping);
s.idsToWorld(mapping);
entities.add(s);
} else {
isComplete = false;
}
}
}
}
@Override
public ItemStack getStack () {
Item item = (Item) Item.itemRegistry.getObject("BuildCraft|Builders:blueprintItem");
ItemStack stack = new ItemStack(item, 1);
NBTTagCompound nbt = NBTUtils.getItemData(stack);
id.write (nbt);
nbt.setString("author", author);
nbt.setString("name", id.name);
nbt.setByte ("permission", (byte) buildingPermission.ordinal());
nbt.setBoolean("isComplete", isComplete);
return stack;
}
}