TileEntityFurnace .java

net.minecraft.tileentity.TileEntityFurnace (class): members: setCustomInventoryName, furnaceCustomName, furnaceItemStacks, getItemBurnTime, getCookTime.

net.minecraft.tileentityclass523 linescommonextends TileEntityLockable15.7 KB

Implements

API Surface (31)

int getSizeInventory()
ItemStack getStackInSlot(int index)
ItemStack decrStackSize(int index, int count)
ItemStack removeStackFromSlot(int index)
void setInventorySlotContents(int index, ItemStack stack)
String getName()
boolean hasCustomName()
void setCustomInventoryName(String p_145951_1_)
void readFromNBT(NBTTagCompound compound)
void writeToNBT(NBTTagCompound compound)
int getInventoryStackLimit()
boolean isBurning()
boolean isBurning(IInventory p_174903_0_)
void update()
int getCookTime(ItemStack stack)
void smeltItem()
int getItemBurnTime(ItemStack p_145952_0_)
boolean isItemFuel(ItemStack p_145954_0_)
boolean isUseableByPlayer(EntityPlayer player)
void openInventory(EntityPlayer player)
void closeInventory(EntityPlayer player)
boolean isItemValidForSlot(int index, ItemStack stack)
int[] getSlotsForFace(EnumFacing side)
boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
String getGuiID()
Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
int getField(int id)
void setField(int id, int value)
int getFieldCount()
void clear()

Fields & Constants (9)

slotsTop
slotsBottom
slotsSides
furnaceItemStacks
furnaceBurnTime
currentItemBurnTime
cookTime
totalCookTime
furnaceCustomName

String literals (9)

"container.furnace"
"Items"
"Slot"
"BurnTime"
"CookTime"
"CookTimeTotal"
"CustomName"
"WOOD"
"minecraft:furnace"

Full Source

