Search in sources :

Example 1 with FurnaceBurnEvent

use of org.bukkit.event.inventory.FurnaceBurnEvent in project Glowstone by GlowstoneMC.

the class FurnaceEntity method burn.

// TODO: Change block on burning
public void burn() {
    GlowFurnaceInventory inv = (GlowFurnaceInventory) getInventory();
    if (burnTime > 0)
        burnTime--;
    boolean isBurnable = isBurnable();
    if (cookTime > 0 && isBurnable) {
        cookTime++;
    } else if (burnTime != 0) {
        cookTime = 0;
    }
    if (cookTime == 0 && isBurnable) {
        cookTime = 1;
    }
    if (burnTime == 0) {
        if (isBurnable) {
            CraftingManager cm = ((GlowServer) Bukkit.getServer()).getCraftingManager();
            FurnaceBurnEvent burnEvent = new FurnaceBurnEvent(block, inv.getFuel(), cm.getFuelTime(inv.getFuel().getType()));
            EventFactory.callEvent(burnEvent);
            if (!burnEvent.isCancelled() && burnEvent.isBurning()) {
                burnTime = (short) burnEvent.getBurnTime();
                burnTimeFuel = burnTime;
                if (inv.getFuel().getAmount() == 1) {
                    if (inv.getFuel().getType().equals(Material.LAVA_BUCKET)) {
                        inv.setFuel(new ItemStack(Material.BUCKET));
                    } else {
                        inv.setFuel(null);
                    }
                } else {
                    inv.getFuel().setAmount(inv.getFuel().getAmount() - 1);
                }
            } else if (cookTime != 0) {
                if (cookTime % 2 == 0) {
                    cookTime = (short) (cookTime - 2);
                } else {
                    cookTime--;
                }
            }
        } else if (cookTime != 0) {
            if (cookTime % 2 == 0) {
                cookTime = (short) (cookTime - 2);
            } else {
                cookTime--;
            }
        }
    }
    if (cookTime == 200) {
        CraftingManager cm = ((GlowServer) Bukkit.getServer()).getCraftingManager();
        Recipe recipe = cm.getFurnaceRecipe(inv.getSmelting());
        if (recipe != null) {
            FurnaceSmeltEvent smeltEvent = new FurnaceSmeltEvent(block, inv.getSmelting(), recipe.getResult());
            EventFactory.callEvent(smeltEvent);
            if (!smeltEvent.isCancelled()) {
                if (inv.getSmelting().getType().equals(Material.SPONGE) && inv.getSmelting().getData().getData() == 1 && inv.getFuel() != null && inv.getFuel().getType().equals(Material.BUCKET) && inv.getFuel().getAmount() == 1) {
                    inv.setFuel(new ItemStack(Material.WATER_BUCKET));
                }
                if (inv.getResult() == null || inv.getResult().getType().equals(Material.AIR)) {
                    inv.setResult(smeltEvent.getResult());
                } else if (inv.getResult().getType().equals(smeltEvent.getResult().getType())) {
                    inv.getResult().setAmount(inv.getResult().getAmount() + smeltEvent.getResult().getAmount());
                }
                if (inv.getSmelting().getAmount() == 1) {
                    inv.setSmelting(null);
                } else {
                    inv.getSmelting().setAmount(inv.getSmelting().getAmount() - 1);
                }
            }
            cookTime = 0;
        }
    }
    inv.getViewersSet().forEach(human -> {
        human.setWindowProperty(Property.BURN_TIME, burnTime);
        human.setWindowProperty(Property.TICKS_FOR_CURRENT_FUEL, burnTimeFuel);
        human.setWindowProperty(Property.COOK_TIME, cookTime);
        human.setWindowProperty(Property.TICKS_FOR_CURRENT_SMELTING, 200);
    });
    if (!isBurnable && burnTime == 0 && cookTime == 0) {
        getState().getBlock().getWorld().cancelPulse(getState().getBlock());
    }
}
Also used : FurnaceBurnEvent(org.bukkit.event.inventory.FurnaceBurnEvent) GlowFurnaceInventory(net.glowstone.inventory.GlowFurnaceInventory) Recipe(org.bukkit.inventory.Recipe) CraftingManager(net.glowstone.inventory.crafting.CraftingManager) GlowServer(net.glowstone.GlowServer) ItemStack(org.bukkit.inventory.ItemStack) FurnaceSmeltEvent(org.bukkit.event.inventory.FurnaceSmeltEvent)

Aggregations

GlowServer (net.glowstone.GlowServer)1 GlowFurnaceInventory (net.glowstone.inventory.GlowFurnaceInventory)1 CraftingManager (net.glowstone.inventory.crafting.CraftingManager)1 FurnaceBurnEvent (org.bukkit.event.inventory.FurnaceBurnEvent)1 FurnaceSmeltEvent (org.bukkit.event.inventory.FurnaceSmeltEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 Recipe (org.bukkit.inventory.Recipe)1