-
Notifications
You must be signed in to change notification settings - Fork 497
/
SchematicEntity.java
executable file
·220 lines (177 loc) · 6.31 KB
/
SchematicEntity.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
package buildcraft.api.blueprints;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagDouble;
import net.minecraft.nbt.NBTTagFloat;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.core.Position;
public class SchematicEntity extends Schematic {
public Class<? extends Entity> entity;
/**
* This tree contains additional data to be stored in the blueprint. By
* default, it will be initialized from Schematic.readFromWord with the
* standard readNBT function of the corresponding tile (if any) and will be
* loaded from BptBlock.writeToWorld using the standard writeNBT function.
*/
public NBTTagCompound entityNBT = new NBTTagCompound();
/**
* This field contains requirements for a given block when stored in the
* blueprint. Modders can either rely on this list or compute their own int
* Schematic.
*/
public ItemStack[] storedRequirements = new ItemStack[0];
public BuildingPermission defaultPermission = BuildingPermission.ALL;
@Override
public void getRequirementsForPlacement(IBuilderContext context, LinkedList<ItemStack> requirements) {
Collections.addAll(requirements, storedRequirements);
}
public void writeToWorld(IBuilderContext context) {
Entity e = EntityList.createEntityFromNBT(entityNBT, context.world());
context.world().spawnEntityInWorld(e);
}
public void readFromWorld(IBuilderContext context, Entity entity) {
entity.writeToNBTOptional(entityNBT);
}
@Override
public void translateToBlueprint(Translation transform) {
NBTTagList nbttaglist = entityNBT.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = transform.translate(pos);
entityNBT.setTag("Pos",
this.newDoubleNBTList(pos.x, pos.y, pos.z));
}
@Override
public void translateToWorld(Translation transform) {
NBTTagList nbttaglist = entityNBT.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = transform.translate(pos);
entityNBT.setTag("Pos",
this.newDoubleNBTList(pos.x, pos.y, pos.z));
}
@Override
public void idsToBlueprint(MappingRegistry registry) {
registry.scanAndTranslateStacksToRegistry(entityNBT);
}
@Override
public void idsToWorld(MappingRegistry registry) {
try {
registry.scanAndTranslateStacksToWorld(entityNBT);
} catch (MappingNotFoundException e) {
entityNBT = new NBTTagCompound();
}
}
@Override
public void rotateLeft(IBuilderContext context) {
NBTTagList nbttaglist = entityNBT.getTagList("Pos", 6);
Position pos = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
pos = context.rotatePositionLeft(pos);
entityNBT.setTag("Pos",
this.newDoubleNBTList(pos.x, pos.y, pos.z));
nbttaglist = entityNBT.getTagList("Rotation", 5);
float yaw = nbttaglist.func_150308_e(0);
yaw += 90;
entityNBT.setTag(
"Rotation",
this.newFloatNBTList(yaw,
nbttaglist.func_150308_e(1)));
}
@Override
public void writeSchematicToNBT(NBTTagCompound nbt, MappingRegistry registry) {
super.writeSchematicToNBT(nbt, registry);
nbt.setInteger("entityId", registry.getIdForEntity(entity));
nbt.setTag("entity", entityNBT);
NBTTagList rq = new NBTTagList();
for (ItemStack stack : storedRequirements) {
NBTTagCompound sub = new NBTTagCompound();
stack.writeToNBT(stack.writeToNBT(sub));
sub.setInteger("id", registry.getIdForItem(stack.getItem()));
rq.appendTag(sub);
}
nbt.setTag("rq", rq);
}
@Override
public void readSchematicFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
super.readSchematicFromNBT(nbt, registry);
entityNBT = nbt.getCompoundTag("entity");
NBTTagList rq = nbt.getTagList("rq",
Constants.NBT.TAG_COMPOUND);
ArrayList<ItemStack> rqs = new ArrayList<ItemStack>();
for (int i = 0; i < rq.tagCount(); ++i) {
try {
NBTTagCompound sub = rq.getCompoundTagAt(i);
if (sub.getInteger("id") >= 0) {
// Maps the id in the blueprint to the id in the world
sub.setInteger("id", Item.itemRegistry
.getIDForObject(registry.getItemForId(sub
.getInteger("id"))));
rqs.add(ItemStack.loadItemStackFromNBT(sub));
} else {
defaultPermission = BuildingPermission.CREATIVE_ONLY;
}
} catch (Throwable t) {
t.printStackTrace();
defaultPermission = BuildingPermission.CREATIVE_ONLY;
}
}
storedRequirements = rqs.toArray(new ItemStack[rqs.size()]);
}
protected NBTTagList newDoubleNBTList(double... par1ArrayOfDouble) {
NBTTagList nbttaglist = new NBTTagList();
double[] adouble = par1ArrayOfDouble;
int i = par1ArrayOfDouble.length;
for (int j = 0; j < i; ++j) {
double d1 = adouble[j];
nbttaglist.appendTag(new NBTTagDouble(d1));
}
return nbttaglist;
}
protected NBTTagList newFloatNBTList(float... par1ArrayOfFloat) {
NBTTagList nbttaglist = new NBTTagList();
float[] afloat = par1ArrayOfFloat;
int i = par1ArrayOfFloat.length;
for (int j = 0; j < i; ++j) {
float f1 = afloat[j];
nbttaglist.appendTag(new NBTTagFloat(f1));
}
return nbttaglist;
}
public boolean isAlreadyBuilt(IBuilderContext context) {
NBTTagList nbttaglist = entityNBT.getTagList("Pos", 6);
Position newPosition = new Position(nbttaglist.func_150309_d(0),
nbttaglist.func_150309_d(1), nbttaglist.func_150309_d(2));
for (Object o : context.world().loadedEntityList) {
Entity e = (Entity) o;
Position existingPositon = new Position(e.posX, e.posY, e.posZ);
if (existingPositon.isClose(newPosition, 0.1F)) {
return true;
}
}
return false;
}
@Override
public int buildTime() {
return 5;
}
@Override
public BuildingPermission getBuildingPermission() {
return defaultPermission;
}
}