TileEntityFurnace.java523 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.tileentity;
2
3
import net.minecraft.block.Block;
4
import net.minecraft.block.BlockFurnace;
5
import net.minecraft.block.material.Material;
6
import net.minecraft.entity.player.EntityPlayer;
7
import net.minecraft.entity.player.InventoryPlayer;
8
import net.minecraft.init.Blocks;
9
import net.minecraft.init.Items;
10
import net.minecraft.inventory.Container;
11
import net.minecraft.inventory.ContainerFurnace;
12
import net.minecraft.inventory.IInventory;
13
import net.minecraft.inventory.ISidedInventory;
14
import net.minecraft.inventory.SlotFurnaceFuel;
15
import net.minecraft.item.Item;
16
import net.minecraft.item.ItemBlock;
17
import net.minecraft.item.ItemHoe;
18
import net.minecraft.item.ItemStack;
19
import net.minecraft.item.ItemSword;
20
import net.minecraft.item.ItemTool;
21
import net.minecraft.item.crafting.FurnaceRecipes;
22
import net.minecraft.nbt.NBTTagCompound;
23
import net.minecraft.nbt.NBTTagList;
24
import net.minecraft.util.EnumFacing;
25
import net.minecraft.util.ITickable;
26
import net.minecraft.util.MathHelper;
27
28
public class TileEntityFurnace extends TileEntityLockable implements ITickable, ISidedInventory
29
{
30
    private static final int[] slotsTop = new int[] {0};
31
    private static final int[] slotsBottom = new int[] {2, 1};
32
    private static final int[] slotsSides = new int[] {1};
33
34
    /**
35
     * The ItemStacks that hold the items currently being used in the furnace
36
     */
37
    private ItemStack[] furnaceItemStacks = new ItemStack[3];
38
39
    /** The number of ticks that the furnace will keep burning */
40
    private int furnaceBurnTime;
41
42
    /**
43
     * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for
44
     */
45
    private int currentItemBurnTime;
46
    private int cookTime;
47
    private int totalCookTime;
48
    private String furnaceCustomName;
49
50
    /**
51
     * Returns the number of slots in the inventory.
52
     */
53
    public int getSizeInventory()
54
    {
55
        return this.furnaceItemStacks.length;
56
    }
57
58
    /**
59
     * Returns the stack in the given slot.
60
     */
61
    public ItemStack getStackInSlot(int index)
62
    {
63
        return this.furnaceItemStacks[index];
64
    }
65
66
    /**
67
     * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
68
     */
69
    public ItemStack decrStackSize(int index, int count)
70
    {
71
        if (this.furnaceItemStacks[index] != null)
72
        {
73
            if (this.furnaceItemStacks[index].stackSize <= count)
74
            {
75
                ItemStack itemstack1 = this.furnaceItemStacks[index];
76
                this.furnaceItemStacks[index] = null;
77
                return itemstack1;
78
            }
79
            else
80
            {
81
                ItemStack itemstack = this.furnaceItemStacks[index].splitStack(count);
82
83
                if (this.furnaceItemStacks[index].stackSize == 0)
84
                {
85
                    this.furnaceItemStacks[index] = null;
86
                }
87
88
                return itemstack;
89
            }
90
        }
91
        else
92
        {
93
            return null;
94
        }
95
    }
96
97
    /**
98
     * Removes a stack from the given slot and returns it.
99
     */
100
    public ItemStack removeStackFromSlot(int index)
101
    {
102
        if (this.furnaceItemStacks[index] != null)
103
        {
104
            ItemStack itemstack = this.furnaceItemStacks[index];
105
            this.furnaceItemStacks[index] = null;
106
            return itemstack;
107
        }
108
        else
109
        {
110
            return null;
111
        }
112
    }
113
114
    /**
115
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
116
     */
117
    public void setInventorySlotContents(int index, ItemStack stack)
118
    {
119
        boolean flag = stack != null && stack.isItemEqual(this.furnaceItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.furnaceItemStacks[index]);
120
        this.furnaceItemStacks[index] = stack;
121
122
        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
123
        {
124
            stack.stackSize = this.getInventoryStackLimit();
125
        }
126
127
        if (index == 0 && !flag)
128
        {
129
            this.totalCookTime = this.getCookTime(stack);
130
            this.cookTime = 0;
131
            this.markDirty();
132
        }
133
    }
134
135
    /**
136
     * Get the name of this object. For players this returns their username
137
     */
138
    public String getName()
139
    {
140
        return this.hasCustomName() ? this.furnaceCustomName : "container.furnace";
141
    }
142
143
    /**
144
     * Returns true if this thing is named
145
     */
146
    public boolean hasCustomName()
147
    {
148
        return this.furnaceCustomName != null && this.furnaceCustomName.length() > 0;
149
    }
150
151
    public void setCustomInventoryName(String p_145951_1_)
152
    {
153
        this.furnaceCustomName = p_145951_1_;
154
    }
155
156
    public void readFromNBT(NBTTagCompound compound)
157
    {
158
        super.readFromNBT(compound);
159
        NBTTagList nbttaglist = compound.getTagList("Items", 10);
160
        this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
161
162
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
163
        {
164
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
165
            int j = nbttagcompound.getByte("Slot");
166
167
            if (j >= 0 && j < this.furnaceItemStacks.length)
168
            {
169
                this.furnaceItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
170
            }
171
        }
172
173
        this.furnaceBurnTime = compound.getShort("BurnTime");
174
        this.cookTime = compound.getShort("CookTime");
175
        this.totalCookTime = compound.getShort("CookTimeTotal");
176
        this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
177
178
        if (compound.hasKey("CustomName", 8))
179
        {
180
            this.furnaceCustomName = compound.getString("CustomName");
181
        }
182
    }
183
184
    public void writeToNBT(NBTTagCompound compound)
185
    {
186
        super.writeToNBT(compound);
187
        compound.setShort("BurnTime", (short)this.furnaceBurnTime);
188
        compound.setShort("CookTime", (short)this.cookTime);
189
        compound.setShort("CookTimeTotal", (short)this.totalCookTime);
190
        NBTTagList nbttaglist = new NBTTagList();
191
192
        for (int i = 0; i < this.furnaceItemStacks.length; ++i)
193
        {
194
            if (this.furnaceItemStacks[i] != null)
195
            {
196
                NBTTagCompound nbttagcompound = new NBTTagCompound();
197
                nbttagcompound.setByte("Slot", (byte)i);
198
                this.furnaceItemStacks[i].writeToNBT(nbttagcompound);
199
                nbttaglist.appendTag(nbttagcompound);
200
            }
201
        }
202
203
        compound.setTag("Items", nbttaglist);
204
205
        if (this.hasCustomName())
206
        {
207
            compound.setString("CustomName", this.furnaceCustomName);
208
        }
209
    }
210
211
    /**
212
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
213
     */
214
    public int getInventoryStackLimit()
215
    {
216
        return 64;
217
    }
218
219
    /**
220
     * Furnace isBurning
221
     */
222
    public boolean isBurning()
223
    {
224
        return this.furnaceBurnTime > 0;
225
    }
226
227
    public static boolean isBurning(IInventory p_174903_0_)
228
    {
229
        return p_174903_0_.getField(0) > 0;
230
    }
231
232
    /**
233
     * Like the old updateEntity(), except more generic.
234
     */
235
    public void update()
236
    {
237
        boolean flag = this.isBurning();
238
        boolean flag1 = false;
239
240
        if (this.isBurning())
241
        {
242
            --this.furnaceBurnTime;
243
        }
244
245
        if (!this.worldObj.isRemote)
246
        {
247
            if (this.isBurning() || this.furnaceItemStacks[1] != null && this.furnaceItemStacks[0] != null)
248
            {
249
                if (!this.isBurning() && this.canSmelt())
250
                {
251
                    this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
252
253
                    if (this.isBurning())
254
                    {
255
                        flag1 = true;
256
257
                        if (this.furnaceItemStacks[1] != null)
258
                        {
259
                            --this.furnaceItemStacks[1].stackSize;
260
261
                            if (this.furnaceItemStacks[1].stackSize == 0)
262
                            {
263
                                Item item = this.furnaceItemStacks[1].getItem().getContainerItem();
264
                                this.furnaceItemStacks[1] = item != null ? new ItemStack(item) : null;
265
                            }
266
                        }
267
                    }
268
                }
269
270
                if (this.isBurning() && this.canSmelt())
271
                {
272
                    ++this.cookTime;
273
274
                    if (this.cookTime == this.totalCookTime)
275
                    {
276
                        this.cookTime = 0;
277
                        this.totalCookTime = this.getCookTime(this.furnaceItemStacks[0]);
278
                        this.smeltItem();
279
                        flag1 = true;
280
                    }
281
                }
282
                else
283
                {
284
                    this.cookTime = 0;
285
                }
286
            }
287
            else if (!this.isBurning() && this.cookTime > 0)
288
            {
289
                this.cookTime = MathHelper.clamp_int(this.cookTime - 2, 0, this.totalCookTime);
290
            }
291
292
            if (flag != this.isBurning())
293
            {
294
                flag1 = true;
295
                BlockFurnace.setState(this.isBurning(), this.worldObj, this.pos);
296
            }
297
        }
298
299
        if (flag1)
300
        {
301
            this.markDirty();
302
        }
303
    }
304
305
    public int getCookTime(ItemStack stack)
306
    {
307
        return 200;
308
    }
309
310
    /**
311
     * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
312
     */
313
    private boolean canSmelt()
314
    {
315
        if (this.furnaceItemStacks[0] == null)
316
        {
317
            return false;
318
        }
319
        else
320
        {
321
            ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
322
            return itemstack == null ? false : (this.furnaceItemStacks[2] == null ? true : (!this.furnaceItemStacks[2].isItemEqual(itemstack) ? false : (this.furnaceItemStacks[2].stackSize < this.getInventoryStackLimit() && this.furnaceItemStacks[2].stackSize < this.furnaceItemStacks[2].getMaxStackSize() ? true : this.furnaceItemStacks[2].stackSize < itemstack.getMaxStackSize())));
323
        }
324
    }
325
326
    /**
327
     * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
328
     */
329
    public void smeltItem()
330
    {
331
        if (this.canSmelt())
332
        {
333
            ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.furnaceItemStacks[0]);
334
335
            if (this.furnaceItemStacks[2] == null)
336
            {
337
                this.furnaceItemStacks[2] = itemstack.copy();
338
            }
339
            else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
340
            {
341
                ++this.furnaceItemStacks[2].stackSize;
342
            }
343
344
            if (this.furnaceItemStacks[0].getItem() == Item.getItemFromBlock(Blocks.sponge) && this.furnaceItemStacks[0].getMetadata() == 1 && this.furnaceItemStacks[1] != null && this.furnaceItemStacks[1].getItem() == Items.bucket)
345
            {
346
                this.furnaceItemStacks[1] = new ItemStack(Items.water_bucket);
347
            }
348
349
            --this.furnaceItemStacks[0].stackSize;
350
351
            if (this.furnaceItemStacks[0].stackSize <= 0)
352
            {
353
                this.furnaceItemStacks[0] = null;
354
            }
355
        }
356
    }
