-
Notifications
You must be signed in to change notification settings - Fork 755
/
DrawbridgeLogic.java
500 lines (453 loc) · 14.8 KB
/
DrawbridgeLogic.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package tconstruct.blocks.logic;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import tconstruct.inventory.DrawbridgeContainer;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.blocks.InventoryLogic;
import tconstruct.library.util.IActiveLogic;
import tconstruct.library.util.IFacingLogic;
import tconstruct.util.player.FakePlayerLogic;
public class DrawbridgeLogic extends InventoryLogic implements IFacingLogic, IActiveLogic
{
boolean active;
boolean working;
int ticks;
byte extension;
byte direction;
byte placementDirection = 4;
FakePlayerLogic fakePlayer;
public DrawbridgeLogic()
{
super(2);
}
@Override
public void setWorldObj (World par1World)
{
this.worldObj = par1World;
fakePlayer = new FakePlayerLogic(worldObj, "Player.Drawbridge", this);
}
@Override
public boolean getActive ()
{
return active;
}
@Override
public void setActive (boolean flag)
{
active = flag;
working = true;
}
@Override
public byte getRenderDirection ()
{
return direction;
}
@Override
public ForgeDirection getForgeDirection ()
{
return ForgeDirection.VALID_DIRECTIONS[direction];
}
@Override
public void setDirection (int side)
{
}
public boolean canDropInventorySlot (int slot)
{
return false;
}
@Override
public void setDirection (float yaw, float pitch, EntityLivingBase player)
{
if (pitch > 45)
{
direction = 1;
}
else if (pitch < -45)
{
direction = 0;
}
else
{
int facing = MathHelper.floor_double((double) (yaw / 360) + 0.5D) & 3;
switch (facing)
{
case 0:
direction = 2;
break;
case 1:
direction = 5;
break;
case 2:
direction = 3;
break;
case 3:
direction = 4;
break;
}
}
}
/* 0 = Up
* 1 = Right
* 2 = Down
* 3 = Left
* 4 = Center, neutral
*/
public void setPlacementDirection (byte keycode)
{
if (keycode == 4)
{
fakePlayer.rotationYaw = 0;
fakePlayer.rotationPitch = 0;
}
else if (this.direction == 0 || this.direction == 1)
{
switch (keycode)
{
case 0:
fakePlayer.rotationYaw = 0;
break;
case 1:
fakePlayer.rotationYaw = 90;
break;
case 2:
fakePlayer.rotationYaw = 180;
break;
case 3:
fakePlayer.rotationYaw = 270;
break;
}
if (this.direction == 0)
fakePlayer.rotationPitch = -90;
else
fakePlayer.rotationPitch = 90;
}
else
{
if (keycode == 0) //Forward
{
fakePlayer.rotationYaw = mapDirection() * 90;
if (keycode == 0)
fakePlayer.rotationPitch = 90;
else
fakePlayer.rotationPitch = -90;
}
else if (keycode == 2) //Backward
{
int face = mapDirection() + 2;
if (face > 3)
face -= 4;
fakePlayer.rotationYaw = face * 90;
if (keycode == 0)
fakePlayer.rotationPitch = 90;
else
fakePlayer.rotationPitch = -90;
}
else
{
fakePlayer.rotationPitch = 0;
int facing = mapDirection();
if (keycode == 1)
facing += 1;
else
facing -= 1;
if (facing >= 4)
facing = 0;
if (facing < 0)
facing = 3;
fakePlayer.rotationYaw = facing * 90;
}
}
placementDirection = keycode;
}
int mapDirection ()
{
if (this.direction == 2) //North
return 0;
if (this.direction == 5) //East
return 1;
if (this.direction == 3) //South
return 2;
return 3; //West
}
public byte getPlacementDirection ()
{
return placementDirection;
}
@Override
public Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z)
{
return new DrawbridgeContainer(inventoryplayer, this);
}
@Override
protected String getDefaultName ()
{
return "tinker.drawbridge";
}
@Override
public void setInventorySlotContents (int slot, ItemStack itemstack)
{
super.setInventorySlotContents(slot, itemstack);
if (slot == 1)
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
@Override
public ItemStack decrStackSize (int slot, int quantity)
{
ItemStack stack = super.decrStackSize(slot, quantity);
if (slot == 1)
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
return stack;
}
public void updateEntity ()
{
if (working)
{
ticks++;
if (ticks == 5)
{
ticks = 0;
if (active) //Placement
{
if (inventory[0] != null && inventory[0].stackSize > 1 && extension < 15)
{
extension++;
int xPos = xCoord;
int yPos = yCoord;
int zPos = zCoord;
switch (direction)
{
case 0:
yPos -= extension;
break;
case 1:
yPos += extension;
break;
case 2:
zPos -= extension;
break;
case 3:
zPos += extension;
break;
case 4:
xPos -= extension;
break;
case 5:
xPos += extension;
break;
}
Block block = Block.blocksList[worldObj.getBlockId(xPos, yPos, zPos)];
if (block == null || block.isAirBlock(worldObj, xPos, yPos, zPos) || block.isBlockReplaceable(worldObj, xPos, yPos, zPos))
{
//tryExtend(worldObj, xPos, yPos, zPos, direction);
int blockToItem = TConstructRegistry.blockToItemMapping[inventory[0].itemID];
if (blockToItem == 0)
{
if (inventory[0].itemID >= 4096 || Block.blocksList[inventory[0].itemID] == null)
return;
Block placeBlock = Block.blocksList[inventory[0].itemID];
placeBlockAt(inventory[0], fakePlayer, worldObj, xPos, yPos, zPos, direction, 0, 0, 0, inventory[0].getItemDamage(), placeBlock);
}
else
{
Block placeBlock = Block.blocksList[blockToItem];
placeBlockAt(inventory[0], fakePlayer, worldObj, xPos, yPos, zPos, direction, 0, 0, 0, inventory[0].getItemDamage(), placeBlock);
}
worldObj.playSoundEffect((double) xPos + 0.5D, (double) yPos + 0.5D, (double) zPos + 0.5D, "tile.piston.out", 0.25F, worldObj.rand.nextFloat() * 0.25F + 0.6F);
inventory[0].stackSize--;
}
else
{
extension--;
working = false;
}
}
else
{
working = false;
}
}
else
//Retraction
{
if ((inventory[0] == null || inventory[0].stackSize < inventory[0].getMaxStackSize()) && extension > 0)
{
int xPos = xCoord;
int yPos = yCoord;
int zPos = zCoord;
switch (direction)
{
case 0:
yPos -= extension;
break;
case 1:
yPos += extension;
break;
case 2:
zPos -= extension;
break;
case 3:
zPos += extension;
break;
case 4:
xPos -= extension;
break;
case 5:
xPos += extension;
break;
}
Block block = Block.blocksList[worldObj.getBlockId(xPos, yPos, zPos)];
if (block != null)
{
int meta = worldObj.getBlockMetadata(xPos, yPos, zPos);
if (inventory[0] != null && validBlock(block) && validMetadata(block.blockID, meta))
{
worldObj.playSoundEffect((double) xPos + 0.5D, (double) yPos + 0.5D, (double) zPos + 0.5D, "tile.piston.in", 0.25F, worldObj.rand.nextFloat() * 0.15F + 0.6F);
if (worldObj.setBlock(xPos, yPos, zPos, 0))
inventory[0].stackSize++;
}
else
{
working = false;
}
}
extension--;
}
else
{
working = false;
}
}
}
}
}
/**
* Called to actually place the block, after the location is determined
* and all permission checks have been made.
* Copied from ItemBlock
*
* @param stack The item stack that was used to place the block. This can be changed inside the method.
* @param player The player who is placing the block. Can be null if the block is not being placed by a player.
* @param side The side the player (or machine) right-clicked on.
*/
public boolean placeBlockAt (ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata, Block block)
{
if (!world.setBlock(x, y, z, block.blockID, metadata, 3))
{
return false;
}
if (world.getBlockId(x, y, z) == block.blockID)
{
block.onBlockPlacedBy(world, x, y, z, player, stack);
block.onPostBlockPlaced(world, x, y, z, metadata);
}
return true;
}
boolean validBlock (Block block)
{
int type = TConstructRegistry.interchangableBlockMapping[block.blockID];
if (type != 0)
{
if (type == inventory[0].itemID)
return true;
}
int blockToItem = TConstructRegistry.blockToItemMapping[block.blockID];
if (blockToItem != 0)
{
if (blockToItem == inventory[0].itemID)
return true;
}
return block.blockID == inventory[0].itemID;
}
boolean validMetadata (int blockID, int metadata)
{
int type = TConstructRegistry.drawbridgeState[blockID];
if (type == 0)
{
return metadata == inventory[0].getItemDamage();
}
if (type == 1)
{
return true;
}
if (type == 2)
{
return false;
}
if (type == 3)
{
return true; //TODO: rotational metadata, probably not needed anymore
}
if (type == 4)
{
return true;
}
if (type == 5)
{
return metadata == inventory[0].getItemDamage();
}
return false;
}
@Override
public void readFromNBT (NBTTagCompound tags)
{
super.readFromNBT(tags);
active = tags.getBoolean("Active");
working = tags.getBoolean("Working");
extension = tags.getByte("Extension");
readCustomNBT(tags);
}
@Override
public void writeToNBT (NBTTagCompound tags)
{
super.writeToNBT(tags);
tags.setBoolean("Active", active);
tags.setBoolean("Working", working);
tags.setByte("Extension", extension);
writeCustomNBT(tags);
}
public void readCustomNBT (NBTTagCompound tags)
{
direction = tags.getByte("Direction");
placementDirection = tags.getByte("Placement");
}
public void writeCustomNBT (NBTTagCompound tags)
{
tags.setByte("Direction", direction);
tags.setByte("Placement", placementDirection);
}
/* Packets */
@Override
public Packet getDescriptionPacket ()
{
NBTTagCompound tag = new NBTTagCompound();
writeToNBT(tag);
return new Packet132TileEntityData(xCoord, yCoord, zCoord, 1, tag);
}
@Override
public void onDataPacket (INetworkManager net, Packet132TileEntityData packet)
{
readFromNBT(packet.data);
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
}
public boolean hasExtended ()
{
return extension != 0;
}
@Override
public void onInventoryChanged ()
{
super.onInventoryChanged();
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}