-
Notifications
You must be signed in to change notification settings - Fork 497
/
BuildingItem.java
executable file
·290 lines (219 loc) · 7.67 KB
/
BuildingItem.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
/**
* 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.builders;
import java.util.Date;
import java.util.LinkedList;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.core.NetworkData;
import buildcraft.api.core.Position;
import buildcraft.core.blueprints.BuildingSlot;
import buildcraft.core.blueprints.BuildingSlotBlock;
import buildcraft.core.blueprints.BuildingSlotEntity;
import buildcraft.core.blueprints.IBuilder;
public class BuildingItem implements IBuilder {
@NetworkData
public Position origin, destination;
@NetworkData
public LinkedList<StackAtPosition> stacksToDisplay = new LinkedList<StackAtPosition>();
public Position posDisplay = new Position();
public boolean isDone = false;
public BuildingSlot slotToBuild;
public IBuilderContext context;
public double receivedProgress = 0;
private long previousUpdate;
private double lifetimeDisplay = 0;
private double maxLifetime = 0;
private boolean initialized = false;
private double vx, vy, vz;
private double maxHeight;
@NetworkData
private double lifetime = 0;
public void initialize () {
if (!initialized) {
double dx = destination.x - origin.x;
double dy = destination.y - origin.y;
double dz = destination.z - origin.z;
double size = Math.sqrt(dx * dx + dy * dy + dz * dz);
maxLifetime = size * 5.0;
maxHeight = 5.0 + (destination.y - origin.y) / 2.0;
// the below computation is an approximation of the distance to
// travel for the object. It really follows a sinus, but we compute
// the size of a triangle for simplification.
Position middle = new Position();
middle.x = (destination.x + origin.x) / 2;
middle.y = (destination.y + origin.y) / 2;
middle.z = (destination.z + origin.z) / 2;
Position top = new Position ();
top.x = middle.x;
top.y = middle.y + maxHeight;
top.z = middle.z;
Position originToTop = new Position ();
originToTop.x = top.x - origin.x;
originToTop.y = top.y - origin.y;
originToTop.z = top.z - origin.z;
Position destinationToTop = new Position ();
destinationToTop.x = destination.x - origin.x;
destinationToTop.y = destination.y - origin.y;
destinationToTop.z = destination.z - origin.z;
Position distance = new Position();
double d1 = Math.sqrt(originToTop.x * originToTop.x + originToTop.y
* originToTop.y + originToTop.z * originToTop.z);
double d2 = Math.sqrt(destinationToTop.x * destinationToTop.x + destinationToTop.y
* destinationToTop.y + destinationToTop.z * destinationToTop.z);
d1 = d1 / size * maxLifetime;
d2 = d2 / size * maxLifetime;
maxLifetime = d1 + d2;
vx = dx / maxLifetime;
vy = dy / maxLifetime;
vz = dz / maxLifetime;
if (stacksToDisplay.size() == 0) {
StackAtPosition sPos = new StackAtPosition();
sPos.stack = new ItemStack(BuildCraftBuilders.buildToolBlock);
stacksToDisplay.add(sPos);
}
initialized = true;
}
}
public Position getDisplayPosition (double time) {
Position result = new Position ();
result.x = origin.x + vx * time;
result.y = origin.y + vy * time + Math.sin(time / maxLifetime * Math.PI) * maxHeight;
result.z = origin.z + vz * time;
return result;
}
public void update () {
if (isDone) {
return;
}
initialize();
lifetime++;
if (lifetime > maxLifetime + stacksToDisplay.size() - 1) {
isDone = true;
build ();
}
lifetimeDisplay = lifetime;
previousUpdate = new Date ().getTime();
if (slotToBuild != null && lifetime > maxLifetime) {
slotToBuild.writeCompleted(context, (lifetime - maxLifetime)
/ stacksToDisplay.size());
}
}
public void displayUpdate () {
initialize();
double tickDuration = 1000.0 / 20.0;
long currentUpdate = new Date ().getTime();
double timeSpan = currentUpdate - previousUpdate;
previousUpdate = currentUpdate;
double displayPortion = timeSpan / tickDuration;
if (lifetimeDisplay - lifetime <= 1.0) {
lifetimeDisplay += 1.0 * displayPortion;
}
}
private void build() {
if (slotToBuild != null) {
int destX = (int) Math.floor(destination.x);
int destY = (int) Math.floor(destination.y);
int destZ = (int) Math.floor(destination.z);
Block block = context.world().getBlock(destX, destY, destZ);
int meta = context.world().getBlockMetadata(destX, destY, destZ);
context.world().playAuxSFXAtEntity(null, 2001,
destX, destY, destZ,
Block.getIdFromBlock(block) + (meta << 12));
/*if (BlockUtil.isToughBlock(context.world(), destX, destY, destZ)) {
BlockUtil.breakBlock(context.world(), destX, destY, destZ, BuildCraftBuilders.fillerLifespanTough);
} else {
BlockUtil.breakBlock(context.world(), destX, destY, destZ, BuildCraftBuilders.fillerLifespanNormal);
}*/
slotToBuild.writeToWorld(context);
}
}
public LinkedList<StackAtPosition> getStacks() {
int d = 0;
for (StackAtPosition s : stacksToDisplay) {
double stackLife = lifetimeDisplay - d;
if (stackLife <= maxLifetime && stackLife > 0) {
s.pos = getDisplayPosition(stackLife);
s.display = true;
} else {
s.display = false;
}
d++;
}
return stacksToDisplay;
}
@Override
public boolean isDone() {
return isDone;
}
public void writeToNBT (NBTTagCompound nbt) {
NBTTagCompound originNBT = new NBTTagCompound();
origin.writeToNBT(originNBT);
nbt.setTag ("origin", originNBT);
NBTTagCompound destinationNBT = new NBTTagCompound();
destination.writeToNBT(destinationNBT);
nbt.setTag ("destination", destinationNBT);
nbt.setDouble("lifetime", lifetime);
NBTTagList items = new NBTTagList();
for (StackAtPosition s : stacksToDisplay) {
NBTTagCompound cpt = new NBTTagCompound();
s.stack.writeToNBT(cpt);
items.appendTag(cpt);
}
nbt.setTag("items", items);
MappingRegistry registry = new MappingRegistry();
NBTTagCompound slotNBT = new NBTTagCompound();
NBTTagCompound registryNBT = new NBTTagCompound();
slotToBuild.writeToNBT(slotNBT, registry);
registry.write(registryNBT);
nbt.setTag("registry", registryNBT);
if (slotToBuild instanceof BuildingSlotBlock) {
nbt.setByte ("slotKind", (byte) 0);
} else {
nbt.setByte ("slotKind", (byte) 1);
}
nbt.setTag("slotToBuild", slotNBT);
}
public void readFromNBT (NBTTagCompound nbt) {
origin = new Position(nbt.getCompoundTag("origin"));
destination = new Position (nbt.getCompoundTag("destination"));
lifetime = nbt.getDouble("lifetime");
NBTTagList items = nbt.getTagList("items",
Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < items.tagCount(); ++i) {
StackAtPosition sPos = new StackAtPosition();
sPos.stack = ItemStack.loadItemStackFromNBT(items
.getCompoundTagAt(i));
stacksToDisplay.add(sPos);
}
MappingRegistry registry = new MappingRegistry();
registry.read(nbt.getCompoundTag("registry"));
if (nbt.getByte("slotKind") == 0) {
slotToBuild = new BuildingSlotBlock();
} else {
slotToBuild = new BuildingSlotEntity();
}
slotToBuild.readFromNBT(nbt.getCompoundTag("slotToBuild"), registry);
}
public void setStacksToDisplay(LinkedList<ItemStack> stacks) {
if (stacks != null) {
for (ItemStack s : stacks) {
StackAtPosition sPos = new StackAtPosition();
sPos.stack = s;
stacksToDisplay.add(sPos);
}
}
}
}