357
358
    /**
359
     * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
360
     * fuel
361
     */
362
    public static int getItemBurnTime(ItemStack p_145952_0_)
363
    {
364
        if (p_145952_0_ == null)
365
        {
366
            return 0;
367
        }
368
        else
369
        {
370
            Item item = p_145952_0_.getItem();
371
372
            if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air)
373
            {
374
                Block block = Block.getBlockFromItem(item);
375
376
                if (block == Blocks.wooden_slab)
377
                {
378
                    return 150;
379
                }
380
381
                if (block.getMaterial() == Material.wood)
382
                {
383
                    return 300;
384
                }
385
386
                if (block == Blocks.coal_block)
387
                {
388
                    return 16000;
389
                }
390
            }
391
392
            return item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD") ? 200 : (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD") ? 200 : (item instanceof ItemHoe && ((ItemHoe)item).getMaterialName().equals("WOOD") ? 200 : (item == Items.stick ? 100 : (item == Items.coal ? 1600 : (item == Items.lava_bucket ? 20000 : (item == Item.getItemFromBlock(Blocks.sapling) ? 100 : (item == Items.blaze_rod ? 2400 : 0)))))));
393
        }
394
    }
395
396
    public static boolean isItemFuel(ItemStack p_145954_0_)
397
    {
398
        return getItemBurnTime(p_145954_0_) > 0;
399
    }
