Removes disableMaterialLimitation
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

Removes the disableMaterialLimitation option
Replaces EnchantmentTarget.BREAKABLE.includes(item) with getMaxDurability(item) > 0 as it seems more generic
This commit is contained in:
Kristian Knarvik 2023-01-16 19:45:41 +01:00
parent e5cb3b4a30
commit 347b69b2a8
9 changed files with 26 additions and 64 deletions

View File

@ -49,15 +49,7 @@ public enum GlobalSetting {
* The cost for repairing a damaged anvil * The cost for repairing a damaged anvil
*/ */
ANVIL_DAMAGED_COST("global.damagedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 20.0, ANVIL_DAMAGED_COST("global.damagedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 20.0,
"damagedAnvilReforgingCost"), "damagedAnvilReforgingCost");
/**
* Whether to disable the normal limitation for which items are reforgeAble
*
* <p>If true, all items instanceof Damageable can be reforged</p>
*/
DISABLE_MATERIAL_LIMITATION("global.disableMaterialLimitation", SettingValueType.BOOLEAN, false,
"disableMaterialLimitation");
private final String path; private final String path;
private final String parent; private final String parent;

View File

@ -295,15 +295,6 @@ public class GlobalSettings {
} }
} }
/**
* Whether to disable the "EnchantmentTarget.BREAKABLE" limitation
*
* @return <p>True if the material limitation is disabled</p>
*/
public boolean disableMaterialLimitation() {
return asBoolean(GlobalSetting.DISABLE_MATERIAL_LIMITATION);
}
/** /**
* Gets the given value as a boolean * Gets the given value as a boolean
* *

View File

@ -460,8 +460,7 @@ public class NPCSettings {
} }
Material material = InputParsingHelper.matchMaterial(item); Material material = InputParsingHelper.matchMaterial(item);
boolean limitationDisabled = BlacksmithPlugin.getInstance().getSettings().disableMaterialLimitation(); if (material != null && ItemHelper.isRepairable(new ItemStack(material, 1))) {
if (material != null && ItemHelper.isRepairable(new ItemStack(material, 1), limitationDisabled)) {
if (!blacklist) { if (!blacklist) {
reforgeAbleItems.add(material); reforgeAbleItems.add(material);
} else { } else {

View File

@ -179,8 +179,7 @@ public class BlacksmithTrait extends Trait {
List<Material> reforgeAbleItems = config.getReforgeAbleItems(); List<Material> reforgeAbleItems = config.getReforgeAbleItems();
boolean notHoldingAnvil = !this.config.getRepairAnvils() || !ItemHelper.isAnvil(hand.getType(), false); boolean notHoldingAnvil = !this.config.getRepairAnvils() || !ItemHelper.isAnvil(hand.getType(), false);
boolean limitationDisabled = BlacksmithPlugin.getInstance().getSettings().disableMaterialLimitation(); boolean notHoldingRepairable = !ItemHelper.isRepairable(hand) ||
boolean notHoldingRepairable = !ItemHelper.isRepairable(hand, limitationDisabled) ||
(!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType())); (!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType()));
if (notHoldingAnvil && notHoldingRepairable) { if (notHoldingAnvil && notHoldingRepairable) {

View File

@ -1,7 +1,6 @@
package net.knarcraft.blacksmith.util; package net.knarcraft.blacksmith.util;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
@ -20,13 +19,11 @@ public final class ItemHelper {
/** /**
* Gets whether the given item is repairable * Gets whether the given item is repairable
* *
* @param item <p>The item to check</p> * @param item <p>The item to check</p>
* @param disableMaterialLimitation <p>Whether to disable the EnchantmentTarget.BREAKABLE limitation</p>
* @return <p>True if the item is repairable</p> * @return <p>True if the item is repairable</p>
*/ */
public static boolean isRepairable(ItemStack item, boolean disableMaterialLimitation) { public static boolean isRepairable(ItemStack item) {
return item.getItemMeta() instanceof Damageable && (disableMaterialLimitation || return item.getItemMeta() instanceof Damageable && getMaxDurability(item) > 0;
EnchantmentTarget.BREAKABLE.includes(item));
} }
/** /**
@ -79,7 +76,7 @@ public final class ItemHelper {
List<Material> reforgeAbleMaterials = new ArrayList<>(); List<Material> reforgeAbleMaterials = new ArrayList<>();
for (Material material : Material.values()) { for (Material material : Material.values()) {
ItemStack item = new ItemStack(material); ItemStack item = new ItemStack(material);
if (item.getItemMeta() instanceof Damageable && EnchantmentTarget.BREAKABLE.includes(item)) { if (isRepairable(item)) {
reforgeAbleMaterials.add(material); reforgeAbleMaterials.add(material);
} }
} }

View File

@ -23,16 +23,14 @@ public final class SalvageHelper {
* *
* <p>Note: Only items craft-able in a crafting table are salvageable. Netherite gear is therefore not salvageable.</p> * <p>Note: Only items craft-able in a crafting table are salvageable. Netherite gear is therefore not salvageable.</p>
* *
* @param server <p>The server to get recipes from</p> * @param server <p>The server to get recipes from</p>
* @param salvagedItem <p>The item stack to salvage</p> * @param salvagedItem <p>The item stack to salvage</p>
* @param ignoredSalvage <p>Any material which should not be returned as part of the salvage.</p> * @param ignoredSalvage <p>Any material which should not be returned as part of the salvage.</p>
* @param limitationDisabled <p>Whether the repairable item limitation is disabled</p>
* @return <p>The items to return to the user, or null if not salvageable</p> * @return <p>The items to return to the user, or null if not salvageable</p>
*/ */
public static List<ItemStack> getSalvage(Server server, ItemStack salvagedItem, List<Material> ignoredSalvage, public static List<ItemStack> getSalvage(Server server, ItemStack salvagedItem, List<Material> ignoredSalvage) {
boolean limitationDisabled) {
if (salvagedItem == null || salvagedItem.getAmount() < 1 || if (salvagedItem == null || salvagedItem.getAmount() < 1 ||
!ItemHelper.isRepairable(salvagedItem, limitationDisabled)) { !ItemHelper.isRepairable(salvagedItem)) {
return null; return null;
} }

View File

@ -34,10 +34,6 @@ global:
# The cost of fully repairing a damaged anvil # The cost of fully repairing a damaged anvil
damagedAnvilReforgingCost: 20.0 damagedAnvilReforgingCost: 20.0
# This disables the EnchantmentTarget.BREAKABLE limitation. If set to true, all items able to have durability are seen
# as reforgeAble. This should not be set to true unless you have special needs.
disableMaterialLimitation: false
# The settings which are set to any new NPC. To change any of these settings for an existing NPC, you must change the # The settings which are set to any new NPC. To change any of these settings for an existing NPC, you must change the
# Citizens NPC file, or use the /blacksmith command # Citizens NPC file, or use the /blacksmith command
defaults: defaults:

View File

@ -4,11 +4,11 @@ import be.seeseemelk.mockbukkit.MockBukkit;
import net.knarcraft.blacksmith.CustomServerMock; import net.knarcraft.blacksmith.CustomServerMock;
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.junit.jupiter.api.AfterAll; 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 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;
@ -36,27 +36,17 @@ public class ItemHelperTest {
name.endsWith("_HELMET") || name.equals("ELYTRA") || name.endsWith("BOW") || name.endsWith("_HELMET") || name.equals("ELYTRA") || name.endsWith("BOW") ||
name.endsWith("_LEGGINGS") || name.endsWith("_BOOTS") || name.equals("TRIDENT") || name.endsWith("_LEGGINGS") || name.endsWith("_BOOTS") || name.equals("TRIDENT") ||
name.equals("FISHING_ROD") || name.equals("FLINT_AND_STEEL") || name.equals("SHEARS")) { name.equals("FISHING_ROD") || name.equals("FLINT_AND_STEEL") || name.equals("SHEARS")) {
assertTrue(ItemHelper.isRepairable(new ItemStack(material, 1), false)); assertTrue(ItemHelper.isRepairable(new ItemStack(material, 1)));
} }
} }
assertFalse(ItemHelper.isRepairable(new ItemStack(Material.POTATO, 1), false)); assertFalse(ItemHelper.isRepairable(new ItemStack(Material.POTATO, 1)));
assertFalse(ItemHelper.isRepairable(new ItemStack(Material.DIRT, 1), false)); assertFalse(ItemHelper.isRepairable(new ItemStack(Material.DIRT, 1)));
} }
@Test @Test
public void isRepairableLimitDisabledTest() { public void getMaxDurabilityTest() {
/*The assertFalse pert of this test is kind of pointless, as every material in the game seems to implement assertEquals(1561, ItemHelper.getMaxDurability(new ItemStack(Material.DIAMOND_PICKAXE, 1)));
Damageable */ assertEquals(0, ItemHelper.getMaxDurability(new ItemStack(Material.POTATO, 1)));
for (Material material : Material.values()) {
ItemStack itemStack = new ItemStack(material, 1);
if (new ItemStack(material, 1).getItemMeta() instanceof Damageable) {
assertTrue(ItemHelper.isRepairable(itemStack, true));
} else {
assertFalse(ItemHelper.isRepairable(itemStack, true));
}
}
assertTrue(ItemHelper.isRepairable(new ItemStack(Material.POTATO, 1), true));
assertTrue(ItemHelper.isRepairable(new ItemStack(Material.DIRT, 1), true));
} }
} }

