Adds a new check for items with full durability (#4)

This commit is contained in:
Kristian Knarvik 2022-10-03 13:03:21 +02:00
parent 5897f68b42
commit 505d6bfb2b
7 changed files with 72 additions and 39 deletions

View File

@ -43,7 +43,9 @@ public enum NPCSetting {
START_REFORGE_MESSAGE("defaults.messages.startReforgeMessage", SettingValueType.STRING, START_REFORGE_MESSAGE("defaults.messages.startReforgeMessage", SettingValueType.STRING,
"&eOk, let's see what I can do...", "startReforgeMessage"), "&eOk, let's see what I can do...", "startReforgeMessage"),
SUCCESS_MESSAGE("defaults.messages.successMessage", SettingValueType.STRING, SUCCESS_MESSAGE("defaults.messages.successMessage", SettingValueType.STRING,
"There you go! All better!", "successMessage"); "There you go! All better!", "successMessage"),
NOT_DAMAGED_MESSAGE("defaults.messages.notDamagedMessage", SettingValueType.STRING,
"&cThat item is not in need of repair", "notDamagedMessage");
private final String path; private final String path;
private final String childPath; private final String childPath;

View File

@ -117,6 +117,15 @@ public class NPCSettings {
return asString(NPCSetting.INVALID_ITEM_MESSAGE); return asString(NPCSetting.INVALID_ITEM_MESSAGE);
} }
/**
* Gets the message to display when the presented item is at full durability
*
* @return <p>The not damaged message</p>
*/
public String getNotDamagedMessage() {
return asString(NPCSetting.NOT_DAMAGED_MESSAGE);
}
/** /**
* Gets the message to display when a blacksmith starts reforging an item * Gets the message to display when a blacksmith starts reforging an item
* *

View File

@ -2,12 +2,12 @@ package net.knarcraft.blacksmith.manager;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.GlobalSettings; import net.knarcraft.blacksmith.config.GlobalSettings;
import net.knarcraft.blacksmith.util.ItemHelper;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
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.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.ServicesManager;
@ -72,37 +72,6 @@ public class EconomyManager {
economy.withdrawPlayer(player, getHeldItemCost(player)); economy.withdrawPlayer(player, getHeldItemCost(player));
} }
/**
* Gets the current durability of the given item
*
* @param itemStack <p>The item to get the durability of</p>
* @return <p>The durability of the item</p>
*/
public static short getDurability(ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
int maxDurability = itemStack.getType().getMaxDurability();
if (damageable != null) {
return (short) (maxDurability - damageable.getDamage());
} else {
return (short) maxDurability;
}
}
/**
* Gets the damage done to the given item
*
* @param itemStack <p>The damage done to the item</p>
* @return <p>The damage done to the item</p>
*/
public static short getDamage(ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
if (damageable != null) {
return (short) damageable.getDamage();
} else {
return 0;
}
}
/** /**
* Gets the cost of the item in the given player's main hand * Gets the cost of the item in the given player's main hand
* *
@ -129,10 +98,10 @@ public class EconomyManager {
double pricePerDurabilityPoint = globalSettings.getPricePerDurabilityPoint(material); double pricePerDurabilityPoint = globalSettings.getPricePerDurabilityPoint(material);
if (globalSettings.getUseNaturalCost()) { if (globalSettings.getUseNaturalCost()) {
//Cost increases with damage //Cost increases with damage
price += ((double) getDamage(item)) * pricePerDurabilityPoint; price += ((double) ItemHelper.getDamage(item)) * pricePerDurabilityPoint;
} else { } else {
//Cost decreases with damage //Cost decreases with damage
price += ((double) getDurability(item)) * pricePerDurabilityPoint; price += ((double) ItemHelper.getDurability(item)) * pricePerDurabilityPoint;
} }
//Increase price for any enchantments //Increase price for any enchantments

View File

@ -6,6 +6,7 @@ import net.citizensnpcs.api.util.DataKey;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.NPCSettings; import net.knarcraft.blacksmith.config.NPCSettings;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.enchantments.EnchantmentTarget;
@ -173,6 +174,11 @@ public class BlacksmithTrait extends Trait {
return; return;
} }
if (ItemHelper.getDamage(hand) == 0) {
sendNPCMessage(this.npc, player, config.getNotDamagedMessage());
return;
}
//Start a new reforge session for the player //Start a new reforge session for the player
_sessionStart = System.currentTimeMillis(); _sessionStart = System.currentTimeMillis();
session = new ReforgeSession(this, player, npc, config); session = new ReforgeSession(this, player, npc, config);

View File

@ -4,6 +4,7 @@ import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.NPCSettings; import net.knarcraft.blacksmith.config.NPCSettings;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
@ -163,7 +164,7 @@ public class ReforgeSession implements Runnable {
} }
// Damage the item // Damage the item
short currentItemDurability = EconomyManager.getDurability(itemToReforge); short currentItemDurability = ItemHelper.getDurability(itemToReforge);
short newDurability = (short) (currentItemDurability + (currentItemDurability * random.nextInt(8))); short newDurability = (short) (currentItemDurability + (currentItemDurability * random.nextInt(8)));
short maxDurability = itemToReforge.getType().getMaxDurability(); short maxDurability = itemToReforge.getType().getMaxDurability();
if (newDurability <= 0) { if (newDurability <= 0) {

View File

@ -0,0 +1,43 @@
package net.knarcraft.blacksmith.util;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
public final class ItemHelper {
private ItemHelper() {
}
/**
* Gets the current durability of the given item
*
* @param itemStack <p>The item to get the durability of</p>
* @return <p>The durability of the item</p>
*/
public static short getDurability(ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
int maxDurability = itemStack.getType().getMaxDurability();
if (damageable != null) {
return (short) (maxDurability - damageable.getDamage());
} else {
return (short) maxDurability;
}
}
/**
* Gets the damage done to the given item
*
* @param itemStack <p>The damage done to the item</p>
* @return <p>The damage done to the item</p>
*/
public static short getDamage(ItemStack itemStack) {
Damageable damageable = (Damageable) itemStack.getItemMeta();
if (damageable != null) {
return (short) damageable.getDamage();
} else {
return 0;
}
}
}

View File

@ -1,6 +1,6 @@
# Blacksmith Configuration # Blacksmith Configuration
# The settings which apply to all Blacksmith NPCs # The settings which apply to all Blacksmith NPCs. These can also be changed using the /blacksmithconfig command
global: global:
# The minimum price of each cost # The minimum price of each cost
basePrice: basePrice:
@ -22,7 +22,7 @@ global:
useNaturalCost: true useNaturalCost: true
# 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 # Citizens NPC file, or use the /blacksmith command
defaults: defaults:
# Whether the item will drop a reforged item on the ground, instead of putting it into the user's inventory # Whether the item will drop a reforged item on the ground, instead of putting it into the user's inventory
dropItem: true dropItem: true
@ -88,4 +88,7 @@ defaults:
startReforgeMessage: "&eOk, let's see what I can do..." startReforgeMessage: "&eOk, let's see what I can do..."
# The message to display once the reforging has successfully finished # The message to display once the reforging has successfully finished
successMessage: "There you go! All better!" successMessage: "There you go! All better!"
# The message to display if a player is trying to reforge an item with full durability
notDamagedMessage: "&cThat item is not in need of repair"