400
401
    /**
402
     * Do not make give this method the name canInteractWith because it clashes with Container
403
     */
404
    public boolean isUseableByPlayer(EntityPlayer player)
405
    {
406
        return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
407
    }
408
409
    public void openInventory(EntityPlayer player)
410
    {
411
    }
412
413
    public void closeInventory(EntityPlayer player)
414
    {
415
    }
416
417
    /**
418
     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
419
     */
420
    public boolean isItemValidForSlot(int index, ItemStack stack)
421
    {
422
        return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack));
423
    }
424
425
    public int[] getSlotsForFace(EnumFacing side)
426
    {
427
        return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
428
    }
429
430
    /**
431
     * Returns true if automation can insert the given item in the given slot from the given side. Args: slot, item,
432
     * side
433
     */
434
    public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
435
    {
436
        return this.isItemValidForSlot(index, itemStackIn);
437
    }
438
439
    /**
440
     * Returns true if automation can extract the given item in the given slot from the given side. Args: slot, item,
441
     * side
442
     */
443
    public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
444
    {
445
        if (direction == EnumFacing.DOWN && index == 1)
446
        {
447
            Item item = stack.getItem();
448
449
            if (item != Items.water_bucket && item != Items.bucket)
450
            {
451
                return false;
452
            }
453
        }
454
455
        return true;
456
    }
457
458
    public String getGuiID()
459
    {
460
        return "minecraft:furnace";
461
    }
462
463
    public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
464
    {
465
        return new ContainerFurnace(playerInventory, this);
466
    }
467
468
    public int getField(int id)
469
    {
470
        switch (id)
471
        {
472
            case 0:
473
                return this.furnaceBurnTime;
474
475
            case 1:
476
                return this.currentItemBurnTime;
477
478
            case 2:
479
                return this.cookTime;
480
481
            case 3:
482
                return this.totalCookTime;
483
484
            default:
485
                return 0;
486
        }
487
    }
488
489
    public void setField(int id, int value)
490
    {
491
        switch (id)
492
        {
493
            case 0:
494
                this.furnaceBurnTime = value;
495
                break;
496
497
            case 1:
498
                this.currentItemBurnTime = value;
499
                break;
500
501
            case 2:
502
                this.cookTime = value;
503
                break;
504
505
            case 3:
506
                this.totalCookTime = value;
507
        }
508
    }
509
510
    public int getFieldCount()
511
    {
512
        return 4;
513
    }
514
515
    public void clear()
516
    {
517
        for (int i = 0; i < this.furnaceItemStacks.length; ++i)
518
        {
519
            this.furnaceItemStacks[i] = null;
520
        }
521
    }
522
}
523

Deobfuscated MCP 1.8.9 source of TileEntityFurnace from Marcelektro/MavenMCP-1.8.9 (net/minecraft/tileentity/TileEntityFurnace.java), stored verbatim.