diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java index 03ed165..365d0d7 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java @@ -11,8 +11,6 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scheduler.BukkitScheduler; import java.util.ArrayList; @@ -209,14 +207,9 @@ public class ReforgeSession implements Runnable { * @param newDamage

The new damage done

*/ private void updateDamage(ItemStack item, int newDamage) { - ItemMeta meta = item.getItemMeta(); - Damageable damageable = (Damageable) meta; - if (damageable != null) { - damageable.setDamage(newDamage); - } else { + if (!ItemHelper.updateDamage(item, newDamage)) { BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to change damage of " + item); } - item.setItemMeta(meta); } /** diff --git a/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java index c4f76bb..d804d50 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ItemHelper.java @@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.util; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.List; @@ -67,6 +68,23 @@ public final class ItemHelper { } } + /** + * Updates the damage done to an item + * + * @param item

The item to update damage for

+ * @param newDamage

The new damage done

+ * @return

True if the damage was updated. False if not damageable.

+ */ + public static boolean updateDamage(ItemStack item, int newDamage) { + ItemMeta meta = item.getItemMeta(); + if (!(meta instanceof Damageable damageable)) { + return false; + } + damageable.setDamage(newDamage); + item.setItemMeta(meta); + return true; + } + /** * Gets a complete list of all reforge-able materials * diff --git a/src/test/java/net/knarcraft/blacksmith/util/ItemHelperTest.java b/src/test/java/net/knarcraft/blacksmith/util/ItemHelperTest.java index 31a9e88..7ba01ad 100644 --- a/src/test/java/net/knarcraft/blacksmith/util/ItemHelperTest.java +++ b/src/test/java/net/knarcraft/blacksmith/util/ItemHelperTest.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -49,4 +51,66 @@ public class ItemHelperTest { assertEquals(0, ItemHelper.getMaxDurability(new ItemStack(Material.POTATO, 1))); } + @Test + public void updateAndGetDamageTest() { + for (Material material : Material.values()) { + //A lot of items seem to have weird metadata which isn't mocked properly. Therefore, this test is limited. + if (!material.name().endsWith("_PICKAXE")) { + continue; + } + ItemStack itemStack = new ItemStack(material, 1); + assertEquals(0, ItemHelper.getDamage(itemStack)); + ItemHelper.updateDamage(itemStack, 3); + assertEquals(3, ItemHelper.getDamage(itemStack)); + } + } + + @Test + public void getDurabilityTest() { + for (Material material : Material.values()) { + ItemStack itemStack = new ItemStack(material, 1); + assertEquals(ItemHelper.getMaxDurability(itemStack), ItemHelper.getDurability(itemStack)); + } + + for (Material material : Material.values()) { + //A lot of items seem to have weird metadata which isn't mocked properly. Therefore, this test is limited. + if (!material.name().endsWith("_PICKAXE")) { + continue; + } + ItemStack itemStack = new ItemStack(material, 1); + ItemHelper.updateDamage(itemStack, 100); + assertEquals(ItemHelper.getMaxDurability(itemStack) - 100, ItemHelper.getDurability(itemStack)); + } + } + + @Test + public void getAllReforgeAbleMaterialsTest() { + List materials = ItemHelper.getAllReforgeAbleMaterials(); + assertTrue(materials.size() > 0); + + for (Material material : materials) { + assertTrue(ItemHelper.getMaxDurability(new ItemStack(material, 1)) > 0); + } + } + + @Test + public void isAnvilRequiresDamagedTest() { + assertTrue(ItemHelper.isAnvil(Material.DAMAGED_ANVIL, true)); + assertTrue(ItemHelper.isAnvil(Material.CHIPPED_ANVIL, true)); + + assertFalse(ItemHelper.isAnvil(Material.ANVIL, true)); + assertFalse(ItemHelper.isAnvil(Material.POTATO, true)); + assertFalse(ItemHelper.isAnvil(Material.IRON_HOE, true)); + } + + @Test + public void isAnvilTest() { + assertTrue(ItemHelper.isAnvil(Material.ANVIL, false)); + assertTrue(ItemHelper.isAnvil(Material.DAMAGED_ANVIL, false)); + assertTrue(ItemHelper.isAnvil(Material.CHIPPED_ANVIL, false)); + + assertFalse(ItemHelper.isAnvil(Material.POTATO, false)); + assertFalse(ItemHelper.isAnvil(Material.IRON_HOE, false)); + } + }