Bukkit's Furnace
I've been writing a Bukkit plugin recently and ended up using the furnace-related methods. Here are a few pitfalls I ran into.
Events
Bukkit has three furnace events:
FurnaceBurnEventFurnaceSmeltEventFurnaceExtractEvent
The first one, FurnaceBurnEvent, fires when the furnace starts burning something. You can get the furnace's current fuel ItemStack, get/set the remaining fuel amount, and set whether it burns (the remaining fuel amount is burnTime; setting it to 0 stops it from burning). But there's no way to get the item being smelted or the item it produces.
The second one, FurnaceSmeltEvent, fires after the furnace finishes smelting. You can get the ItemStacks of the item currently being smelted and the item after smelting. There's no way to get the fuel.
The third one, FurnaceExtractEvent, fires when a player takes something out of the furnace. Not much to say about that one.
FurnaceBurnEvent and FurnaceSmeltEvent are a bit of a pain. There's no way to get both the fuel and the item at the same time. If you want to get everything, you have to use the Furnace Block itself. Both events extend BlockEvent, so you can get the furnace block via getBlock().
Here's how to get it:
(Furnace) furnace = (Furnace) e.getBlock().getState();
furnace.getInventory();
This gets you the furnace's inventory, but there are still a few pitfalls.
Blocks
There are also a few pitfalls with the block side, continuing from above.
- A furnace has two inventories: one is inventory, the other is snapshotInventory. The latter is like a snapshot, but I don't really know what the difference between the two is. The pitfall is that when you want to modify things in the furnace inside an event, setting inventory seems to have no effect; you have to set snapshotInventory (it might be different in other situations).
- After modifying the furnace, you need to update it with
furnace.update(). - For casting, you need to cast the
BlockStateobject obtained fromgetBlock().getState(); you can't cast theBlockobject directly, or you'll get an error.
