-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileUrbanist.java
executable file
·366 lines (296 loc) · 8.58 KB
/
TileUrbanist.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* 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.builders.urbanism;
import java.util.ArrayList;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import cpw.mods.fml.relauncher.Side;
import buildcraft.BuildCraftCore;
import buildcraft.core.Box;
import buildcraft.core.Box.Kind;
import buildcraft.core.IBoxesProvider;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.network.Packet;
import buildcraft.core.network.CommandWriter;
import buildcraft.core.network.ICommandReceiver;
import buildcraft.core.network.PacketCommand;
import buildcraft.core.utils.NetworkUtils;
public class TileUrbanist extends TileBuildCraft implements IInventory, IBoxesProvider, ICommandReceiver {
public EntityUrbanist urbanist;
public ArrayList<AnchoredBox> frames = new ArrayList<AnchoredBox>();
private EntityLivingBase player;
private int thirdPersonView = 0;
private double posX, posY, posZ;
private float yaw;
private int p2x = 0;
private int p2y = 0;
private int p2z = 0;
private boolean isCreatingFrame = false;
// private LinkedList<IRobotTask> tasks = new LinkedList<IRobotTask>();
public void createUrbanistEntity() {
if (worldObj.isRemote) {
if (urbanist == null) {
urbanist = new EntityUrbanist(worldObj);
worldObj.spawnEntityInWorld(urbanist);
player = Minecraft.getMinecraft().renderViewEntity;
urbanist.copyLocationAndAnglesFrom(player);
urbanist.tile = this;
urbanist.player = player;
urbanist.rotationYaw = 0;
urbanist.rotationPitch = 0;
Minecraft.getMinecraft().renderViewEntity = urbanist;
thirdPersonView = Minecraft.getMinecraft().gameSettings.thirdPersonView;
Minecraft.getMinecraft().gameSettings.thirdPersonView = 8;
posX = urbanist.posX;
posY = urbanist.posY + 10;
posZ = urbanist.posZ;
yaw = 0;
urbanist.setPositionAndRotation(posX, posY, posZ, yaw, 50);
urbanist.setPositionAndUpdate(posX, posY, posZ);
}
}
}
@Override
public void updateEntity() {
super.updateEntity();
}
private Packet createXYZPacket(String name, final int x, final int y, final int z) {
return new PacketCommand(this, name, new CommandWriter() {
public void write(ByteBuf data) {
data.writeInt(x);
data.writeShort(y);
data.writeInt(z);
}
});
}
@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
// Non-XYZ commands go here
if (side.isClient() && "setFrameKind".equals(command)) {
setFrameKind(stream.readInt(), stream.readInt());
} else if (side.isServer() && "startFiller".equals(command)) {
String fillerTag = NetworkUtils.readUTF(stream);
Box box = new Box();
box.readData(stream);
startFiller(fillerTag, box);
} else {
// XYZ commands go here
int x = stream.readInt();
int y = stream.readInt();
int z = stream.readInt();
if (side.isServer() && "setBlock".equals(command)) {
worldObj.setBlock(x, y, z, Blocks.brick_block);
} else if (side.isServer() && "eraseBlock".equals(command)) {
// tasks.add(new UrbanistTaskErase(this, x, y, z));
} else if ("createFrame".equals(command)) {
createFrame(x, y, z);
} else if ("moveFrame".equals(command)) {
moveFrame(x, y, z);
}
}
}
public void rpcEraseBlock (int x, int y, int z) {
BuildCraftCore.instance.sendToServer(createXYZPacket("eraseBlock", x, y, z));
}
public void createFrame (int x, int y, int z) {
isCreatingFrame = true;
AnchoredBox a = new AnchoredBox();
a.box = new Box (x, y, z, x, y + 2, z);
a.x1 = x;
a.y1 = y;
a.z1 = z;
frames.add(a);
}
public void rpcCreateFrame (int x, int y, int z) {
p2x = x;
p2y = y;
p2z = z;
// TODO: this is OK in SMP, but the frame actually needs to be
// broadcasted to all players
createFrame(x, y, z);
BuildCraftCore.instance.sendToServer(createXYZPacket("createFrame", x, y, z));
}
public void moveFrame (int x, int y, int z) {
if (isCreatingFrame) {
if (frames.size() > 0) {
frames.get(frames.size() - 1).setP2(x, y, z);
}
}
}
public void rpcMoveFrame (int x, int y, int z) {
if (p2x != x || p2y != y || p2z != z) {
p2x = x;
p2y = y;
p2z = z;
// TODO: this is OK in SMP, but the frame actually needs to be
// broadcasted to all players
moveFrame(x, y, z);
BuildCraftCore.instance.sendToServer(createXYZPacket("moveFrame", x, y, z));
}
}
public class FrameTask {
int nbOfTasks;
AnchoredBox frame;
public void taskDone () {
nbOfTasks--;
if (nbOfTasks <= 0) {
frames.remove(frame);
}
}
}
public void setFrameKind (int id, int kind) {
if (id < frames.size()) {
AnchoredBox b = frames.get(id);
if (b != null) {
b.box.kind = Kind.values()[kind];
}
}
}
public void startFiller (String fillerTag, Box box) {
// TODO: This need to be updated to the new blueprint system
/*BptBuilderBase builder = FillerPattern.patterns.get(fillerTag).getBlueprint(box, worldObj);
List <SchematicBuilder> schematics = builder.getBuilders();
FrameTask task = new FrameTask();
task.frame = frames.get(frames.size() - 1);
task.frame.box.kind = Kind.STRIPES;
RPCHandler.rpcBroadcastPlayers(this, "setFrameKind", frames.size() - 1,
Kind.STRIPES.ordinal());
isCreatingFrame = false;
for (SchematicBuilder b : schematics) {
if (!b.isComplete()) {
tasks.add(new TaskBuildSchematic(b, task));
task.nbOfTasks++;
}
}*/
}
public void rpcStartFiller (final String fillerTag, final Box box) {
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "startFiller", new CommandWriter() {
public void write(ByteBuf data) {
NetworkUtils.writeUTF(data, fillerTag);
box.writeData(data);
}
}));
}
public void destroyUrbanistEntity() {
Minecraft.getMinecraft().renderViewEntity = player;
Minecraft.getMinecraft().gameSettings.thirdPersonView = thirdPersonView;
worldObj.removeEntity(urbanist);
urbanist.setDead();
urbanist = null;
}
@Override
public int getSizeInventory() {
return 0;
}
@Override
public ItemStack getStackInSlot(int var1) {
return null;
}
@Override
public ItemStack decrStackSize(int var1, int var2) {
return null;
}
@Override
public ItemStack getStackInSlotOnClosing(int var1) {
return null;
}
@Override
public void setInventorySlotContents(int var1, ItemStack var2) {
}
@Override
public String getInventoryName() {
return null;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getInventoryStackLimit() {
return 0;
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1) {
return true;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public boolean isItemValidForSlot(int var1, ItemStack var2) {
return false;
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
Box box = new Box(this);
for (AnchoredBox b : frames) {
box.extendToEncompass(b.box);
}
return box.getBoundingBox();
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setInteger("nbFrames", frames.size());
for (int i = 0; i < frames.size(); ++i) {
NBTTagCompound cpt = new NBTTagCompound();
frames.get(i).writeToNBT(cpt);
nbt.setTag("frame[" + i + "]", cpt);
}
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
frames.clear();
int size = nbt.getInteger("nbFrames");
for (int i = 0; i < size; ++i) {
AnchoredBox b = new AnchoredBox();
b.readFromNBT(nbt.getCompoundTag("frame[" + i + "]"));
frames.add(b);
}
}
@Override
public void initialize () {
}
@Override
public void writeData(ByteBuf stream) {
stream.writeShort(frames.size());
for (AnchoredBox b : frames) {
b.writeData(stream);
}
}
@Override
public void readData(ByteBuf stream) {
frames.clear();
int size = stream.readUnsignedShort();
for (int i = 0; i < size; i++) {
AnchoredBox b = new AnchoredBox();
b.readData(stream);
frames.add(b);
}
}
@Override
public ArrayList<Box> getBoxes() {
ArrayList<Box> result = new ArrayList<Box>();
for (AnchoredBox b : frames) {
result.add(b.box);
}
return result;
}
}