Adds optional ability to reforge anvils #15
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
2023-01-09 23:47:17 +01:00
parent a856aa03e0
commit 753c7c6275
10 changed files with 95 additions and 14 deletions

View File

@ -26,7 +26,8 @@ public enum GlobalSetting {
*
* <p>This can be specified for each possible enchantment by setting enchantment-cost.enchantment_name</p>
*/
ENCHANTMENT_COST("global.enchantmentCost.default", SettingValueType.POSITIVE_DOUBLE, 5.0, "enchantmentCost"),
ENCHANTMENT_COST("global.enchantmentCost.default", SettingValueType.POSITIVE_DOUBLE, 5.0,
"enchantmentCost"),
/**
* Whether the cost should increase for damage taken, as opposed to increase for durability present
@ -36,7 +37,19 @@ public enum GlobalSetting {
/**
* Whether to show exact time when displaying the wait time for a reforging or the cool-down
*/
SHOW_EXACT_TIME("global.showExactTime", SettingValueType.BOOLEAN, false, "showExactTime");
SHOW_EXACT_TIME("global.showExactTime", SettingValueType.BOOLEAN, false, "showExactTime"),
/**
* The cost for repairing a chipped anvil
*/
ANVIL_CHIPPED_COST("global.chippedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 10.0,
"chippedAnvilReforgingCost"),
/**
* The cost for repairing a damaged anvil
*/
ANVIL_DAMAGED_COST("global.damagedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 20.0,
"damagedAnvilReforgingCost");
private final String path;
private final String parent;

View File

@ -252,6 +252,22 @@ public class GlobalSettings {
}
}
/**
* Gets the cost for repairing the given type of anvil
*
* @param material <p>The anvil material to repair</p>
* @return <p>The cost of repairing the anvil</p>
*/
public double getAnvilCost(Material material) {
if (material == Material.CHIPPED_ANVIL) {
return asDouble(GlobalSetting.ANVIL_CHIPPED_COST);
} else if (material == Material.DAMAGED_ANVIL) {
return asDouble(GlobalSetting.ANVIL_DAMAGED_COST);
} else {
throw new IllegalArgumentException("An unexpected item was encountered!");
}
}
/**
* Gets the given value as a boolean
*

View File

@ -62,6 +62,11 @@ public enum NPCSetting {
ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, new String[]{"binding_curse",
"mending", "vanishing_curse"}, "enchantmentBlocklist"),
/**
* Whether to allow this blacksmith to repair anvils
*/
REPAIR_ANVILS("reforgeAnvils", SettingValueType.BOOLEAN, false, "reforgeAnvils"),
/*-----------
| Messages |
-----------*/

View File

@ -301,6 +301,15 @@ public class NPCSettings {
return asInt(NPCSetting.MAX_REFORGE_DELAY) <= 0;
}
/**
* Gets whether this blacksmith is able to repair anvils
*
* @return <p>True if this blacksmith is able to repair anvils</p>
*/
public boolean getRepairAnvils() {
return ConfigHelper.asBoolean(getValue(NPCSetting.REPAIR_ANVILS));
}
/**
* Gets the given value as an integer
*

View File

@ -91,6 +91,7 @@ public class EconomyManager {
private static double getCost(ItemStack item) {
GlobalSettings globalSettings = BlacksmithPlugin.getInstance().getSettings();
Material material = item.getType();
//Calculate the base price
double price = globalSettings.getBasePrice(material);
@ -106,6 +107,11 @@ public class EconomyManager {
//Increase price for any enchantments
price += getEnchantmentCost(item);
//Override the cost for anvils
if (ItemHelper.isAnvil(material, true)) {
price = globalSettings.getAnvilCost(material);
}
return price;
}

View File

@ -114,7 +114,7 @@ public class BlacksmithTrait extends Trait {
coolDowns.remove(playerId);
}
//Deny if permission is missing
if (!player.hasPermission("blacksmith.reforge")) {
if (!player.hasPermission("blacksmith.use")) {
return false;
}
@ -157,8 +157,8 @@ public class BlacksmithTrait extends Trait {
if (session.isRunning()) {
int timeRemaining = (int) ((session.getFinishTime() - System.currentTimeMillis()) / 1000);
boolean showExactTime = BlacksmithPlugin.getInstance().getSettings().getShowExactTime();
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getBusyReforgingMessage(), "{time}",
TimeFormatter.formatTime(showExactTime, timeRemaining)));
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getBusyReforgingMessage(),
"{time}", TimeFormatter.formatTime(showExactTime, timeRemaining)));
return;
}
if (session.endSession()) {
@ -179,14 +179,15 @@ public class BlacksmithTrait extends Trait {
ItemStack hand = player.getInventory().getItemInMainHand();
//Refuse if not repairable, or if reforge-able items is set, but doesn't include the held item
List<Material> reforgeAbleItems = config.getReforgeAbleItems();
if (!isRepairable(hand) || (!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType()))) {
if ((!this.config.getRepairAnvils() || !ItemHelper.isAnvil(hand.getType(), false)) &&
(!isRepairable(hand) || (!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType())))) {
String invalidMessage = StringFormatter.replacePlaceholder(config.getInvalidItemMessage(),
"{title}", config.getBlacksmithTitle());
sendNPCMessage(this.npc, player, invalidMessage);
return;
}
if (ItemHelper.getDamage(hand) == 0) {
if (ItemHelper.getDamage(hand) == 0 && !ItemHelper.isAnvil(hand.getType(), true)) {
sendNPCMessage(this.npc, player, config.getNotDamagedMessage());
return;
}

View File

@ -6,6 +6,7 @@ import net.knarcraft.blacksmith.config.NPCSettings;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
@ -129,6 +130,11 @@ public class ReforgeSession implements Runnable {
// Remove any damage done to the item
updateDamage(itemToReforge, 0);
//Replace damaged anvils with a normal anvil
if (ItemHelper.isAnvil(itemToReforge.getType(), true)) {
itemToReforge.setType(Material.ANVIL);
}
// Add random enchantments
int roll = random.nextInt(100);
if (!(roll < config.getExtraEnchantmentChance() &&

View File

@ -61,4 +61,17 @@ public final class ItemHelper {
return reforgeAbleMaterials;
}
/**
* Checks whether the given material is an anvil
*
* @param material <p>The material to check</p>
* @param requireDamaged <p>Whether only a damaged anvil should count</p>
* @return <p>True if the given material is an anvil</p>
*/
public static boolean isAnvil(Material material, boolean requireDamaged) {
boolean isDamagedAnvil = material == Material.CHIPPED_ANVIL || material == Material.DAMAGED_ANVIL;
boolean isAnvil = isDamagedAnvil || material == Material.ANVIL;
return (requireDamaged && isDamagedAnvil) || (!requireDamaged && isAnvil);
}
}

View File

@ -27,6 +27,12 @@ global:
# Exact time displays the exact number of seconds and minutes remaining as part of the reforging cool-down and
# reforging delay messages, instead of just vaguely hinting at the remaining time.
showExactTime: false
# The cost of fully repairing a chipped anvil
chippedAnvilReforgingCost: 10.0
# The cost of fully repairing a damaged anvil
damagedAnvilReforgingCost: 20.0
# 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
@ -50,6 +56,9 @@ defaults:
# The maximum number of enchantments the blacksmith will try to add
maxEnchantments: 3
# Whether the blacksmith will reforge anvils as a special case
reforgeAnvils: false
# All settable delays
delaysInSeconds: