From 7e04566393d7de91252fe46fe5da3b2c02052927 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Thu, 7 May 2020 14:28:54 +0200 Subject: [PATCH] Further unit tests and jacoco coverage reports --- .gitignore | 12 +- pom.xml | 31 +++- .../items/magical/SoulboundRune.java | 53 ++++--- .../listeners/VanillaMachinesListener.java | 6 +- .../Slimefun/Lists/SlimefunItems.java | 41 +++--- .../Slimefun/Objects/Category.java | 12 ++ .../Objects/SlimefunItem/SlimefunItem.java | 5 +- .../Slimefun/api/SlimefunItemStack.java | 5 + .../slimefun4/mocks/MockItemHandler.java | 12 ++ .../slimefun4/mocks/SlimefunMocks.java | 4 + .../tests/items/TestItemHandlers.java | 65 +++++++++ .../items/TestSlimefunItemRegistration.java | 135 +++++++++++++++++- .../TestVanillaMachinesListener.java | 71 ++++++++- 13 files changed, 398 insertions(+), 54 deletions(-) create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/mocks/MockItemHandler.java create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java diff --git a/.gitignore b/.gitignore index 38fc3e2e3..7139df706 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,12 @@ /bin/ +/target/ +/sonar/ +/.settings/ +/.idea/ + +dependency-reduced-pom.xml + .classpath .project -/.settings/ *.iml -/target -/.idea/ -dependency-reduced-pom.xml -javadoc.xml .DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml index ddeacc6bb..2689ec6b8 100644 --- a/pom.xml +++ b/pom.xml @@ -7,8 +7,7 @@ me.mrCookieSlime Slimefun - + 4.3-UNOFFICIAL @@ -25,6 +24,7 @@ TheBusyBiscuit_Slimefun4 thebusybiscuit-github https://sonarcloud.io + target/site/jacoco/jacoco.xml @@ -59,6 +59,7 @@ ${project.name} v${project.version} + org.apache.maven.plugins maven-compiler-plugin @@ -72,6 +73,7 @@ + org.apache.maven.plugins maven-source-plugin @@ -86,12 +88,37 @@ + org.apache.maven.plugins maven-surefire-plugin 3.0.0-M4 + + + org.jacoco + jacoco-maven-plugin + 0.8.5 + + + + + prepare-agent + + + + + report + test + + report + + + + + + org.apache.maven.plugins maven-shade-plugin diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/SoulboundRune.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/SoulboundRune.java index eb7f6c9da..1b543d968 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/SoulboundRune.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/SoulboundRune.java @@ -42,8 +42,9 @@ public class SoulboundRune extends SimpleSlimefunItem { @Override public ItemDropHandler getItemHandler() { - return (e, p, i) -> { - ItemStack item = i.getItemStack(); + return (e, p, droppedItem) -> { + ItemStack item = droppedItem.getItemStack(); + if (isItem(item)) { if (!Slimefun.hasUnlocked(p, SlimefunItems.RUNE_SOULBOUND, true)) { @@ -52,42 +53,39 @@ public class SoulboundRune extends SimpleSlimefunItem { Slimefun.runSync(() -> { // Being sure the entity is still valid and not picked up or whatsoever. - if (!i.isValid()) return; + if (!droppedItem.isValid()) { + return; + } - Location l = i.getLocation(); + Location l = droppedItem.getLocation(); Collection entites = l.getWorld().getNearbyEntities(l, 1.5, 1.5, 1.5, this::findCompatibleItem); - if (entites.isEmpty()) return; + if (entites.isEmpty()) { + return; + } Entity entity = entites.stream().findFirst().get(); - ItemStack ench = ((Item) entity).getItemStack(); - Item ent = (Item) entity; + ItemStack target = ((Item) entity).getItemStack(); + Item targetItem = (Item) entity; - if (ench.getAmount() == 1) { + if (target.getAmount() == 1) { e.setCancelled(true); - ItemMeta enchMeta = ench.getItemMeta(); - List lore = enchMeta.hasLore() ? enchMeta.getLore() : new ArrayList<>(); - // This lightning is just an effect, it deals no damage. l.getWorld().strikeLightningEffect(l); Slimefun.runSync(() -> { - // Being sure entities are still valid and not picked up or whatsoever. - if (i.isValid() && ent.isValid()) { + if (droppedItem.isValid() && targetItem.isValid() && target.getAmount() == 1) { l.getWorld().createExplosion(l, 0.0F); l.getWorld().playSound(l, Sound.ENTITY_GENERIC_EXPLODE, 0.3F, 1F); - lore.add(ChatColor.GRAY + "Soulbound"); + apply(target); - enchMeta.setLore(lore); - ench.setItemMeta(enchMeta); - - ent.remove(); - i.remove(); - l.getWorld().dropItemNaturally(l, ench); + targetItem.remove(); + droppedItem.remove(); + l.getWorld().dropItemNaturally(l, target); SlimefunPlugin.getLocal().sendMessage(p, "messages.soulbound-rune.success", true); } @@ -104,6 +102,21 @@ public class SoulboundRune extends SimpleSlimefunItem { }; } + /** + * This method applies the {@link Soulbound} effect onto a given {@link ItemStack}. + * + * @param item + * The {@link ItemStack} to apply this effect to + */ + public void apply(ItemStack item) { + ItemMeta meta = item.getItemMeta(); + List lore = meta.hasLore() ? meta.getLore() : new ArrayList<>(); + lore.add(ChatColor.GRAY + "Soulbound"); + + meta.setLore(lore); + item.setItemMeta(meta); + } + private boolean findCompatibleItem(Entity n) { if (n instanceof Item) { Item item = (Item) n; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/VanillaMachinesListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/VanillaMachinesListener.java index 50870ff66..17fe7ee4d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/VanillaMachinesListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/VanillaMachinesListener.java @@ -80,14 +80,14 @@ public class VanillaMachinesListener implements Listener { } } - @EventHandler + @EventHandler(ignoreCancelled = true) public void onAnvil(InventoryClickEvent e) { if (e.getRawSlot() == 2 && e.getInventory().getType() == InventoryType.ANVIL && e.getWhoClicked() instanceof Player) { ItemStack item1 = e.getInventory().getContents()[0]; ItemStack item2 = e.getInventory().getContents()[1]; if (checkForUnallowedItems(item1, item2)) { - e.setCancelled(true); + e.setResult(Result.DENY); SlimefunPlugin.getLocal().sendMessage((Player) e.getWhoClicked(), "anvil.not-working", true); } } @@ -98,7 +98,7 @@ public class VanillaMachinesListener implements Listener { Inventory inventory = e.getInventory(); if (inventory.getType() == InventoryType.BREWING && e.getRawSlot() < inventory.getSize() && inventory.getHolder() instanceof BrewingStand) { - e.setCancelled(SlimefunItem.getByItem(e.getCursor()) != null); + e.setCancelled(isUnallowed(SlimefunItem.getByItem(e.getCursor()))); } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Lists/SlimefunItems.java b/src/main/java/me/mrCookieSlime/Slimefun/Lists/SlimefunItems.java index ff8802e51..d5fe18445 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Lists/SlimefunItems.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Lists/SlimefunItems.java @@ -12,7 +12,6 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; -import io.github.thebusybiscuit.cscorelib2.skull.SkullItem; import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.core.attributes.MachineTier; import io.github.thebusybiscuit.slimefun4.core.attributes.MachineType; @@ -710,27 +709,27 @@ public final class SlimefunItems { public static final SlimefunItemStack AUTO_BREEDER = new SlimefunItemStack("AUTO_BREEDER", Material.HAY_BLOCK, "&eAuto-Breeder", "", "&rRuns on &aOrganic Food", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), LoreBuilder.powerBuffer(1024), "&8\u21E8 &e\u26A1 &760 J/Animal"); - public static final ItemStack ORGANIC_FOOD = new CustomItem(SkullItem.fromBase64("b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79"), "&aOrganic Food", "&7Content: &9X"); - public static final SlimefunItemStack WHEAT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_WHEAT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Wheat"); - public static final SlimefunItemStack CARROT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_CARROT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Carrots"); - public static final SlimefunItemStack POTATO_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_POTATO", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Potatoes"); - public static final SlimefunItemStack SEEDS_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SEEDS", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Seeds"); - public static final SlimefunItemStack BEETROOT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_BEETROOT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Beetroot"); - public static final SlimefunItemStack MELON_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_MELON", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Melon"); - public static final SlimefunItemStack APPLE_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_APPLE", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Apple"); - public static final SlimefunItemStack SWEET_BERRIES_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SWEET_BERRIES", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Sweet Berries"); - public static final SlimefunItemStack KELP_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_KELP", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Dried Kelp"); + public static final SlimefunItemStack ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9???"); + public static final SlimefunItemStack WHEAT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_WHEAT", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Wheat"); + public static final SlimefunItemStack CARROT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_CARROT", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Carrots"); + public static final SlimefunItemStack POTATO_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_POTATO", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Potatoes"); + public static final SlimefunItemStack SEEDS_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SEEDS", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Seeds"); + public static final SlimefunItemStack BEETROOT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_BEETROOT", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Beetroot"); + public static final SlimefunItemStack MELON_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_MELON", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Melon"); + public static final SlimefunItemStack APPLE_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_APPLE", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Apple"); + public static final SlimefunItemStack SWEET_BERRIES_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SWEET_BERRIES", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Sweet Berries"); + public static final SlimefunItemStack KELP_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_KELP", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Dried Kelp"); - public static final ItemStack FERTILIZER = new CustomItem(SkullItem.fromBase64("b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79"), "&aOrganic Fertilizer", "&7Content: &9X"); - public static final SlimefunItemStack WHEAT_FERTILIZER = new SlimefunItemStack("FERTILIZER_WHEAT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Wheat"); - public static final SlimefunItemStack CARROT_FERTILIZER = new SlimefunItemStack("FERTILIZER_CARROT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Carrots"); - public static final SlimefunItemStack POTATO_FERTILIZER = new SlimefunItemStack("FERTILIZER_POTATO", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Potatoes"); - public static final SlimefunItemStack SEEDS_FERTILIZER = new SlimefunItemStack("FERTILIZER_SEEDS", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Seeds"); - public static final SlimefunItemStack BEETROOT_FERTILIZER = new SlimefunItemStack("FERTILIZER_BEETROOT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Beetroot"); - public static final SlimefunItemStack MELON_FERTILIZER = new SlimefunItemStack("FERTILIZER_MELON", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Melon"); - public static final SlimefunItemStack APPLE_FERTILIZER = new SlimefunItemStack("FERTILIZER_APPLE", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Apple"); - public static final SlimefunItemStack SWEET_BERRIES_FERTILIZER = new SlimefunItemStack("FERTILIZER_SWEET_BERRIES", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Sweet Berries"); - public static final SlimefunItemStack KELP_FERTILIZER = new SlimefunItemStack("FERTILIZER_KELP", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Dried Kelp"); + public static final SlimefunItemStack FERTILIZER = new SlimefunItemStack("FERTILIZER", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9???"); + public static final SlimefunItemStack WHEAT_FERTILIZER = new SlimefunItemStack("FERTILIZER_WHEAT", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Wheat"); + public static final SlimefunItemStack CARROT_FERTILIZER = new SlimefunItemStack("FERTILIZER_CARROT", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Carrots"); + public static final SlimefunItemStack POTATO_FERTILIZER = new SlimefunItemStack("FERTILIZER_POTATO", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Potatoes"); + public static final SlimefunItemStack SEEDS_FERTILIZER = new SlimefunItemStack("FERTILIZER_SEEDS", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Seeds"); + public static final SlimefunItemStack BEETROOT_FERTILIZER = new SlimefunItemStack("FERTILIZER_BEETROOT", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Beetroot"); + public static final SlimefunItemStack MELON_FERTILIZER = new SlimefunItemStack("FERTILIZER_MELON", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Melon"); + public static final SlimefunItemStack APPLE_FERTILIZER = new SlimefunItemStack("FERTILIZER_APPLE", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Apple"); + public static final SlimefunItemStack SWEET_BERRIES_FERTILIZER = new SlimefunItemStack("FERTILIZER_SWEET_BERRIES", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Sweet Berries"); + public static final SlimefunItemStack KELP_FERTILIZER = new SlimefunItemStack("FERTILIZER_KELP", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Dried Kelp"); public static final SlimefunItemStack ANIMAL_GROWTH_ACCELERATOR = new SlimefunItemStack("ANIMAL_GROWTH_ACCELERATOR", Material.HAY_BLOCK, "&bAnimal Growth Accelerator", "", "&rRuns on &aOrganic Food", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), LoreBuilder.powerBuffer(1024), LoreBuilder.powerPerSecond(28)); public static final SlimefunItemStack CROP_GROWTH_ACCELERATOR = new SlimefunItemStack("CROP_GROWTH_ACCELERATOR", Material.LIME_TERRACOTTA, "&aCrop Growth Accelerator", "", "&rRuns on &aOrganic Fertilizer", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), "&8\u21E8 &7Radius: 7x7", "&8\u21E8 &7Speed: &a3/time", LoreBuilder.powerBuffer(1024), LoreBuilder.powerPerSecond(50)); diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java index 6ca61b4aa..6c98d3c60 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java @@ -152,6 +152,18 @@ public class Category implements Keyed { return items; } + /** + * This method returns whether a given {@link SlimefunItem} exists in this {@link Category}. + * + * @param item + * The {@link SlimefunItem} to find + * + * @return Whether the given {@link SlimefunItem} was found in this {@link Category} + */ + public boolean contains(SlimefunItem item) { + return item != null && items.contains(item); + } + /** * Returns the tier of this {@link Category}. * The tier determines the position of this {@link Category} in the {@link SlimefunGuide}. 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 2409dc0e3..2a06b5641 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java @@ -128,7 +128,7 @@ public class SlimefunItem implements Placeable { * * @return the identifier of this {@link SlimefunItem} */ - public String getID() { + public final String getID() { return id; } @@ -576,6 +576,9 @@ public class SlimefunItem implements Placeable { * Any {@link ItemHandler} that should be added to this {@link SlimefunItem} */ public final void addItemHandler(ItemHandler... handlers) { + Validate.notEmpty(handlers, "You cannot add zero handlers..."); + Validate.noNullElements(handlers, "You cannot add any 'null' ItemHandler!"); + if (state != ItemState.UNREGISTERED) { throw new UnsupportedOperationException("You cannot add an ItemHandler after the SlimefunItem was registered."); } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java b/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java index d6ab12734..158497509 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java @@ -22,6 +22,7 @@ import org.bukkit.potion.PotionEffectType; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.ImmutableItemMeta; import io.github.thebusybiscuit.cscorelib2.skull.SkullItem; +import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.api.exceptions.PrematureCodeException; import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import me.mrCookieSlime.Slimefun.SlimefunPlugin; @@ -184,6 +185,10 @@ public class SlimefunItemStack extends CustomItem { } private static ItemStack getSkull(String id, String texture) { + if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) { + return new ItemStack(Material.PLAYER_HEAD); + } + return SkullItem.fromBase64(getTexture(id, texture)); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/MockItemHandler.java b/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/MockItemHandler.java new file mode 100644 index 000000000..e5d102b02 --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/MockItemHandler.java @@ -0,0 +1,12 @@ +package io.github.thebusybiscuit.slimefun4.mocks; + +import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler; + +public class MockItemHandler implements ItemHandler { + + @Override + public Class getIdentifier() { + return getClass(); + } + +} diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java b/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java index 83eebc3b0..03d43a245 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java @@ -20,6 +20,10 @@ public final class SlimefunMocks { private SlimefunMocks() {} + public static Category getCategory() { + return category; + } + public static Inventory mockInventory(InventoryType type, ItemStack... contents) { Inventory inv = Mockito.mock(Inventory.class); diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java new file mode 100644 index 000000000..fde847873 --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java @@ -0,0 +1,65 @@ +package io.github.thebusybiscuit.slimefun4.tests.items; + +import java.util.concurrent.atomic.AtomicBoolean; + +import org.bukkit.Material; +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 be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.mocks.MockItemHandler; +import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.Objects.handlers.ItemUseHandler; + +public class TestItemHandlers { + + private static ServerMock server; + private static SlimefunPlugin plugin; + + @BeforeAll + public static void load() { + server = MockBukkit.mock(); + plugin = MockBukkit.load(SlimefunPlugin.class); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + @Test + public void testIllegalItemHandlers() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("ITEM_HANDLER_TEST", new CustomItem(Material.DIAMOND, "&cTest")); + item.register(plugin); + + Assertions.assertThrows(IllegalArgumentException.class, () -> item.addItemHandler()); + Assertions.assertThrows(IllegalArgumentException.class, () -> item.addItemHandler((MockItemHandler) null)); + Assertions.assertThrows(UnsupportedOperationException.class, () -> item.addItemHandler(new MockItemHandler())); + } + + @Test + public void testItemHandler() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("ITEM_HANDLER_TEST_2", new CustomItem(Material.DIAMOND, "&cTest")); + + MockItemHandler handler = new MockItemHandler(); + item.addItemHandler(handler); + + item.register(plugin); + + Assertions.assertTrue(item.getHandlers().contains(handler)); + + AtomicBoolean bool = new AtomicBoolean(false); + Assertions.assertTrue(item.callItemHandler(MockItemHandler.class, x -> bool.set(true))); + Assertions.assertTrue(bool.get()); + + AtomicBoolean bool2 = new AtomicBoolean(false); + Assertions.assertFalse(item.callItemHandler(ItemUseHandler.class, x -> bool2.set(true))); + Assertions.assertFalse(bool2.get()); + } +} diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java index 61c379390..bf0117e55 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java @@ -1,6 +1,11 @@ package io.github.thebusybiscuit.slimefun4.tests.items; +import java.util.Optional; + +import org.bukkit.ChatColor; 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; @@ -12,6 +17,7 @@ import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks; import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.ItemState; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; @@ -33,10 +39,53 @@ public class TestSlimefunItemRegistration { @Test public void testSuccessfulRegistration() { - SlimefunItem item = SlimefunMocks.mockSlimefunItem("TEST_ITEM", new CustomItem(Material.DIAMOND, "&cTest")); + String id = "TEST_ITEM"; + SlimefunItem item = SlimefunMocks.mockSlimefunItem(id, new CustomItem(Material.DIAMOND, "&cTest")); + + Assertions.assertEquals(ItemState.UNREGISTERED, item.getState()); + item.register(plugin); Assertions.assertEquals(ItemState.ENABLED, item.getState()); + Assertions.assertFalse(item.isDisabled()); + Assertions.assertEquals(id, item.getID()); + Assertions.assertFalse(item.isAddonItem()); + Assertions.assertEquals(item, SlimefunItem.getByID(id)); + } + + @Test + public void testDisabledItem() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("DISABLED_ITEM", new CustomItem(Material.DIAMOND, "&cTest")); + SlimefunPlugin.getItemCfg().setValue("DISABLED_ITEM.enabled", false); + item.register(plugin); + + Assertions.assertEquals(ItemState.DISABLED, item.getState()); + Assertions.assertTrue(item.isDisabled()); + } + + @Test + public void testWikiPages() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("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/TheBusyBiscuit/Slimefun4/wiki/Test", wiki.get()); + } + + @Test + public void testGetItemName() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("ITEM_NAME_TEST", new CustomItem(Material.DIAMOND, "&cTest")); + item.register(plugin); + + Assertions.assertEquals(ChatColor.RED + "Test", item.getItemName()); } @Test @@ -44,7 +93,9 @@ public class TestSlimefunItemRegistration { VanillaItem item = SlimefunMocks.mockVanillaItem(Material.ACACIA_SIGN, false); item.register(plugin); + Assertions.assertTrue(item.isUseableInWorkbench()); Assertions.assertEquals(ItemState.VANILLA_FALLBACK, item.getState()); + Assertions.assertTrue(item.isDisabled()); } @Test @@ -59,4 +110,86 @@ public class TestSlimefunItemRegistration { Assertions.assertEquals(ItemState.UNREGISTERED, item2.getState()); } + @Test + public void testRecipeOutput() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("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 + public void testIsItem() { + CustomItem item = new CustomItem(Material.BEACON, "&cItem Test"); + SlimefunItem sfItem = SlimefunMocks.mockSlimefunItem("IS_ITEM_TEST", item); + sfItem.register(plugin); + + Assertions.assertTrue(sfItem.isItem(sfItem.getItem())); + Assertions.assertTrue(sfItem.isItem(item)); + Assertions.assertTrue(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test"))); + + Assertions.assertFalse(sfItem.isItem(null)); + Assertions.assertFalse(sfItem.isItem(new ItemStack(Material.BEACON))); + Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.REDSTONE, "&cTest"))); + + Assertions.assertEquals(sfItem, SlimefunItem.getByItem(item)); + } + + @Test + public void testCategoryRegistration() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_TEST", new CustomItem(Material.DIAMOND, "&cTest")); + item.register(plugin); + item.load(); + + // null should not be a valid argument + Assertions.assertThrows(IllegalArgumentException.class, () -> item.setCategory(null)); + + Category category = SlimefunMocks.getCategory(); + Category category2 = new Category(new NamespacedKey(plugin, "test2"), new CustomItem(Material.OBSIDIAN, "&6Test 2")); + + Assertions.assertTrue(category.contains(item)); + Assertions.assertFalse(category2.contains(item)); + Assertions.assertEquals(category, item.getCategory()); + + item.setCategory(category2); + Assertions.assertFalse(category.contains(item)); + Assertions.assertTrue(category2.contains(item)); + Assertions.assertEquals(category2, item.getCategory()); + } + + @Test + public void testHiddenItem() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("HIDDEN_TEST", new CustomItem(Material.DIAMOND, "&cTest")); + item.setHidden(true); + item.register(plugin); + item.load(); + + Category category = SlimefunMocks.getCategory(); + + Assertions.assertTrue(item.isHidden()); + Assertions.assertFalse(category.contains(item)); + Assertions.assertEquals(category, item.getCategory()); + + item.setHidden(false); + Assertions.assertFalse(item.isHidden()); + Assertions.assertTrue(category.contains(item)); + Assertions.assertEquals(category, item.getCategory()); + + item.setHidden(true); + Assertions.assertTrue(item.isHidden()); + Assertions.assertFalse(category.contains(item)); + Assertions.assertEquals(category, item.getCategory()); + + // Do nothing if the value hasn't changed + item.setHidden(true); + Assertions.assertTrue(item.isHidden()); + Assertions.assertFalse(category.contains(item)); + } } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/listeners/TestVanillaMachinesListener.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/listeners/TestVanillaMachinesListener.java index ebd86f958..b1ac64857 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/listeners/TestVanillaMachinesListener.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/listeners/TestVanillaMachinesListener.java @@ -63,6 +63,27 @@ public class TestVanillaMachinesListener { return event; } + private InventoryClickEvent mockAnvilEvent(ItemStack item) { + Player player = server.addPlayer(); + Inventory inv = SlimefunMocks.mockInventory(InventoryType.ANVIL, item, null, new ItemStack(Material.IRON_CHESTPLATE)); + InventoryView view = player.openInventory(inv); + InventoryClickEvent event = new InventoryClickEvent(view, SlotType.CONTAINER, 2, ClickType.LEFT, InventoryAction.PICKUP_ONE); + + listener.onAnvil(event); + return event; + } + + private InventoryClickEvent mockBrewingEvent(ItemStack item) { + Player player = server.addPlayer(); + Inventory inv = SlimefunMocks.mockInventory(InventoryType.BREWING); + InventoryView view = player.openInventory(inv); + view.setCursor(item); + InventoryClickEvent event = new InventoryClickEvent(view, SlotType.CONTAINER, 1, ClickType.LEFT, InventoryAction.PICKUP_ONE); + + listener.onPreBrew(event); + return event; + } + private CraftItemEvent mockCraftingEvent(ItemStack item) { Recipe recipe = new ShapedRecipe(new NamespacedKey(plugin, "test_recipe"), new ItemStack(Material.EMERALD)); Player player = server.addPlayer(); @@ -106,7 +127,7 @@ public class TestVanillaMachinesListener { } @Test - public void testGrindStoneWithSlimefunItems() { + public void testGrindStoneWithSlimefunItem() { SlimefunItem item = SlimefunMocks.mockSlimefunItem("ENCHANTED_MOCK_BOOK", new CustomItem(Material.ENCHANTED_BOOK, "&6Mock")); item.register(plugin); @@ -176,4 +197,52 @@ public class TestVanillaMachinesListener { PrepareItemCraftEvent event = mockPreCraftingEvent(item.getItem()); Assertions.assertNotNull(event.getInventory().getResult()); } + + @Test + public void testAnvilWithoutSlimefunItems() { + InventoryClickEvent event = mockAnvilEvent(new ItemStack(Material.IRON_SWORD)); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + public void testAnvilWithSlimefunItem() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("MOCKED_IRON_SWORD", new CustomItem(Material.IRON_SWORD, "&6Mock")); + item.register(plugin); + + InventoryClickEvent event = mockAnvilEvent(item.getItem()); + Assertions.assertEquals(Result.DENY, event.getResult()); + } + + @Test + public void testAnvilWithVanillaItem() { + VanillaItem item = SlimefunMocks.mockVanillaItem(Material.IRON_SWORD, true); + item.register(plugin); + + InventoryClickEvent event = mockAnvilEvent(item.getItem()); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + public void testBrewingWithoutSlimefunItems() { + InventoryClickEvent event = mockBrewingEvent(new ItemStack(Material.BLAZE_POWDER)); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } + + @Test + public void testBrewingWithSlimefunItem() { + SlimefunItem item = SlimefunMocks.mockSlimefunItem("MOCK_POWDER", new CustomItem(Material.BLAZE_POWDER, "&6Mock")); + item.register(plugin); + + InventoryClickEvent event = mockBrewingEvent(item.getItem()); + Assertions.assertEquals(Result.DENY, event.getResult()); + } + + @Test + public void testBrewingithVanillaItem() { + VanillaItem item = SlimefunMocks.mockVanillaItem(Material.BLAZE_POWDER, true); + item.register(plugin); + + InventoryClickEvent event = mockBrewingEvent(item.getItem()); + Assertions.assertEquals(Result.DEFAULT, event.getResult()); + } }