View File

@ -40,14 +40,14 @@ public class SalvageHelperTest {
@Test @Test
public void getNullForInvalidItemTest() { public void getNullForInvalidItemTest() {
//Assert that a non-reforge-able item will return null //Assert that a non-reforge-able item will return null
assertNull(SalvageHelper.getSalvage(server, new ItemStack(Material.POTATO, 1), null, false)); assertNull(SalvageHelper.getSalvage(server, new ItemStack(Material.POTATO, 1), null));
} }
@Test @Test
public void getNullForLessThanOneItemTest() { public void getNullForLessThanOneItemTest() {
//Assert that 0 or 1 items will return null //Assert that 0 or 1 items will return null
assertNull(SalvageHelper.getSalvage(server, new ItemStack(Material.IRON_AXE, 0), null, false)); assertNull(SalvageHelper.getSalvage(server, new ItemStack(Material.IRON_AXE, 0), null));
assertNull(SalvageHelper.getSalvage(server, new ItemStack(Material.IRON_SWORD, -1), null, false)); assertNull(SalvageHelper.getSalvage(server, new ItemStack(Material.IRON_SWORD, -1), null));
} }
@Test @Test
@ -57,7 +57,7 @@ public class SalvageHelperTest {
expectedSalvage.add(new ItemStack(Material.STICK, 2)); expectedSalvage.add(new ItemStack(Material.STICK, 2));
ItemStack itemToSalvage = new ItemStack(Material.DIAMOND_PICKAXE, 1); ItemStack itemToSalvage = new ItemStack(Material.DIAMOND_PICKAXE, 1);
//Note: Conversion to sets makes sure the order doesn't matter //Note: Conversion to sets makes sure the order doesn't matter
assertEquals(expectedSalvage, new HashSet<>(SalvageHelper.getSalvage(server, itemToSalvage, null, false))); assertEquals(expectedSalvage, new HashSet<>(SalvageHelper.getSalvage(server, itemToSalvage, null)));
} }
@Test @Test
@ -67,7 +67,7 @@ public class SalvageHelperTest {
expectedSalvage.add(new ItemStack(Material.STICK, 14)); expectedSalvage.add(new ItemStack(Material.STICK, 14));
ItemStack itemToSalvage = new ItemStack(Material.DIAMOND_PICKAXE, 7); ItemStack itemToSalvage = new ItemStack(Material.DIAMOND_PICKAXE, 7);
//Note: Conversion to sets makes sure the order doesn't matter //Note: Conversion to sets makes sure the order doesn't matter
assertEquals(expectedSalvage, new HashSet<>(SalvageHelper.getSalvage(server, itemToSalvage, null, false))); assertEquals(expectedSalvage, new HashSet<>(SalvageHelper.getSalvage(server, itemToSalvage, null)));
} }
@Test @Test
@ -82,7 +82,7 @@ public class SalvageHelperTest {
damageable.setDamage(100); damageable.setDamage(100);
} }
itemToSalvage.setItemMeta(meta); itemToSalvage.setItemMeta(meta);
List<ItemStack> salvage = SalvageHelper.getSalvage(server, itemToSalvage, null, false); List<ItemStack> salvage = SalvageHelper.getSalvage(server, itemToSalvage, null);
//Assert that some items are given //Assert that some items are given
assertNotEquals(salvage, new ArrayList<>()); assertNotEquals(salvage, new ArrayList<>());
//Assert that a damaged item won't give full salvage //Assert that a damaged item won't give full salvage
@ -102,7 +102,7 @@ public class SalvageHelperTest {
damageable.setDamage(100); damageable.setDamage(100);
} }
itemToSalvage.setItemMeta(meta); itemToSalvage.setItemMeta(meta);
List<ItemStack> salvage = SalvageHelper.getSalvage(server, itemToSalvage, ignoredSalvage, false); List<ItemStack> salvage = SalvageHelper.getSalvage(server, itemToSalvage, ignoredSalvage);
//Assert that some items are given //Assert that some items are given
assertNotEquals(salvage, new ArrayList<>()); assertNotEquals(salvage, new ArrayList<>());
//Assert that a damaged diamond pickaxe with sticks ignored returns 2 diamonds a salvage //Assert that a damaged diamond pickaxe with sticks ignored returns 2 diamonds a salvage