-
Notifications
You must be signed in to change notification settings - Fork 497
/
Blueprint.java
183 lines (146 loc) · 5.13 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
/**
* 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.util.LinkedList;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.CoordTransformation;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.blueprints.SchematicRegistry;
import buildcraft.core.utils.BCLog;
import buildcraft.core.utils.NBTUtils;
import buildcraft.core.utils.Utils;
public class Blueprint extends BlueprintBase {
public LinkedList <SchematicEntity> entities = new LinkedList <SchematicEntity> ();
public Blueprint() {
super ();
}
public Blueprint(int sizeX, int sizeY, int sizeZ) {
super(sizeX, sizeY, sizeZ);
}
@Override
public void readFromWorld(IBuilderContext context, TileEntity anchorTile, int x, int y, int z) {
Block block = anchorTile.getWorldObj().getBlock(x, y, z);
SchematicBlock slot = SchematicRegistry.newSchematicBlock(block);
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 = anchorTile.getWorldObj().getBlockMetadata(x, y, z);
if (slot.block instanceof BlockContainer) {
TileEntity tile = anchorTile.getWorldObj().getTileEntity(x, y, z);
}
try {
slot.readFromWorld(context, x, y, z);
contents[posX][posY][posZ] = slot;
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BCLog.logger.throwing("BptBlueprint", "readFromWorld", t);
}
}
@Override
public void readEntitiesFromWorld(IBuilderContext context, TileEntity anchorTile) {
CoordTransformation transform = new CoordTransformation();
/*transform.x = -context.surroundingBox().pMin().x;
transform.y = -context.surroundingBox().pMin().y;
transform.z = -context.surroundingBox().pMin().z;*/
transform.x = -anchorTile.xCoord;
transform.y = -anchorTile.yCoord;
transform.z = -anchorTile.zCoord;
for (Object o : context.world().loadedEntityList) {
Entity e = (Entity) o;
if (context.surroundingBox().contains(e.posX, e.posY, e.posZ)) {
SchematicEntity s = SchematicRegistry.newSchematicEntity(e.getClass());
if (s != null) {
s.readFromWorld(context, e, transform);
entities.add(s);
}
}
}
}
@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) {
NBTTagCompound cpt = new NBTTagCompound();
if (contents [x][y][z] != null) {
contents[x][y][z].writeToNBT(cpt, mapping);
}
nbtContents.appendTag(cpt);
}
}
}
nbt.setTag("contents", nbtContents);
NBTTagList entitiesNBT = new NBTTagList();
for (SchematicEntity s : entities) {
NBTTagCompound subNBT = new NBTTagCompound();
s.writeToNBT(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",
Utils.NBTTag_Types.NBTTagCompound.ordinal());
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")) {
int blockId = cpt.getInteger("blockId");
contents[x][y][z] = SchematicRegistry.newSchematicBlock(mapping.getBlockForId(blockId));
contents[x][y][z].readFromNBT(cpt, mapping);
} else {
contents[x][y][z] = null;
}
}
}
}
NBTTagList entitiesNBT = nbt.getTagList("entities",
Utils.NBTTag_Types.NBTTagCompound.ordinal());
for (int i = 0; i < entitiesNBT.tagCount(); ++i) {
NBTTagCompound cpt = entitiesNBT.getCompoundTagAt(i);
if (cpt.hasKey("entityId")) {
int entityId = cpt.getInteger("entityId");
SchematicEntity s = SchematicRegistry.newSchematicEntity(mapping.getEntityForId(entityId));
s.readFromNBT(cpt, mapping);
entities.add(s);
}
}
}
@Override
public ItemStack getStack () {
ItemStack stack = new ItemStack(BuildCraftBuilders.blueprintItem, 1);
NBTTagCompound nbt = NBTUtils.getItemData(stack);
id.write (nbt);
nbt.setString("author", author);
return stack;
}
}