From ac7bb44d9845ef293cd84cd1bed4e73339387fa7 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Tue, 1 Dec 2020 11:46:59 +0100 Subject: [PATCH] Fixes #2585 --- CHANGELOG.md | 1 + .../items/seasonal/ChristmasPresent.java | 25 ++- .../Objects/SlimefunItem/SlimefunItem.java | 15 ++ .../testing/tests/items/TestSlimefunItem.java | 179 ++++++++++++++++++ .../TestSlimefunItemRegistration.java | 125 ------------ 5 files changed, 207 insertions(+), 138 deletions(-) create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/TestSlimefunItem.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 50c767c83..56e018a84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,7 @@ * Fixed color in android script downloading screen * Fixed #2576 * Fixed #2496 +* Fixed #2585 ## Release Candidate 17 (17 Oct 2020) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java index 065c7277e..d0d7ac080 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java @@ -2,14 +2,15 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.seasonal; import java.util.concurrent.ThreadLocalRandom; +import javax.annotation.ParametersAreNonnullByDefault; + import org.bukkit.GameMode; import org.bukkit.block.Block; -import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils; import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; -import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; +import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler; import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem; import io.github.thebusybiscuit.slimefun4.utils.FireworkUtils; import me.mrCookieSlime.Slimefun.Lists.RecipeType; @@ -26,10 +27,11 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; * @see EasterEgg * */ -public class ChristmasPresent extends SimpleSlimefunItem implements NotPlaceable { +public class ChristmasPresent extends SimpleSlimefunItem implements NotPlaceable { private final ItemStack[] gifts; + @ParametersAreNonnullByDefault public ChristmasPresent(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack... gifts) { super(category, item, recipeType, recipe); @@ -37,24 +39,21 @@ public class ChristmasPresent extends SimpleSlimefunItem impl } @Override - public BlockPlaceHandler getItemHandler() { - return new BlockPlaceHandler(false) { - - @Override - public void onPlayerPlace(BlockPlaceEvent e) { - e.setCancelled(true); + public ItemUseHandler getItemHandler() { + return e -> { + e.cancel(); + e.getClickedBlock().ifPresent(block -> { if (e.getPlayer().getGameMode() != GameMode.CREATIVE) { - ItemUtils.consumeItem(e.getItemInHand(), false); + ItemUtils.consumeItem(e.getItem(), false); } FireworkUtils.launchRandom(e.getPlayer(), 3); - Block b = e.getBlock(); + Block b = block.getRelative(e.getClickedFace()); ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone(); b.getWorld().dropItemNaturally(b.getLocation(), gift); - } - + }); }; } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java index 362e90051..666f8dae4 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java @@ -979,6 +979,20 @@ public class SlimefunItem implements Placeable { } } + @Override + public final boolean equals(Object obj) { + if (obj instanceof SlimefunItem) { + return ((SlimefunItem) obj).getId().equals(getId()); + } else { + return false; + } + } + + @Override + public final int hashCode() { + return getId().hashCode(); + } + @Nullable public static SlimefunItem getByID(@Nonnull String id) { return SlimefunPlugin.getRegistry().getSlimefunItemIds().get(id); @@ -1028,4 +1042,5 @@ public class SlimefunItem implements Placeable { public static void registerBlockHandler(String id, SlimefunBlockHandler handler) { SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler); } + } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/TestSlimefunItem.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/TestSlimefunItem.java new file mode 100644 index 000000000..343303f1a --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/TestSlimefunItem.java @@ -0,0 +1,179 @@ +package io.github.thebusybiscuit.slimefun4.testing.tests.items; + +import java.util.Optional; + +import org.bukkit.Material; +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.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import be.seeseemelk.mockbukkit.MockBukkit; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.api.exceptions.UnregisteredItemException; +import io.github.thebusybiscuit.slimefun4.api.exceptions.WrongItemStackException; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.testing.TestUtilities; +import me.mrCookieSlime.Slimefun.Lists.RecipeType; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; + +class TestSlimefunItem { + + private static SlimefunPlugin plugin; + + @BeforeAll + public static void load() { + MockBukkit.mock(); + plugin = MockBukkit.load(SlimefunPlugin.class); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + @Test + @DisplayName("Test wiki pages getting assigned correctly") + void testWikiPages() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "WIKI_ITEM", new CustomItem(Material.BOOK, "&cTest")); + item.register(plugin); + + Assertions.assertFalse(item.getWikipage().isPresent()); + + // null should not be a valid argument + Assertions.assertThrows(IllegalArgumentException.class, () -> item.addOficialWikipage(null)); + + item.addOficialWikipage("Test"); + + Optional wiki = item.getWikipage(); + Assertions.assertTrue(wiki.isPresent()); + Assertions.assertEquals("https://github.com/Slimefun/Slimefun4/wiki/Test", wiki.get()); + } + + @Test + @DisplayName("Test SlimefunItem registering Recipes properly") + void testRecipe() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TEST", new CustomItem(Material.DIAMOND, "&dAnother one bites the test")); + + ItemStack[] recipe = { null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null }; + item.setRecipe(recipe); + item.register(plugin); + + Assertions.assertArrayEquals(recipe, item.getRecipe()); + + Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(null)); + Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[3])); + Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[20])); + } + + @Test + @DisplayName("Test Recipe outputs being handled correctly") + void testRecipeOutput() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_OUTPUT_TEST", new CustomItem(Material.DIAMOND, "&cTest")); + item.register(plugin); + + Assertions.assertEquals(item.getItem(), item.getRecipeOutput()); + + ItemStack output = new ItemStack(Material.EMERALD, 64); + item.setRecipeOutput(output); + Assertions.assertEquals(output, item.getRecipeOutput()); + + item.setRecipeOutput(item.getItem()); + Assertions.assertEquals(item.getItem(), item.getRecipeOutput()); + } + + @Test + @DisplayName("Test Recipe Types being handled properly") + void testRecipeType() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TYPE_TEST", new CustomItem(Material.DIAMOND, "&cTest")); + item.register(plugin); + + Assertions.assertNotNull(item.getRecipeType()); + + item.setRecipeType(RecipeType.ENHANCED_CRAFTING_TABLE); + Assertions.assertEquals(RecipeType.ENHANCED_CRAFTING_TABLE, item.getRecipeType()); + + item.setRecipeType(RecipeType.NULL); + Assertions.assertEquals(RecipeType.NULL, item.getRecipeType()); + + Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipeType(null)); + } + + @ParameterizedTest + @DisplayName("Test SlimefunItem#isItem(...)") + @ValueSource(booleans = { true, false }) + void testIsItem(boolean compatibility) { + CustomItem item = new CustomItem(Material.BEACON, "&cItem Test"); + String id = "IS_ITEM_TEST" + (compatibility ? "_COMPATIBLE" : ""); + SlimefunItem sfItem = TestUtilities.mockSlimefunItem(plugin, id, item); + sfItem.register(plugin); + + Assertions.assertTrue(sfItem.isItem(sfItem.getItem())); + + Assertions.assertFalse(sfItem.isItem(null)); + Assertions.assertFalse(sfItem.isItem(new ItemStack(Material.BEACON))); + Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.REDSTONE, "&cTest"))); + + if (compatibility) { + SlimefunPlugin.getRegistry().setBackwardsCompatible(true); + + Assertions.assertEquals(sfItem, SlimefunItem.getByItem(item)); + Assertions.assertTrue(sfItem.isItem(item)); + Assertions.assertTrue(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test"))); + + SlimefunPlugin.getRegistry().setBackwardsCompatible(false); + } else { + Assertions.assertFalse(sfItem.isItem(item)); + Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test"))); + } + + Assertions.assertEquals(sfItem, SlimefunItem.getByItem(new SlimefunItemStack(sfItem.getId(), item))); + } + + @Test + @DisplayName("Test WrongItemStackException") + void testWrongItemStackException() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "WRONG_ITEMSTACK_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me")); + item.register(plugin); + item.load(); + + ItemStack itemStack = item.getItem(); + Assertions.assertThrows(WrongItemStackException.class, () -> itemStack.setAmount(40)); + } + + @Test + @DisplayName("Test UnregisteredItemException") + void testUnregisteredItemException() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "UNREGISTERED_ITEM_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me")); + Assertions.assertThrows(UnregisteredItemException.class, () -> item.getAddon()); + } + + @Test + @DisplayName("Test SlimefunItem#equals(...)") + void testEquals() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal")); + SlimefunItem item2 = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal")); + SlimefunItem differentItem = TestUtilities.mockSlimefunItem(plugin, "I_AM_DIFFERENT", new CustomItem(Material.LANTERN, "&6We are equal")); + + Assertions.assertEquals(item, item2); + Assertions.assertNotEquals(item, differentItem); + Assertions.assertNotEquals(item2, differentItem); + } + + @Test + @DisplayName("Test SlimefunItem#hashCode()") + void testHashCode() { + SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal")); + SlimefunItem item2 = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal")); + SlimefunItem differentItem = TestUtilities.mockSlimefunItem(plugin, "I_AM_DIFFERENT", new CustomItem(Material.LANTERN, "&6We are equal")); + + Assertions.assertEquals(item.hashCode(), item2.hashCode()); + Assertions.assertNotEquals(item.hashCode(), differentItem.hashCode()); + Assertions.assertNotEquals(item2.hashCode(), differentItem.hashCode()); + } +} diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestSlimefunItemRegistration.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestSlimefunItemRegistration.java index e6125957e..aa47fa5b4 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestSlimefunItemRegistration.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestSlimefunItemRegistration.java @@ -1,31 +1,22 @@ package io.github.thebusybiscuit.slimefun4.testing.tests.registration; -import java.util.Optional; - import org.bukkit.Material; import org.bukkit.NamespacedKey; -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.DisplayName; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; import be.seeseemelk.mockbukkit.MockBukkit; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.api.exceptions.IdConflictException; -import io.github.thebusybiscuit.slimefun4.api.exceptions.UnregisteredItemException; -import io.github.thebusybiscuit.slimefun4.api.exceptions.WrongItemStackException; import io.github.thebusybiscuit.slimefun4.api.items.ItemState; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; import io.github.thebusybiscuit.slimefun4.testing.TestUtilities; -import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; class TestSlimefunItemRegistration { @@ -69,24 +60,6 @@ class TestSlimefunItemRegistration { Assertions.assertTrue(item.isDisabled()); } - @Test - @DisplayName("Test wiki pages getting assigned correctly") - void testWikiPages() { - SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "WIKI_ITEM", new CustomItem(Material.BOOK, "&cTest")); - item.register(plugin); - - Assertions.assertFalse(item.getWikipage().isPresent()); - - // null should not be a valid argument - Assertions.assertThrows(IllegalArgumentException.class, () -> item.addOficialWikipage(null)); - - item.addOficialWikipage("Test"); - - Optional wiki = item.getWikipage(); - Assertions.assertTrue(wiki.isPresent()); - Assertions.assertEquals("https://github.com/Slimefun/Slimefun4/wiki/Test", wiki.get()); - } - @Test @DisplayName("Test VanillaItem falling back to vanilla.") void testVanillaItemFallback() { @@ -111,86 +84,6 @@ class TestSlimefunItemRegistration { Assertions.assertEquals(ItemState.UNREGISTERED, item2.getState()); } - @Test - @DisplayName("Test SlimefunItem registering Recipes properly") - void testRecipe() { - SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TEST", new CustomItem(Material.DIAMOND, "&dAnother one bites the test")); - - ItemStack[] recipe = { null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null }; - item.setRecipe(recipe); - item.register(plugin); - - Assertions.assertArrayEquals(recipe, item.getRecipe()); - - Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(null)); - Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[3])); - Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[20])); - } - - @Test - @DisplayName("Test Recipe outputs being handled correctly") - void testRecipeOutput() { - SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_OUTPUT_TEST", new CustomItem(Material.DIAMOND, "&cTest")); - item.register(plugin); - - Assertions.assertEquals(item.getItem(), item.getRecipeOutput()); - - ItemStack output = new ItemStack(Material.EMERALD, 64); - item.setRecipeOutput(output); - Assertions.assertEquals(output, item.getRecipeOutput()); - - item.setRecipeOutput(item.getItem()); - Assertions.assertEquals(item.getItem(), item.getRecipeOutput()); - } - - @Test - @DisplayName("Test Recipe Types being handled properly") - void testRecipeType() { - SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TYPE_TEST", new CustomItem(Material.DIAMOND, "&cTest")); - item.register(plugin); - - Assertions.assertNotNull(item.getRecipeType()); - - item.setRecipeType(RecipeType.ENHANCED_CRAFTING_TABLE); - Assertions.assertEquals(RecipeType.ENHANCED_CRAFTING_TABLE, item.getRecipeType()); - - item.setRecipeType(RecipeType.NULL); - Assertions.assertEquals(RecipeType.NULL, item.getRecipeType()); - - Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipeType(null)); - } - - @ParameterizedTest - @DisplayName("Test SlimefunItem#isItem(...)") - @ValueSource(booleans = { true, false }) - void testIsItem(boolean compatibility) { - CustomItem item = new CustomItem(Material.BEACON, "&cItem Test"); - String id = "IS_ITEM_TEST" + (compatibility ? "_COMPATIBLE" : ""); - SlimefunItem sfItem = TestUtilities.mockSlimefunItem(plugin, id, item); - sfItem.register(plugin); - - Assertions.assertTrue(sfItem.isItem(sfItem.getItem())); - - Assertions.assertFalse(sfItem.isItem(null)); - Assertions.assertFalse(sfItem.isItem(new ItemStack(Material.BEACON))); - Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.REDSTONE, "&cTest"))); - - if (compatibility) { - SlimefunPlugin.getRegistry().setBackwardsCompatible(true); - - Assertions.assertEquals(sfItem, SlimefunItem.getByItem(item)); - Assertions.assertTrue(sfItem.isItem(item)); - Assertions.assertTrue(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test"))); - - SlimefunPlugin.getRegistry().setBackwardsCompatible(false); - } else { - Assertions.assertFalse(sfItem.isItem(item)); - Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test"))); - } - - Assertions.assertEquals(sfItem, SlimefunItem.getByItem(new SlimefunItemStack(sfItem.getId(), item))); - } - @Test @DisplayName("Test Category registration when registering an item") void testCategoryRegistration() { @@ -243,22 +136,4 @@ class TestSlimefunItemRegistration { Assertions.assertTrue(item.isHidden()); Assertions.assertFalse(category.contains(item)); } - - @Test - @DisplayName("Test WrongItemStackException") - void testWrongItemStackException() { - SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "WRONG_ITEMSTACK_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me")); - item.register(plugin); - item.load(); - - ItemStack itemStack = item.getItem(); - Assertions.assertThrows(WrongItemStackException.class, () -> itemStack.setAmount(40)); - } - - @Test - @DisplayName("Test UnregisteredItemException") - void testUnregisteredItemException() { - SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "UNREGISTERED_ITEM_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me")); - Assertions.assertThrows(UnregisteredItemException.class, () -> item.getAddon()); - } }