From 1de54daf5de36519952578f0a6c8cd851badd8e1 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Fri, 30 Aug 2019 22:18:36 +0200 Subject: [PATCH] Refactoring --- .../multiblocks/AutomatedPanningMachine.java | 68 +++++++++++++++++++ .../Slimefun/Setup/SlimefunSetup.java | 44 +----------- 2 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/multiblocks/AutomatedPanningMachine.java diff --git a/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/multiblocks/AutomatedPanningMachine.java b/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/multiblocks/AutomatedPanningMachine.java new file mode 100644 index 000000000..8e2b67c6a --- /dev/null +++ b/src/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/multiblocks/AutomatedPanningMachine.java @@ -0,0 +1,68 @@ +package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.multiblocks; + +import java.util.Random; + +import org.bukkit.Bukkit; +import org.bukkit.Effect; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import me.mrCookieSlime.CSCoreLibPlugin.general.Player.PlayerInventory; +import me.mrCookieSlime.Slimefun.SlimefunStartup; +import me.mrCookieSlime.Slimefun.Lists.Categories; +import me.mrCookieSlime.Slimefun.Lists.SlimefunItems; +import me.mrCookieSlime.Slimefun.Setup.Messages; +import me.mrCookieSlime.Slimefun.api.Slimefun; + +public class AutomatedPanningMachine extends MultiBlockMachine { + + private Random random = new Random(); + + public AutomatedPanningMachine() { + super( + Categories.MACHINES_1, + SlimefunItems.AUTOMATED_PANNING_MACHINE, + "AUTOMATED_PANNING_MACHINE", + new ItemStack[] {null, null, null, null, new ItemStack(Material.OAK_TRAPDOOR), null, null, new ItemStack(Material.CAULDRON), null}, + new ItemStack[] { + new ItemStack(Material.GRAVEL), new ItemStack(Material.FLINT), + new ItemStack(Material.GRAVEL), new ItemStack(Material.CLAY_BALL), + new ItemStack(Material.GRAVEL), SlimefunItems.SIFTED_ORE + }, + Material.OAK_TRAPDOOR + ); + } + + @Override + public void onInteract(Player p, Block b) { + final ItemStack input = p.getInventory().getItemInMainHand(); + ItemStack output = null; + + if (random.nextInt(100) < (Integer) Slimefun.getItemValue("GOLD_PAN", "chance.SIFTED_ORE")) output = SlimefunItems.SIFTED_ORE; + else if (random.nextInt(100) < (Integer) Slimefun.getItemValue("GOLD_PAN", "chance.CLAY")) output = new ItemStack(Material.CLAY_BALL); + else if (random.nextInt(100) < (Integer) Slimefun.getItemValue("GOLD_PAN", "chance.FLINT")) output = new ItemStack(Material.FLINT); + + final ItemStack drop = output; + + if (input != null && input.getType() == Material.GRAVEL) { + PlayerInventory.consumeItemInHand(p); + for (int i = 1; i < 7; i++) { + int j = i; + Bukkit.getScheduler().runTaskLater(SlimefunStartup.instance, () -> { + b.getWorld().playEffect(b.getRelative(BlockFace.DOWN).getLocation(), Effect.STEP_SOUND, Material.GRAVEL); + if (j == 6) { + if (drop != null) b.getWorld().dropItemNaturally(b.getLocation(), drop); + p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F); + } + }, i * 30L); + } + } + + Messages.local.sendTranslation(p, "machines.wrong-item", true); + } + +} diff --git a/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java b/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java index fa8879cac..20758fbd4 100644 --- a/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java +++ b/src/me/mrCookieSlime/Slimefun/Setup/SlimefunSetup.java @@ -141,6 +141,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.TrashCan; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.WitherAssembler; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.XPCollector; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.multiblocks.ArmorForge; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.multiblocks.AutomatedPanningMachine; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.multiblocks.Compressor; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.multiblocks.EnhancedCraftingTable; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.multiblocks.GrindStone; @@ -1537,48 +1538,7 @@ public final class SlimefunSetup { } }); - new SlimefunMachine(Categories.MACHINES_1, SlimefunItems.AUTOMATED_PANNING_MACHINE, "AUTOMATED_PANNING_MACHINE", - new ItemStack[] {null, null, null, null, new ItemStack(Material.OAK_TRAPDOOR), null, null, new ItemStack(Material.CAULDRON), null}, - new ItemStack[] {new ItemStack(Material.GRAVEL), new ItemStack(Material.FLINT), new ItemStack(Material.GRAVEL), new ItemStack(Material.CLAY_BALL), new ItemStack(Material.GRAVEL), SlimefunItems.SIFTED_ORE}, Material.OAK_TRAPDOOR) - .register(true, new MultiBlockInteractionHandler() { - - private Random random = new Random(); - - @Override - public boolean onInteract(final Player p, MultiBlock mb, final Block b) { - if (mb.isMultiBlock(SlimefunItem.getByID("AUTOMATED_PANNING_MACHINE"))) { - if (CSCoreLib.getLib().getProtectionManager().canAccessChest(p.getUniqueId(), b, true)) { - final ItemStack input = p.getInventory().getItemInMainHand(); - ItemStack output = null; - - if (random.nextInt(100) < (Integer) Slimefun.getItemValue("GOLD_PAN", "chance.SIFTED_ORE")) output = SlimefunItems.SIFTED_ORE; - else if (random.nextInt(100) < (Integer) Slimefun.getItemValue("GOLD_PAN", "chance.CLAY")) output = new ItemStack(Material.CLAY_BALL); - else if (random.nextInt(100) < (Integer) Slimefun.getItemValue("GOLD_PAN", "chance.FLINT")) output = new ItemStack(Material.FLINT); - - final ItemStack drop = output; - if (input != null && input.getType() == Material.GRAVEL) { - PlayerInventory.consumeItemInHand(p); - for (int i = 1; i < 7; i++) { - int j = i; - Bukkit.getScheduler().runTaskLater(SlimefunStartup.instance, () -> { - b.getWorld().playEffect(b.getRelative(BlockFace.DOWN).getLocation(), Effect.STEP_SOUND, Material.GRAVEL); - if (j == 6) { - if (drop != null) b.getWorld().dropItemNaturally(b.getLocation(), drop); - p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F); - } - }, i*30L); - } - return true; - } - - Messages.local.sendTranslation(p, "machines.wrong-item", true); - return true; - } - return true; - } - else return false; - } - }); + new AutomatedPanningMachine().register(); new SlimefunItem(Categories.MAGIC_ARMOR, SlimefunItems.BOOTS_OF_THE_STOMPER, "BOOTS_OF_THE_STOMPER", RecipeType.ARMOR_FORGE, new ItemStack[] {null, null, null, new ItemStack(Material.YELLOW_WOOL), null, new ItemStack(Material.YELLOW_WOOL), new ItemStack(Material.PISTON), null, new ItemStack(Material.PISTON)})