From 206e205d1b8aca1dd93c4747420e676a7ae3286d Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Mon, 13 Sep 2021 13:26:35 +0100 Subject: [PATCH] Deny using Slimefun Netherite in the SmithingTable + Tests --- .../slimefun4/implementation/Slimefun.java | 2 + .../crafting/SmithingTableListener.java | 42 ++++++ .../listeners/crafting/package-info.java | 2 +- src/main/resources/languages/en/messages.yml | 3 + .../listeners/TestSmithingTableListener.java | 135 ++++++++++++++++++ 5 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/SmithingTableListener.java create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSmithingTableListener.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java index 0522e3920..6b6bf9a01 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java @@ -101,6 +101,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.Cart import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.CauldronListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.CraftingTableListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.GrindstoneListener; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.SmithingTableListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.entity.BeeListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.entity.EntityInteractionListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.entity.FireworksListener; @@ -610,6 +611,7 @@ public final class Slimefun extends JavaPlugin implements SlimefunAddon { new CauldronListener(this); new GrindstoneListener(this); new CartographyTableListener(this); + new SmithingTableListener(this); new ButcherAndroidListener(this); new MiningAndroidListener(this); new NetworkListener(this, networkManager); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/SmithingTableListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/SmithingTableListener.java new file mode 100644 index 000000000..7803dd4c0 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/SmithingTableListener.java @@ -0,0 +1,42 @@ +package io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting; + +import javax.annotation.Nonnull; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event.Result; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; + +/** + * This {@link Listener} prevents any {@link SlimefunItem} from being used in a + * cartography table. + * + * @author Sefiraat + * + */ +public class SmithingTableListener implements SlimefunCraftingListener { + + public SmithingTableListener(@Nonnull Slimefun plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler(ignoreCancelled = true) + public void onSmith(InventoryClickEvent e) { + if (e.getRawSlot() == 2 && e.getWhoClicked() instanceof Player && e.getInventory().getType() == InventoryType.SMITHING) { + ItemStack itemStack = e.getInventory().getContents()[1]; + + if (isUnallowed(itemStack)) { + e.setResult(Result.DENY); + Slimefun.getLocalization().sendMessage(e.getWhoClicked(), "smithing_table.not-working", true); + } + } + + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/package-info.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/package-info.java index 8c8f08ecc..0d777f377 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/package-info.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/crafting/package-info.java @@ -1,5 +1,5 @@ /** * This package holds every {@link org.bukkit.event.Listener} which is responsible for preventing that a - * {@link me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem} is used in an unallowed crafting operation + * {@link io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem} is used in an unallowed crafting operation */ package io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting; \ No newline at end of file diff --git a/src/main/resources/languages/en/messages.yml b/src/main/resources/languages/en/messages.yml index e721c3f1b..14b52086d 100644 --- a/src/main/resources/languages/en/messages.yml +++ b/src/main/resources/languages/en/messages.yml @@ -329,6 +329,9 @@ brewing_stand: cartography_table: not-working: '&4You cannot use Slimefun items in a cartography table!' +smithing_table: + not-working: '&4You cannot use Slimefun items as a smithing material!' + villagers: no-trading: '&4You cannot trade Slimefun items with Villagers!' diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSmithingTableListener.java b/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSmithingTableListener.java new file mode 100644 index 000000000..1de1ddfac --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSmithingTableListener.java @@ -0,0 +1,135 @@ +package io.github.thebusybiscuit.slimefun4.implementation.listeners; + +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.Event.Result; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.inventory.InventoryType.SlotType; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.DisplayName; + +import io.github.bakedlibs.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.SmithingTableListener; +import io.github.thebusybiscuit.slimefun4.test.TestUtilities; + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; + +class TestSmithingTableListener { + + private static SmithingTableListener listener; + private static ServerMock server; + + private static SlimefunItem slimefunIngot; + private static SlimefunItem slimefunTool; + private static VanillaItem vanillaIngot; + private static VanillaItem vanillaTool; + + @BeforeAll + public static void load() { + server = MockBukkit.mock(); + Slimefun plugin = MockBukkit.load(Slimefun.class); + listener = new SmithingTableListener(plugin); + + slimefunTool = TestUtilities.mockSlimefunItem(plugin, "MOCK_DIAMOND_SWORD", new CustomItemStack(Material.DIAMOND_SWORD, "&6Mock")); + slimefunIngot = TestUtilities.mockSlimefunItem(plugin, "MOCK_NETHERITE_INGOT", new CustomItemStack(Material.NETHERITE_INGOT, "&6Mock")); + vanillaTool = TestUtilities.mockVanillaItem(plugin, Material.DIAMOND_SWORD, true); + vanillaIngot = TestUtilities.mockVanillaItem(plugin, Material.NETHERITE_INGOT, true); + + slimefunTool.register(plugin); + slimefunIngot.register(plugin); + vanillaTool.register(plugin); + vanillaIngot.register(plugin); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + private InventoryClickEvent mockSmithingEvent(ItemStack tool, ItemStack material) { + Player player = server.addPlayer(); + Inventory inv = TestUtilities.mockInventory(InventoryType.SMITHING, tool, material, null); + InventoryView view = player.openInventory(inv); + InventoryClickEvent event = new InventoryClickEvent(view, SlotType.CONTAINER, 2, ClickType.LEFT, InventoryAction.PICKUP_ONE); + + listener.onSmith(event); + return event; + } + + @Test + @DisplayName("Test that vanilla is unchanged (ItemStack tool x ItemStack material)") + void testSmithingTableWithItemStacks() { + InventoryClickEvent event = mockSmithingEvent(new ItemStack(Material.DIAMOND_SWORD), new ItemStack(Material.NETHERITE_INGOT)); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + @DisplayName("Test that SlimefunItem material doesn't work (ItemStack tool x SlimefunItem material)") + void testSmithingTableWithItemStackAndSlimefunItem() { + InventoryClickEvent event = mockSmithingEvent(new ItemStack(Material.DIAMOND_SWORD), slimefunIngot.getItem()); + Assertions.assertEquals(Result.DENY, event.getResult()); + } + + @Test + @DisplayName("Test that VanillaItem material works (ItemStack tool x VanillaItem material)") + void testSmithingTableWithItemStackAndVanillaItem() { + InventoryClickEvent event = mockSmithingEvent(new ItemStack(Material.DIAMOND_SWORD), vanillaIngot.getItem()); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + @DisplayName("Test that SlimefunItems can upgrade with vanilla (SlimefunItem tool x ItemStack material)") + void testSmithingTableWithSlimefunItemAndItemStack() { + InventoryClickEvent event = mockSmithingEvent(slimefunTool.getItem(), new ItemStack(Material.NETHERITE_INGOT)); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + @DisplayName("Test that SlimefunItems can't upgrade with SlimefunItem materials (SlimefunItem tool x SlimefunItem material)") + void testSmithingTableWithSlimefunItems() { + InventoryClickEvent event = mockSmithingEvent(slimefunTool.getItem(), slimefunIngot.getItem()); + Assertions.assertEquals(Result.DENY, event.getResult()); + } + + @Test + @DisplayName("Test that SlimefunItems can upgrade with VanillaItems (SlimefunItem tool x VanillaItem material)") + void testSmithingTableWithSlimefunItemAndVanillaItem() { + InventoryClickEvent event = mockSmithingEvent(slimefunTool.getItem(), vanillaIngot.getItem()); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + @DisplayName("Test that SlimefunItems can upgrade with vanilla (SlimefunItem tool x ItemStack material)") + void testSmithingTableWithVanillaItemAndItemStack() { + InventoryClickEvent event = mockSmithingEvent(vanillaTool.getItem(), new ItemStack(Material.NETHERITE_INGOT)); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + @DisplayName("Test that SlimefunItems can't upgrade with SlimefunItem materials (SlimefunItem tool x SlimefunItem material)") + void testSmithingTableWithVanillaItemAndSlimefunItem() { + InventoryClickEvent event = mockSmithingEvent(vanillaTool.getItem(), slimefunIngot.getItem()); + Assertions.assertEquals(Result.DENY, event.getResult()); + } + + @Test + @DisplayName("Test that SlimefunItems can upgrade with VanillaItems (SlimefunItem tool x VanillaItem material)") + void testSmithingTableWithVanillaItemAndVanillaItem() { + InventoryClickEvent event = mockSmithingEvent(vanillaTool.getItem(), vanillaIngot.getItem()); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + +}