-
Notifications
You must be signed in to change notification settings - Fork 81
/
TileEntityNetherrackFurnace.java
649 lines (577 loc) · 18.8 KB
/
TileEntityNetherrackFurnace.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
package com.progwml6.natura.nether.block.furnace.tile;
import com.progwml6.natura.common.gui.common.FurnaceContainer;
import com.progwml6.natura.nether.block.furnace.BlockNetherrackFurnace;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.inventory.SlotFurnaceFuel;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBoat;
import net.minecraft.item.ItemDoor;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityLockable;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class TileEntityNetherrackFurnace extends TileEntityLockable implements ITickable, ISidedInventory
{
private static final int[] SLOTS_TOP = new int[] { 0 };
private static final int[] SLOTS_BOTTOM = new int[] { 2, 1 };
private static final int[] SLOTS_SIDES = new int[] { 1 };
/** The ItemStacks that hold the items currently being used in the furnace */
private NonNullList<ItemStack> furnaceItemStacks = NonNullList.<ItemStack> withSize(3, ItemStack.EMPTY);
/** The number of ticks that the furnace will keep burning */
private int furnaceBurnTime;
/** The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for */
private int currentItemBurnTime;
private int cookTime;
private int totalCookTime;
private String furnaceCustomName;
/**
* Returns the number of slots in the inventory.
*/
@Override
public int getSizeInventory()
{
return this.furnaceItemStacks.size();
}
@Override
public boolean isEmpty()
{
for (ItemStack itemstack : this.furnaceItemStacks)
{
if (!itemstack.isEmpty())
{
return false;
}
}
return true;
}
/**
* Returns the stack in the given slot.
*/
@Override
public ItemStack getStackInSlot(int index)
{
return this.furnaceItemStacks.get(index);
}
/**
* Removes up to a specified number of items from an inventory slot and returns them in a new stack.
*/
@Override
public ItemStack decrStackSize(int index, int count)
{
return ItemStackHelper.getAndSplit(this.furnaceItemStacks, index, count);
}
/**
* Removes a stack from the given slot and returns it.
*/
@Override
public ItemStack removeStackFromSlot(int index)
{
return ItemStackHelper.getAndRemove(this.furnaceItemStacks, index);
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
@Override
public void setInventorySlotContents(int index, ItemStack stack)
{
ItemStack itemstack = this.furnaceItemStacks.get(index);
boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
this.furnaceItemStacks.set(index, stack);
if (stack.getCount() > this.getInventoryStackLimit())
{
stack.setCount(this.getInventoryStackLimit());
}
if (index == 0 && !flag)
{
this.totalCookTime = this.getCookTime(stack);
this.cookTime = 0;
this.markDirty();
}
}
/**
* Get the name of this object. For players this returns their username
*/
@Override
public String getName()
{
return this.hasCustomName() ? this.furnaceCustomName : "container.furnace";
}
/**
* Returns true if this thing is named
*/
@Override
public boolean hasCustomName()
{
return this.furnaceCustomName != null && !this.furnaceCustomName.isEmpty();
}
public void setCustomInventoryName(String p_145951_1_)
{
this.furnaceCustomName = p_145951_1_;
}
@Override
public void readFromNBT(NBTTagCompound compound)
{
super.readFromNBT(compound);
this.furnaceItemStacks = NonNullList.<ItemStack> withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(compound, this.furnaceItemStacks);
this.furnaceBurnTime = compound.getInteger("BurnTime");
this.cookTime = compound.getInteger("CookTime");
this.totalCookTime = compound.getInteger("CookTimeTotal");
this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks.get(1));
if (compound.hasKey("CustomName", 8))
{
this.furnaceCustomName = compound.getString("CustomName");
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound)
{
super.writeToNBT(compound);
compound.setInteger("BurnTime", (short) this.furnaceBurnTime);
compound.setInteger("CookTime", (short) this.cookTime);
compound.setInteger("CookTimeTotal", (short) this.totalCookTime);
ItemStackHelper.saveAllItems(compound, this.furnaceItemStacks);
if (this.hasCustomName())
{
compound.setString("CustomName", this.furnaceCustomName);
}
return compound;
}
/**
* Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
*/
@Override
public int getInventoryStackLimit()
{
return 64;
}
/**
* Furnace isBurning
*/
public boolean isBurning()
{
return this.furnaceBurnTime > 0;
}
@SideOnly(Side.CLIENT)
public static boolean isBurning(IInventory inventory)
{
return inventory.getField(0) > 0;
}
/**
* Like the old updateEntity(), except more generic.
*/
@Override
public void update()
{
boolean flag = this.isBurning();
boolean flag1 = false;
if (this.isBurning())
{
--this.furnaceBurnTime;
}
if (!this.world.isRemote)
{
ItemStack itemstack = this.furnaceItemStacks.get(1);
if (this.isBurning() || !itemstack.isEmpty() && !this.furnaceItemStacks.get(0).isEmpty())
{
if (!this.isBurning() && this.canSmelt())
{
this.furnaceBurnTime = getItemBurnTime(itemstack) * 2;
this.currentItemBurnTime = this.furnaceBurnTime;
if (this.isBurning())
{
flag1 = true;
if (!itemstack.isEmpty())
{
Item item = itemstack.getItem();
itemstack.shrink(1);
if (itemstack.isEmpty())
{
ItemStack item1 = item.getContainerItem(itemstack);
this.furnaceItemStacks.set(1, item1);
}
}
}
}
if (this.isBurning() && this.canSmelt())
{
++this.cookTime;
if (this.cookTime == this.totalCookTime)
{
this.cookTime = 0;
this.totalCookTime = this.getCookTime(this.furnaceItemStacks.get(0));
this.smeltItem();
flag1 = true;
}
}
else
{
this.cookTime = 0;
}
}
else if (!this.isBurning() && this.cookTime > 0)
{
this.cookTime = MathHelper.clamp(this.cookTime - 2, 0, this.totalCookTime);
}
if (flag != this.isBurning())
{
flag1 = true;
BlockNetherrackFurnace.setState(this.isBurning(), this.world, this.pos);
}
}
if (flag1)
{
this.markDirty();
}
}
public int getCookTime(ItemStack stack)
{
return 200;
}
/**
* Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
*/
private boolean canSmelt()
{
if (this.furnaceItemStacks.get(0).isEmpty())
{
return false;
}
else
{
ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks.get(0));
if (itemstack.isEmpty())
{
return false;
}
else
{
ItemStack itemstack1 = this.furnaceItemStacks.get(2);
if (itemstack1.isEmpty())
{
return true;
}
else if (!itemstack1.isItemEqual(itemstack))
{
return false;
}
else if (itemstack1.getCount() + itemstack.getCount() <= this.getInventoryStackLimit() && itemstack1.getCount() + itemstack.getCount() <= itemstack1.getMaxStackSize()) // Forge fix: make furnace respect stack sizes in furnace recipes
{
return true;
}
else
{
return itemstack1.getCount() + itemstack.getCount() <= itemstack.getMaxStackSize(); // Forge fix: make furnace respect stack sizes in furnace recipes
}
}
}
}
/**
* Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
*/
public void smeltItem()
{
if (this.canSmelt())
{
ItemStack itemstack = this.furnaceItemStacks.get(0);
ItemStack itemstack1 = FurnaceRecipes.instance().getSmeltingResult(itemstack);
ItemStack itemstack2 = this.furnaceItemStacks.get(2);
if (itemstack2.isEmpty())
{
this.furnaceItemStacks.set(2, itemstack1.copy());
}
else if (itemstack2.getItem() == itemstack1.getItem())
{
itemstack2.grow(itemstack1.getCount());
}
if (itemstack.getItem() == Item.getItemFromBlock(Blocks.SPONGE) && itemstack.getMetadata() == 1 && !this.furnaceItemStacks.get(1).isEmpty() && this.furnaceItemStacks.get(1).getItem() == Items.BUCKET)
{
this.furnaceItemStacks.set(1, new ItemStack(Items.WATER_BUCKET));
}
itemstack.shrink(1);
}
}
/**
* Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
* fuel
*/
public static int getItemBurnTime(ItemStack stack)
{
if (stack.isEmpty())
{
return 0;
}
else
{
int burnTime = net.minecraftforge.event.ForgeEventFactory.getItemBurnTime(stack);
if (burnTime >= 0)
{
return burnTime;
}
Item item = stack.getItem();
if (item == Item.getItemFromBlock(Blocks.WOODEN_SLAB))
{
return 150;
}
else if (item == Item.getItemFromBlock(Blocks.WOOL))
{
return 100;
}
else if (item == Item.getItemFromBlock(Blocks.CARPET))
{
return 67;
}
else if (item == Item.getItemFromBlock(Blocks.LADDER))
{
return 300;
}
else if (item == Item.getItemFromBlock(Blocks.WOODEN_BUTTON))
{
return 100;
}
else if (Block.getBlockFromItem(item).getDefaultState().getMaterial() == Material.WOOD)
{
return 300;
}
else if (item == Item.getItemFromBlock(Blocks.COAL_BLOCK))
{
return 16000;
}
else if (item instanceof ItemTool && "WOOD".equals(((ItemTool) item).getToolMaterialName()))
{
return 200;
}
else if (item instanceof ItemSword && "WOOD".equals(((ItemSword) item).getToolMaterialName()))
{
return 200;
}
else if (item instanceof ItemHoe && "WOOD".equals(((ItemHoe) item).getMaterialName()))
{
return 200;
}
else if (item == Items.STICK)
{
return 100;
}
else if (item != Items.BOW && item != Items.FISHING_ROD)
{
if (item == Items.SIGN)
{
return 200;
}
else if (item == Items.COAL)
{
return 1600;
}
else if (item == Items.LAVA_BUCKET)
{
return 20000;
}
else if (item != Item.getItemFromBlock(Blocks.SAPLING) && item != Items.BOWL)
{
if (item == Items.BLAZE_ROD)
{
return 2400;
}
else if (item instanceof ItemDoor && item != Items.IRON_DOOR)
{
return 200;
}
else
{
return item instanceof ItemBoat ? 400 : 0;
}
}
else
{
return 100;
}
}
else
{
return 300;
}
}
}
public static boolean isItemFuel(ItemStack stack)
{
return getItemBurnTime(stack) > 0;
}
/**
* Don't rename this method to canInteractWith due to conflicts with Container
*/
@Override
public boolean isUsableByPlayer(EntityPlayer player)
{
if (this.world.getTileEntity(this.pos) != this)
{
return false;
}
else
{
return player.getDistanceSq(this.pos.getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D) <= 64.0D;
}
}
@Override
public void openInventory(EntityPlayer player)
{
}
@Override
public void closeInventory(EntityPlayer player)
{
}
/**
* Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot. For
* guis use Slot.isItemValid
*/
@Override
public boolean isItemValidForSlot(int index, ItemStack stack)
{
if (index == 2)
{
return false;
}
else if (index != 1)
{
return true;
}
else
{
ItemStack itemstack = this.furnaceItemStacks.get(1);
return isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack) && itemstack.getItem() != Items.BUCKET;
}
}
@Override
public int[] getSlotsForFace(EnumFacing side)
{
if (side == EnumFacing.DOWN)
{
return SLOTS_BOTTOM;
}
else
{
return side == EnumFacing.UP ? SLOTS_TOP : SLOTS_SIDES;
}
}
/**
* Returns true if automation can insert the given item in the given slot from the given side.
*/
@Override
public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
{
return this.isItemValidForSlot(index, itemStackIn);
}
/**
* Returns true if automation can extract the given item in the given slot from the given side.
*/
@Override
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
{
if (direction == EnumFacing.DOWN && index == 1)
{
Item item = stack.getItem();
if (item != Items.WATER_BUCKET && item != Items.BUCKET)
{
return false;
}
}
return true;
}
@Override
public String getGuiID()
{
return "natura:netherrack_furnace";
}
@Override
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
{
return new FurnaceContainer(playerInventory, this);
}
@Override
public int getField(int id)
{
switch (id)
{
case 0:
return this.furnaceBurnTime;
case 1:
return this.currentItemBurnTime;
case 2:
return this.cookTime;
case 3:
return this.totalCookTime;
default:
return 0;
}
}
@Override
public void setField(int id, int value)
{
switch (id)
{
case 0:
this.furnaceBurnTime = value;
break;
case 1:
this.currentItemBurnTime = value;
break;
case 2:
this.cookTime = value;
break;
case 3:
this.totalCookTime = value;
}
}
@Override
public int getFieldCount()
{
return 4;
}
@Override
public void clear()
{
this.furnaceItemStacks.clear();
}
net.minecraftforge.items.IItemHandler handlerTop = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.UP);
net.minecraftforge.items.IItemHandler handlerBottom = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.DOWN);
net.minecraftforge.items.IItemHandler handlerSide = new net.minecraftforge.items.wrapper.SidedInvWrapper(this, net.minecraft.util.EnumFacing.WEST);
@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
{
if (facing != null && capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
{
if (facing == EnumFacing.DOWN)
{
return (T) this.handlerBottom;
}
else if (facing == EnumFacing.UP)
{
return (T) this.handlerTop;
}
else
{
return (T) this.handlerSide;
}
}
return super.getCapability(capability, facing);
}
}