/
AdvancedDrawbridgeLogic.java
669 lines (599 loc) · 20.7 KB
/
AdvancedDrawbridgeLogic.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package tmechworks.blocks.logic;
import com.mojang.authlib.GameProfile;
import mantle.blocks.BlockUtils;
import mantle.blocks.abstracts.InventoryLogic;
import mantle.blocks.iface.*;
import mantle.common.ComparisonHelper;
import mantle.world.WorldHelper;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.*;
import net.minecraft.inventory.*;
import net.minecraft.item.*;
import net.minecraft.nbt.*;
import net.minecraft.network.*;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.ForgeDirection;
import tmechworks.inventory.AdvancedDrawbridgeContainer;
import tmechworks.lib.TMechworksRegistry;
import tmechworks.lib.blocks.IDrawbridgeLogicBase;
import tmechworks.lib.player.FakePlayerLogic;
public class AdvancedDrawbridgeLogic extends InventoryLogic implements IFacingLogic, IActiveLogic, IDrawbridgeLogicBase
{
boolean active;
boolean working;
int ticks;
public int selSlot = 0;
byte extension;
byte direction;
byte placementDirection = 4;
FakePlayerLogic fakePlayer;
ItemStack[] bufferStacks = new ItemStack[getSizeInventory()];
public InvCamo camoInventory = new InvCamo();
public AdvancedDrawbridgeLogic()
{
super(16);
}
@Override
public void func_145834_a (World par1World)
{
this.field_145850_b = par1World;
if (!par1World.isRemote)
fakePlayer = new FakePlayerLogic((WorldServer)field_145850_b, new GameProfile(null, "Player.Drawbridge"), (InventoryLogic)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 (!field_145850_b.isRemote)
{
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 ItemStack getStackInSlot (int slot)
{
return slot < inventory.length ? inventory[slot] : null;
}
public ItemStack getStackInBufferSlot (int slot)
{
return slot < bufferStacks.length ? bufferStacks[slot] : null;
}
public void setBufferSlotContents (int slot, ItemStack itemstack)
{
if (slot < bufferStacks.length)
{
bufferStacks[slot] = itemstack;
}
else
{
return;
}
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
{
itemstack.stackSize = getInventoryStackLimit();
}
}
@Override
public Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z)
{
return new AdvancedDrawbridgeContainer(inventoryplayer, this);
}
@Override
protected String getDefaultName ()
{
return "tinker.drawbridge";
}
@Override
public ItemStack decrStackSize (int slot, int quantity)
{
ItemStack stack = super.decrStackSize(slot, quantity);
if (slot == 1)
field_145850_b.func_147471_g(field_145851_c, field_145848_d, field_145849_e);
return stack;
}
public void updateEntity ()
{
if (working)
{
ticks++;
if (ticks == 5)
{
ticks = 0;
if (active) // Placement
{
if (getStackInSlot(extension) != null && getStackInSlot(extension).stackSize > 0 && extension < 15)
{
extension++;
int xPos = field_145851_c;
int yPos = field_145848_d;
int zPos = field_145849_e;
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 = field_145850_b.func_147439_a(xPos, yPos, zPos);
if (block == null || WorldHelper.isAirBlock(field_145850_b, xPos, yPos, zPos) || block.func_149742_c(field_145850_b, xPos, yPos, zPos))
{
// tryExtend(field_145850_b, xPos, yPos, zPos, direction);
Item blockToItem = (Item) (getStackInBufferSlot(extension - 1) != null ? TMechworksRegistry.blockToItemMapping.get(getStackInBufferSlot(extension - 1).getItem()) : 0);
if (blockToItem == null)
{
if (getStackInSlot(extension - 1) == null || BlockUtils.getBlockFromItem(getStackInSlot(extension - 1).getItem()) == null)
return;
Block placeBlock = BlockUtils.getBlockFromItem(getStackInBufferSlot(extension - 1).getItem());
placeBlockAt(getStackInSlot(extension - 1), fakePlayer, field_145850_b, xPos, yPos, zPos, direction, 0, 0, 0, getStackInSlot(extension - 1).getItemDamage(), placeBlock);
}
else
{
Block placeBlock = BlockUtils.getBlockFromItem(blockToItem);
placeBlockAt(getStackInSlot(extension - 1), fakePlayer, field_145850_b, xPos, yPos, zPos, direction, 0, 0, 0, getStackInSlot(extension - 1).getItemDamage(), placeBlock);
}
field_145850_b.playSoundEffect((double) xPos + 0.5D, (double) yPos + 0.5D, (double) zPos + 0.5D, "tile.piston.out", 0.25F, field_145850_b.rand.nextFloat() * 0.25F + 0.6F);
decrStackSize(extension - 1, 1);
}
else
{
extension--;
working = false;
}
}
else
{
working = false;
}
}
else
// Retraction
{
if ((getStackInSlot(extension) == null || getStackInSlot(extension).stackSize < getStackInSlot(extension).getMaxStackSize()) && extension > 0)
{
int xPos = field_145851_c;
int yPos = field_145848_d;
int zPos = field_145849_e;
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 = field_145850_b.func_147439_a(xPos, yPos, zPos);
if (block != null)
{
int meta = field_145850_b.getBlockMetadata(xPos, yPos, zPos);
if (getStackInBufferSlot(extension - 1) != null && validBlock(extension - 1, block) && validMetadata(extension - 1, block, meta) && validDrawbridge(xPos, yPos, zPos))
{
field_145850_b.playSoundEffect((double) xPos + 0.5D, (double) yPos + 0.5D, (double) zPos + 0.5D, "tile.piston.in", 0.25F, field_145850_b.rand.nextFloat() * 0.15F + 0.6F);
if (WorldHelper.setBlockToAirBool(field_145850_b, xPos, yPos, zPos))
if (getStackInSlot(extension - 1) == null)
{
setInventorySlotContents(extension - 1, getStackInBufferSlot(extension - 1).copy());
}
else
{
getStackInSlot(extension - 1).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.func_147465_d(x, y, z, block, metadata, 3))
{
return false;
}
if (world.func_147439_a(x, y, z) == block)
{
block.func_149689_a(world, x, y, z, player, stack);
block.func_149714_e(world, x, y, z, metadata);
}
return true;
}
boolean validDrawbridge (int x, int y, int z)
{
TileEntity te = field_145850_b.func_147438_o(x, y, z);
if (te instanceof IDrawbridgeLogicBase && ((IDrawbridgeLogicBase) te).hasExtended())
return false;
return true;
}
boolean validBlock (int slot, Block block)
{
ItemStack type = new ItemStack(TMechworksRegistry.interchangableBlockMapping.get(block));
if (type != null)
{
if (type == getStackInBufferSlot(slot))
return true;
}
Item blockToItem = TMechworksRegistry.blockToItemMapping.get(block);
if (blockToItem != null)
{
if (blockToItem == getStackInBufferSlot(slot).getItem())
return true;
}
return ComparisonHelper.areEquivalent(getStackInBufferSlot(slot).getItem(),block);
}
boolean validMetadata (int slot, Block block, int metadata)
{
int type = TMechworksRegistry.drawbridgeState.get(block).getTypeID();
if (type == 0)
{
return metadata == getStackInBufferSlot(slot).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 == getStackInBufferSlot(slot).getItemDamage();
}
return false;
}
@Override
public void func_145839_a (NBTTagCompound tags)
{
super.func_145839_a(tags);
active = tags.getBoolean("Active");
working = tags.getBoolean("Working");
extension = tags.getByte("Extension");
NBTTagCompound camoTag = (NBTTagCompound) tags.getTag("Camo");
if (camoTag != null)
{
camoInventory.setInventorySlotContents(0, ItemStack.loadItemStackFromNBT(camoTag));
}
readBufferFromNBT(tags);
readCustomNBT(tags);
}
@Override
public void func_145841_b (NBTTagCompound tags)
{
super.func_145841_b(tags);
tags.setBoolean("Active", active);
tags.setBoolean("Working", working);
tags.setByte("Extension", extension);
if (camoInventory.getStackInSlot(0) != null)
{
NBTTagCompound camoTag = new NBTTagCompound();
camoInventory.getStackInSlot(0).writeToNBT(camoTag);
tags.setTag("Camo", camoTag);
}
writeBufferToNBT(tags);
writeCustomNBT(tags);
}
public void readBufferFromNBT (NBTTagCompound tags)
{
NBTTagList nbttaglist = tags.func_150295_c("Buffer", 9);
bufferStacks = new ItemStack[getSizeInventory()];
// bufferStacks.ensureCapacity(nbttaglist.tagCount() > getSizeInventory() ? getSizeInventory() : nbttaglist.tagCount());
for (int iter = 0; iter < nbttaglist.tagCount(); iter++)
{
NBTTagCompound tagList = (NBTTagCompound) nbttaglist.func_150305_b(iter);
byte slotID = tagList.getByte("Slot");
if (slotID >= 0 && slotID < bufferStacks.length)
{
setBufferSlotContents(slotID, ItemStack.loadItemStackFromNBT(tagList));
}
}
}
public void writeBufferToNBT (NBTTagCompound tags)
{
NBTTagList nbttaglist = new NBTTagList();
for (int iter = 0; iter < bufferStacks.length; iter++)
{
if (getStackInBufferSlot(iter) != null)
{
NBTTagCompound tagList = new NBTTagCompound();
tagList.setByte("Slot", (byte) iter);
getStackInBufferSlot(iter).writeToNBT(tagList);
nbttaglist.appendTag(tagList);
}
}
tags.setTag("Buffer", nbttaglist);
}
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 func_145844_m ()
{
NBTTagCompound tag = new NBTTagCompound();
func_145841_b(tag);
return new S35PacketUpdateTileEntity(field_145851_c, field_145848_d, field_145849_e, 1, tag);
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
{
func_145839_a(packet.func_148857_g());
field_145850_b.func_147479_m(field_145851_c, field_145848_d, field_145849_e);
}
public boolean hasExtended ()
{
return extension != 0;
}
@Override
public void onInventoryChanged ()
{
super.onInventoryChanged();
for (int i = 0; i < getSizeInventory(); i++)
{
if (getStackInSlot(i) != null)
{
setBufferSlotContents(i, getStackInSlot(i).copy());
getStackInBufferSlot(i).stackSize = 1;
}
}
this.field_145850_b.func_147471_g(field_145851_c, field_145848_d, field_145849_e);
}
// @Override
// public int getMaxSize() {
// return 16;
// }
@Override
public int getInventoryStackLimit ()
{
return 1;
}
public class InvCamo extends InventoryBasic
{
private InvCamo()
{
super("camoSlot", false, 1);
}
public ItemStack getCamoStack ()
{
return this.getStackInSlot(0);
}
@Override
public void setInventorySlotContents (int slot, ItemStack content)
{
super.setInventorySlotContents(slot, content);
if (slot == 0 && field_145850_b != null)
field_145850_b.func_147471_g(field_145851_c, field_145848_d, field_145849_e);
}
@Override
public void onInventoryChanged ()
{
super.onInventoryChanged();
if (field_145850_b != null)
{
field_145850_b.func_147471_g(field_145851_c, field_145848_d, field_145849_e);
}
}
@Override
public int getInventoryStackLimit ()
{
return 1;
}
}
@Override
public String func_145825_b ()
{
return getDefaultName();
}
@Override
public boolean func_145818_k_ ()
{
return true;
}
}