From 9b5bcfde44a5aad1437be84a4ff634160f6653c3 Mon Sep 17 00:00:00 2001 From: SoSeDiK Date: Sun, 8 Dec 2019 15:24:22 +0200 Subject: [PATCH] Updated Crucible's mechanics --- .../SlimefunItem/machines/Crucible.java | 113 +++++++++++------- 1 file changed, 72 insertions(+), 41 deletions(-) diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/Crucible.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/Crucible.java index 06ecdbe1f..3fb9dc79e 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/Crucible.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/machines/Crucible.java @@ -9,12 +9,11 @@ import org.bukkit.Tag; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.data.Levelled; -import org.bukkit.entity.Player; +import org.bukkit.block.data.Waterlogged; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections; import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; -import me.mrCookieSlime.CSCoreLibPlugin.events.ItemUseEvent; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; @@ -59,59 +58,91 @@ public class Crucible extends SlimefunGadget { return items.toArray(new ItemStack[0]); } + @Override public void preRegister() { - addItemHandler(new ItemInteractionHandler() { + addItemHandler((ItemInteractionHandler) (e, p, item) -> { + if (e.getClickedBlock() != null) { + String id = BlockStorage.checkID(e.getClickedBlock()); + if (id != null && id.equals("CRUCIBLE")) { + if (p.hasPermission("slimefun.inventory.bypass") || SlimefunPlugin.getProtectionManager().hasPermission(p, e.getClickedBlock().getLocation(), ProtectableAction.ACCESS_INVENTORIES)) { + final ItemStack input = p.getInventory().getItemInMainHand(); + final Block block = e.getClickedBlock().getRelative(BlockFace.UP); + SlimefunItem machine = SlimefunItem.getByID(id); - @Override - public boolean onRightClick(ItemUseEvent e, final Player p, ItemStack item) { - if (e.getClickedBlock() != null) { - String id = BlockStorage.checkID(e.getClickedBlock()); - if (id != null && id.equals("CRUCIBLE")) { - if (p.hasPermission("slimefun.inventory.bypass") || SlimefunPlugin.getProtectionManager().hasPermission(p, e.getClickedBlock().getLocation(), ProtectableAction.ACCESS_INVENTORIES)) { - final ItemStack input = p.getInventory().getItemInMainHand(); - final Block block = e.getClickedBlock().getRelative(BlockFace.UP); - SlimefunItem machine = SlimefunItem.getByID(id); - - for (ItemStack convert: RecipeType.getRecipeInputs(machine)) { - if (SlimefunManager.isItemSimilar(input, convert, true)) { - e.setCancelled(true); - ItemStack removing = input.clone(); - removing.setAmount(convert.getAmount()); - - p.getInventory().removeItem(removing); + for (ItemStack convert: RecipeType.getRecipeInputs(machine)) { + if (SlimefunManager.isItemSimilar(input, convert, true)) { + e.setCancelled(true); - for (int i = 1; i < 9; i++) {int j = 8 - i; - Slimefun.runSync(() -> { - if (input.getType() == Material.COBBLESTONE || input.getType() == Material.TERRACOTTA || MaterialCollections.getAllTerracottaColors().contains(input.getType())) { - block.setType(Material.LAVA); - Levelled le = (Levelled) block.getBlockData(); - le.setLevel(j); - block.setBlockData(le, false); - block.getWorld().playSound(block.getLocation(), Sound.BLOCK_LAVA_POP, 1F, 1F); - } - else if (Tag.LEAVES.isTagged(input.getType())) { - block.setType(Material.WATER); - Levelled le = (Levelled) block.getBlockData(); - le.setLevel(j); - block.setBlockData(le, false); - block.getWorld().playSound(block.getLocation(), Sound.ENTITY_PLAYER_SPLASH, 1F, 1F); - } - }, i * 50L); + ItemStack removing = input.clone(); + removing.setAmount(convert.getAmount()); + p.getInventory().removeItem(removing); + + boolean water = Tag.LEAVES.isTagged(input.getType()); + if (block.getType() == (water ? Material.WATER : Material.LAVA)) { + int level = ((Levelled) block.getBlockData()).getLevel(); + if (level > 7) + level -= 8; + if (level == 0) { + block.getWorld().playSound(block.getLocation(), water ? Sound.ENTITY_PLAYER_SPLASH : Sound.BLOCK_LAVA_POP, 1F, 1F); + } else { + int finalLevel = 7 - level; + Slimefun.runSync(() -> runPostTask(block, water ? Sound.ENTITY_PLAYER_SPLASH : Sound.BLOCK_LAVA_POP, finalLevel), 50L); } - + return true; + } else if (block.getType() == (water ? Material.LAVA : Material.WATER)) { + int level = ((Levelled) block.getBlockData()).getLevel(); + block.setType(level == 0 || level == 8 ? Material.OBSIDIAN : Material.STONE); + block.getWorld().playSound(block.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1F, 1F); return true; } + + Slimefun.runSync(() -> { + if (!block.getType().isAir()) { + if (water) { + if (block.getBlockData() instanceof Waterlogged) { + Waterlogged wl = (Waterlogged) block.getBlockData(); + wl.setWaterlogged(true); + block.setBlockData(wl, false); + block.getWorld().playSound(block.getLocation(), Sound.ENTITY_PLAYER_SPLASH, 1F, 1F); + return; + } + block.getWorld().playSound(block.getLocation(), Sound.BLOCK_METAL_BREAK, 1F, 1F); + return; + } + if (BlockStorage.hasBlockInfo(block)) + BlockStorage.clearBlockInfo(block); + block.getWorld().playSound(block.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1F, 1F); + } + block.setType(water ? Material.WATER : Material.LAVA); + runPostTask(block, water ? Sound.ENTITY_PLAYER_SPLASH : Sound.BLOCK_LAVA_POP, 1); + }, 50L); + + return true; } - SlimefunPlugin.getLocal().sendMessage(p, "machines.wrong-item", true); - return true; } + SlimefunPlugin.getLocal().sendMessage(p, "machines.wrong-item", true); return true; } + return true; } - return false; } + return false; }); } + private void runPostTask(Block block, Sound sound, int times) { + if (!(block.getBlockData() instanceof Levelled)) { + block.getWorld().playSound(block.getLocation(), Sound.BLOCK_METAL_BREAK, 1F, 1F); + return; + } + block.getWorld().playSound(block.getLocation(), sound, 1F, 1F); + int level = 8 - times; + Levelled le = (Levelled) block.getBlockData(); + le.setLevel(level); + block.setBlockData(le, false); + if (times < 8) + Slimefun.runSync(() -> runPostTask(block, sound, times + 1), 50L); + } + }