-
Notifications
You must be signed in to change notification settings - Fork 54
/
TileAdventureBackpack.java
595 lines (514 loc) · 16.9 KB
/
TileAdventureBackpack.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
package com.darkona.adventurebackpack.block;
import com.darkona.adventurebackpack.common.BackpackAbilities;
import com.darkona.adventurebackpack.common.Constants;
import com.darkona.adventurebackpack.common.IAdvBackpack;
import com.darkona.adventurebackpack.events.EquipBackpackEvent;
import com.darkona.adventurebackpack.init.ModBlocks;
import com.darkona.adventurebackpack.init.ModItems;
import com.darkona.adventurebackpack.inventory.InventoryActions;
import com.darkona.adventurebackpack.inventory.SlotTool;
import com.darkona.adventurebackpack.item.ItemAdventureBackpack;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidTank;
/**
* Created by Darkona on 12/10/2014.
*/
public class TileAdventureBackpack extends TileEntity implements IAdvBackpack
{
public ItemStack[] inventory;
private FluidTank leftTank;
private FluidTank rightTank;
public boolean sleepingBagDeployed;
private boolean special;
private int sbdir;
private int sbx;
private int sby;
private int sbz;
private int checkTime = 0;
private String colorName;
private int lastTime;
private int luminosity;
private NBTTagCompound extendedProperties;
public int getLuminosity()
{
return luminosity;
}
public void setLuminosity(int luminosity)
{
this.luminosity = luminosity;
}
public int getLastTime()
{
return lastTime;
}
public void setLastTime(int lastTime)
{
this.lastTime = lastTime;
}
public NBTTagCompound getExtendedProperties()
{
return extendedProperties;
}
public void setExtendedProperties(NBTTagCompound extendedProperties)
{
this.extendedProperties = extendedProperties;
}
public TileAdventureBackpack()
{
leftTank = new FluidTank(Constants.basicTankCapacity);
rightTank = new FluidTank(Constants.basicTankCapacity);
inventory = new ItemStack[Constants.inventorySize];
sleepingBagDeployed = false;
setColorName("Standard");
luminosity = 0;
lastTime = 0;
checkTime = 0;
}
public boolean deploySleepingBag(EntityPlayer player, World world, int x, int y, int z, int meta)
{
if (world.isRemote) return false;
Block sleepingBag = ModBlocks.blockSleepingBag;
if (world.setBlock(x, y, z, sleepingBag, meta, 3))
{
world.playSoundAtEntity(player, Block.soundTypeCloth.func_150496_b(), 0.5f, 1.0f);
sbx = x;
sby = y;
sbz = z;
sbdir = meta;
switch (meta & 3)
{
case 0:
++z;
break;
case 1:
--x;
break;
case 2:
--z;
break;
case 3:
++x;
break;
}
sleepingBagDeployed = world.setBlock(x, y, z, sleepingBag, meta + 8, 3);
//LogHelper.info("deploySleepingBag() => SleepingBagDeployed is: " + sleepingBagDeployed);
world.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
return sleepingBagDeployed;
}
return false;
}
public void setSleepingBagDeployed(boolean state)
{
this.sleepingBagDeployed = state;
}
public boolean removeSleepingBag(World world)
{
if (sleepingBagDeployed)
{
if (world.getBlock(sbx, sby, sbz) == ModBlocks.blockSleepingBag)
{
world.func_147480_a(sbx, sby, sbz, false);
this.sleepingBagDeployed = false;
//LogHelper.info("removeSleepingBag() ==> SleepingBagDeployed is:" + sleepingBagDeployed);
saveChanges();
return true;
}
} else
{
this.sleepingBagDeployed = false;
saveChanges();
}
return false;
}
//=====================================================GETTERS====================================================//
public String getColorName()
{
return this.colorName;
}
@Override
public ItemStack[] getInventory()
{
return this.inventory;
}
@Override
public int getSizeInventory()
{
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int slot)
{
return inventory[slot];
}
@Override
public String getInventoryName()
{
return null;
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public FluidTank getLeftTank()
{
return leftTank;
}
@Override
public FluidTank getRightTank()
{
return rightTank;
}
//=====================================================SETTERS====================================================//
public void setColorName(String string)
{
this.colorName = string;
}
public void setLeftTank(FluidTank leftTank)
{
this.leftTank = leftTank;
markDirty();
}
public void setRightTank(FluidTank rightTank)
{
this.rightTank = rightTank;
markDirty();
}
//=====================================================BOOLEANS===================================================//
@Override
public boolean hasCustomInventoryName()
{
return false;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player)
{
return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) <= 64;
}
public boolean isSBDeployed()
{
return this.sleepingBagDeployed;
}
public boolean isSpecial()
{
return special;
}
//=======================================================NBT======================================================//
@Override
public void readFromNBT(NBTTagCompound compound)
{
super.readFromNBT(compound);
sleepingBagDeployed = compound.getBoolean("sleepingbag");
sbx = compound.getInteger("sbx");
sby = compound.getInteger("sby");
sbz = compound.getInteger("sbz");
sbdir = compound.getInteger("sbdir");
luminosity = compound.getInteger("lumen");
special = compound.getBoolean("special");
loadFromNBT(compound);
}
@Override
public void writeToNBT(NBTTagCompound compound)
{
NBTTagCompound tankLeft = new NBTTagCompound();
NBTTagCompound tankRight = new NBTTagCompound();
NBTTagList items = new NBTTagList();
for (int i = 0; i < inventory.length; i++)
{
ItemStack stack = inventory[i];
if (stack != null)
{
NBTTagCompound item = new NBTTagCompound();
item.setByte("Slot", (byte) i);
stack.writeToNBT(item);
items.appendTag(item);
}
}
compound.setTag("ABPItems", items);
compound.setBoolean("sleepingbag", sleepingBagDeployed);
compound.setInteger("sbx", sbx);
compound.setInteger("sby", sby);
compound.setInteger("sbz", sbz);
compound.setInteger("lumen", luminosity);
compound.setInteger("sbdir", sbdir);
compound.setString("colorName", colorName);
compound.setTag("rightTank", rightTank.writeToNBT(tankRight));
compound.setTag("leftTank", leftTank.writeToNBT(tankLeft));
compound.setInteger("lastTime", lastTime);
compound.setBoolean("special", BackpackAbilities.hasAbility(colorName));
compound.setTag("extended", extendedProperties);
super.writeToNBT(compound);
}
public void loadFromNBT(NBTTagCompound compound)
{
if (compound != null)
{
NBTTagList items = compound.getTagList("ABPItems", net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < items.tagCount(); i++)
{
NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
byte slot = item.getByte("Slot");
if (slot >= 0 && slot < inventory.length)
{
inventory[slot] = ItemStack.loadItemStackFromNBT(item);
}
}
leftTank.readFromNBT(compound.getCompoundTag("leftTank"));
rightTank.readFromNBT(compound.getCompoundTag("rightTank"));
colorName = compound.getString("colorName");
lastTime = compound.getInteger("lastTime");
extendedProperties = compound.getCompoundTag("extended");
}
}
@Override
public NBTTagCompound writeToNBT()
{
NBTTagCompound compound = new NBTTagCompound();
writeToNBT(compound);
return compound;
}
@Override
public void readFromNBT()
{
}
//====================================================INVENTORY===================================================//
@Override
public void openInventory()
{
}
@Override
public void closeInventory()
{
markDirty();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack)
{
if (stack.getItem() instanceof ItemAdventureBackpack || Block.getBlockFromItem(stack.getItem()) instanceof BlockAdventureBackpack)
{
return false;
}
if (slot == 6 || slot == 8)
{
return FluidContainerRegistry.isContainer(stack);
}
return !(slot == 0 || slot == 3) || SlotTool.isValidTool(stack);
}
@Override
public ItemStack decrStackSize(int i, int count)
{
ItemStack itemstack = getStackInSlot(i);
if (itemstack != null)
{
if (itemstack.stackSize <= count)
{
setInventorySlotContents(i, null);
} else
{
itemstack = itemstack.splitStack(count);
}
}
onInventoryChanged();
return itemstack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot)
{
if (slot == Constants.bucketInLeft || slot == Constants.bucketInRight || slot == Constants.bucketOutLeft || slot == Constants.bucketOutRight)
{
return inventory[slot];
}
return null;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
inventory[i] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
{
itemstack.stackSize = getInventoryStackLimit();
}
onInventoryChanged();
}
public void onInventoryChanged()
{
boolean changed = false;
for (int i = 0; i < inventory.length; i++)
{
if (i == Constants.bucketInLeft && inventory[i] != null)
{
updateTankSlots(getLeftTank(), i);
}
if (i == Constants.bucketInRight && inventory[i] != null)
{
updateTankSlots(getRightTank(), i);
}
}
markDirty();
}
@Override
public void setInventorySlotContentsNoSave(int slot, ItemStack itemstack)
{
inventory[slot] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
{
itemstack.stackSize = getInventoryStackLimit();
}
}
@Override
public void saveChanges()
{
markDirty();
}
@Override
public ItemStack decrStackSizeNoSave(int slot, int amount)
{
ItemStack stack = getStackInSlot(slot);
if (stack != null)
{
if (stack.stackSize <= amount)
{
setInventorySlotContents(slot, null);
} else
{
stack = stack.splitStack(amount);
}
}
return stack;
}
@Override
public boolean hasItem(Item item)
{
return InventoryActions.hasItem(this, item);
}
@Override
public void consumeInventoryItem(Item item)
{
InventoryActions.consumeItemInBackpack(this, item);
}
//===================================================TILE ENTITY==================================================//
//SEND SYNC PACKET
@Override
public Packet getDescriptionPacket()
{
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 0, writeToNBT());
}
//RECEIV SYNC PACKET - This is necessary for the TileEntity to load the nbt as soon as it is loaded and be rendered
//properly when the custom renderer reads it
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
{
super.onDataPacket(net, pkt);
readFromNBT(pkt.func_148857_g());
}
@Override
public void updateEntity()
{
//Execute this backpack's ability. No, seriously. You might not infer that from the code. Just sayin'
if (isSpecial() && !colorName.isEmpty())
{
BackpackAbilities.instance.executeAbility(null, this.worldObj, this);
}
//Check for backpack luminosity and a deployed sleeping bag, just in case because i'm super paranoid.
if (checkTime == 0)
{
int lastLumen = luminosity;
int left = (leftTank.getFluid() != null) ? leftTank.getFluid().getFluid().getLuminosity() : 0;
int right = (rightTank.getFluid() != null) ? rightTank.getFluid().getFluid().getLuminosity() : 0;
luminosity = Math.max(left, right);
if (luminosity != lastLumen)
{
int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
worldObj.setBlock(xCoord, yCoord, zCoord, ModBlocks.blockBackpack, meta, 3);
worldObj.setLightValue(EnumSkyBlock.Block, xCoord, yCoord, zCoord, luminosity);
}
if (worldObj.getBlock(sbx, sby, sbz) != ModBlocks.blockSleepingBag)
{
sleepingBagDeployed = false;
}
checkTime = 20;
} else
{
checkTime--;
}
}
@Override
public void updateTankSlots(FluidTank tank, int slotIn)
{
InventoryActions.transferContainerTank(this, tank, slotIn);
}
@Override
public void saveTanks()
{
}
@Override
public void loadTanks()
{
}
@Override
public TileAdventureBackpack getTile()
{
return this;
}
@Override
public ItemStack getParentItemStack()
{
return null;
}
//=====================================================BACKPACK===================================================//
public boolean equip(World world, EntityPlayer player, int x, int y, int z)
{
ItemStack stacky = new ItemStack(ModItems.adventureBackpack, 1);
stacky.setTagCompound(this.writeToNBT());
removeSleepingBag(world);
boolean response = false;
if (player.inventory.armorInventory[2] == null)
{
player.inventory.armorInventory[2] = stacky;
response = true;
} else if (player.inventory.addItemStackToInventory(stacky))
{
response = true;
}
if (response)
{
EquipBackpackEvent event = new EquipBackpackEvent(player, stacky);
MinecraftForge.EVENT_BUS.post(event);
return response;
} else
{
return drop(world, player, x, y, z);
}
}
public boolean drop(World world, EntityPlayer player, int x, int y, int z)
{
removeSleepingBag(world);
if (player.capabilities.isCreativeMode) return true;
ItemStack stacky = new ItemStack(ModItems.adventureBackpack, 1);
stacky.setTagCompound(this.writeToNBT());
float spawnX = x + world.rand.nextFloat();
float spawnY = y + world.rand.nextFloat();
float spawnZ = z + world.rand.nextFloat();
EntityItem droppedItem = new EntityItem(world, spawnX, spawnY, spawnZ, stacky);
float mult = 0.05F;
droppedItem.motionX = (-0.5F + world.rand.nextFloat()) * mult;
droppedItem.motionY = (4 + world.rand.nextFloat()) * mult;
droppedItem.motionZ = (-0.5F + world.rand.nextFloat()) * mult;
return world.spawnEntityInWorld(droppedItem);
}
}