Adds missing tests for ItemHelper
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2023-01-17 05:28:46 +01:00
parent 347b69b2a8
commit a79f2e273a
3 changed files with 83 additions and 8 deletions

View File

@ -11,8 +11,6 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import java.util.ArrayList; import java.util.ArrayList;
@ -209,14 +207,9 @@ public class ReforgeSession implements Runnable {
* @param newDamage <p>The new damage done</p> * @param newDamage <p>The new damage done</p>
*/ */
private void updateDamage(ItemStack item, int newDamage) { private void updateDamage(ItemStack item, int newDamage) {
ItemMeta meta = item.getItemMeta(); if (!ItemHelper.updateDamage(item, newDamage)) {
Damageable damageable = (Damageable) meta;
if (damageable != null) {
damageable.setDamage(newDamage);
} else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to change damage of " + item); BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to change damage of " + item);
} }
item.setItemMeta(meta);
} }
/** /**

View File

@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.util;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -67,6 +68,23 @@ public final class ItemHelper {
} }
} }
/**
* Updates the damage done to an item
*
* @param item <p>The item to update damage for</p>
* @param newDamage <p>The new damage done</p>
* @return <p>True if the damage was updated. False if not damageable.</p>
*/
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 * Gets a complete list of all reforge-able materials
* *

View File

@ -8,6 +8,8 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -49,4 +51,66 @@ public class ItemHelperTest {
assertEquals(0, ItemHelper.getMaxDurability(new ItemStack(Material.POTATO, 1))); 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<Material> 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));
}
} }