From 057fad1f0847dacd342002c840919da1c1c9f655 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sat, 6 Feb 2021 00:46:55 +0100 Subject: [PATCH] Finished Auto-Crafters --- .../implementation/SlimefunItems.java | 5 ++ .../auto_crafters/AbstractAutoCrafter.java | 5 +- .../auto_crafters/VanillaAutoCrafter.java | 59 +++++++++++++++++-- .../setup/SlimefunItemSetup.java | 16 ++++- .../tasks/AsyncRecipeChoiceTask.java | 14 +++++ 5 files changed, 92 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java index 84d183605..a7b37fc84 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java @@ -841,10 +841,15 @@ public final class SlimefunItems { public static final SlimefunItemStack ELECTRIC_INGOT_FACTORY_2 = new SlimefunItemStack("ELECTRIC_INGOT_FACTORY_2", Material.RED_TERRACOTTA, "&cElectric Ingot Factory &7(&eII&7)", "", LoreBuilder.machine(MachineTier.BASIC, MachineType.MACHINE), LoreBuilder.speed(2), LoreBuilder.powerPerSecond(14)); public static final SlimefunItemStack ELECTRIC_INGOT_FACTORY_3 = new SlimefunItemStack("ELECTRIC_INGOT_FACTORY_3", Material.RED_TERRACOTTA, "&cElectric Ingot Factory &7(&eIII&7)", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), LoreBuilder.speed(8), LoreBuilder.powerPerSecond(40)); + @Deprecated public static final SlimefunItemStack AUTOMATED_CRAFTING_CHAMBER = new SlimefunItemStack("AUTOMATED_CRAFTING_CHAMBER", Material.CRAFTING_TABLE, "&6Automated Crafting Chamber", "", LoreBuilder.machine(MachineTier.ADVANCED, MachineType.MACHINE), "&8\u21E8 &e\u26A1 &710 J/Item"); + public static final SlimefunItemStack FLUID_PUMP = new SlimefunItemStack("FLUID_PUMP", Material.BLUE_TERRACOTTA, "&9Fluid Pump", "", LoreBuilder.machine(MachineTier.ADVANCED, MachineType.MACHINE), "&8\u21E8 &e\u26A1 &732 J/Block"); public static final SlimefunItemStack CHARGING_BENCH = new SlimefunItemStack("CHARGING_BENCH", Material.CRAFTING_TABLE, "&6Charging Bench", "", "&fCharges Items such as Jetpacks", "", LoreBuilder.machine(MachineTier.BASIC, MachineType.MACHINE), LoreBuilder.powerBuffer(128), "&8\u21E8 &e\u26A1 &7Energy Loss: &c50%"); + public static final SlimefunItemStack VANILLA_AUTO_CRAFTER = new SlimefunItemStack("VANILLA_AUTO_CRAFTER", HeadTexture.PORTABLE_CRAFTER, "&aAuto-Crafter &7(Vanilla)", "", "&fPlace this machine on top of a", "&fchest and make it craft anything", "&fthat can be crafted using a", "&fnormal &eCrafting Table", "", LoreBuilder.machine(MachineTier.ADVANCED, MachineType.MACHINE), "&8\u21E8 &e\u26A1 &716 J/Item"); + public static final SlimefunItemStack ENHANCED_AUTO_CRAFTER = new SlimefunItemStack("ENHANCED_AUTO_CRAFTER", HeadTexture.PORTABLE_CRAFTER, "&aAuto-Crafter &7(Enhanced)", "", "&fPlace this machine on top of a", "&fchest and make it craft anything", "&fthat can be crafted using an", "&eEnhanced Crafting Table", "", LoreBuilder.machine(MachineTier.ADVANCED, MachineType.MACHINE), "&8\u21E8 &e\u26A1 &716 J/Item"); + public static final SlimefunItemStack IRON_GOLEM_ASSEMBLER = new SlimefunItemStack("IRON_GOLEM_ASSEMBLER", Material.IRON_BLOCK, "&6Iron Golem Assembler", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), "&8\u21E8 &7Cooldown: &b30 Seconds", LoreBuilder.powerBuffer(4096), "&8\u21E8 &e\u26A1 &72048 J/Golem"); public static final SlimefunItemStack WITHER_ASSEMBLER = new SlimefunItemStack("WITHER_ASSEMBLER", Material.OBSIDIAN, "&5Wither Assembler", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), "&8\u21E8 &7Cooldown: &b30 Seconds", LoreBuilder.powerBuffer(4096), "&8\u21E8 &e\u26A1 &74096 J/Wither"); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java index 5b064b327..9a31a0221 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java @@ -9,6 +9,7 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang.Validate; +import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Sound; @@ -70,7 +71,7 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy protected final NamespacedKey recipeStorageKey; // @formatter:off - private final int[] background = { + protected final int[] background = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16, 17, 18, 19, 23, 25, 26, @@ -196,7 +197,7 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy ChestMenuUtils.drawBackground(menu, background); ChestMenuUtils.drawBackground(menu, 45, 46, 47, 48, 50, 51, 52, 53); - menu.addItem(49, new CustomItem(Material.BARRIER, SlimefunPlugin.getLocalization().getMessage(p, "messages.auto-crafting.remove"))); + menu.addItem(49, new CustomItem(Material.BARRIER, ChatColor.RED + SlimefunPlugin.getLocalization().getMessage(p, "messages.auto-crafting.remove"))); menu.addMenuClickHandler(49, (pl, item, slot, action) -> { setSelectedRecipe(b, null); pl.closeInventory(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaAutoCrafter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaAutoCrafter.java index 7aba79446..ed7ebe0c2 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaAutoCrafter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaAutoCrafter.java @@ -8,8 +8,12 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Skull; @@ -20,10 +24,13 @@ import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapelessRecipe; import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask; +import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import io.papermc.lib.PaperLib; +import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; @@ -89,13 +96,57 @@ public class VanillaAutoCrafter extends AbstractAutoCrafter { if (recipes.isEmpty()) { SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes"); } else { - // TODO Choose vanilla recipe + ChestMenu menu = new ChestMenu(getItemName()); + menu.setPlayerInventoryClickable(false); + menu.setEmptySlotsClickable(false); + + ChestMenuUtils.drawBackground(menu, background); + ChestMenuUtils.drawBackground(menu, 45, 47, 48, 50, 51, 53); + // 46 // 52 + } } - + @ParametersAreNonnullByDefault - private void offerRecipe(Player p, Block b, List recipes, int page, BlockMenu menu, AsyncRecipeChoiceTask task) { - + private void offerRecipe(Player p, Block b, List recipes, int index, BlockMenu menu, AsyncRecipeChoiceTask task) { + Validate.isTrue(index >= 0 && index < recipes.size(), "page must be between 0 and " + (recipes.size() - 1)); + + menu.addItem(46, ChestMenuUtils.getPreviousButton(p, index + 1, recipes.size())); + menu.addMenuClickHandler(46, (pl, slot, item, action) -> { + if (index > 0) { + pl.playSound(pl.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1); + offerRecipe(p, b, recipes, index - 1, menu, task); + } + + return false; + }); + + menu.addItem(52, ChestMenuUtils.getNextButton(p, index + 1, recipes.size())); + menu.addMenuClickHandler(52, (pl, slot, item, action) -> { + if (index < (recipes.size() - 1)) { + pl.playSound(pl.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1); + offerRecipe(p, b, recipes, index + 1, menu, task); + } + + return false; + }); + + AbstractRecipe recipe = AbstractRecipe.of(recipes.get(index)); + + menu.addItem(49, new CustomItem(Material.CRAFTING_TABLE, ChatColor.GREEN + SlimefunPlugin.getLocalization().getMessage(p, "messages.auto-crafting.select"))); + menu.addMenuClickHandler(49, (pl, slot, item, action) -> { + setSelectedRecipe(b, recipe); + pl.closeInventory(); + + p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1); + SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.recipe-set"); + showRecipe(p, b, recipe); + + return false; + }); + + task.clear(); + recipe.show(menu, task); } @Nonnull diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java index e02d055bd..78a17f6f1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java @@ -102,6 +102,8 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.accelerators.AnimalGrowthAccelerator; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.accelerators.CropGrowthAccelerator; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.accelerators.TreeGrowthAccelerator; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters.EnhancedAutoCrafter; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters.VanillaAutoCrafter; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoDisenchanter; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoEnchanter; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.BookBinder; @@ -2578,7 +2580,19 @@ public final class SlimefunItemSetup { } new ElytraCap(categories.magicalArmor, SlimefunItems.ELYTRA_CAP, RecipeType.ARMOR_FORGE, - new ItemStack[]{new ItemStack(Material.SLIME_BALL), new ItemStack(Material.SLIME_BALL), new ItemStack(Material.SLIME_BALL), SlimefunItems.ELYTRA_SCALE, SlimefunItems.ELYTRA_SCALE, SlimefunItems.ELYTRA_SCALE, new ItemStack(Material.SLIME_BALL), new ItemStack(Material.LEATHER_HELMET), new ItemStack(Material.SLIME_BALL)}) + new ItemStack[] {new ItemStack(Material.SLIME_BALL), new ItemStack(Material.SLIME_BALL), new ItemStack(Material.SLIME_BALL), SlimefunItems.ELYTRA_SCALE, SlimefunItems.ELYTRA_SCALE, SlimefunItems.ELYTRA_SCALE, new ItemStack(Material.SLIME_BALL), new ItemStack(Material.LEATHER_HELMET), new ItemStack(Material.SLIME_BALL)}) + .register(plugin); + + new VanillaAutoCrafter(categories.cargo, SlimefunItems.VANILLA_AUTO_CRAFTER, RecipeType.ENHANCED_CRAFTING_TABLE, + new ItemStack[] {null, SlimefunItems.BLISTERING_INGOT_3, null, new ItemStack(Material.CRAFTING_TABLE), SlimefunItems.CARGO_MOTOR, new ItemStack(Material.CRAFTING_TABLE), null, SlimefunItems.ELECTRIC_MOTOR, null}) + .setCapacity(256) + .setEnergyConsumption(16) + .register(plugin); + + new EnhancedAutoCrafter(categories.cargo, SlimefunItems.ENHANCED_AUTO_CRAFTER, RecipeType.ENHANCED_CRAFTING_TABLE, + new ItemStack[] {null, SlimefunItems.VANILLA_AUTO_CRAFTER, null, new ItemStack(Material.CRAFTING_TABLE), new ItemStack(Material.DISPENSER), new ItemStack(Material.CRAFTING_TABLE), null, SlimefunItems.CARGO_MOTOR, null}) + .setCapacity(256) + .setEnergyConsumption(16) .register(plugin); // @formatter:on diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/AsyncRecipeChoiceTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/AsyncRecipeChoiceTask.java index a0e51256f..5eea7b237 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/AsyncRecipeChoiceTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/AsyncRecipeChoiceTask.java @@ -92,6 +92,20 @@ public class AsyncRecipeChoiceTask implements Runnable { } } + /** + * This method clears the {@link AsyncRecipeChoiceTask} and removes all active + * iterators. + */ + public void clear() { + lock.writeLock().lock(); + + try { + iterators.clear(); + } finally { + lock.writeLock().unlock(); + } + } + @Override public void run() { // Terminate the task when noone is viewing the Inventory