Fixes several bugs and problems

Fixes getting the name of enchantments
Fixes inconsistencies in material and enchantment name-checking
Allows using "null" or "-1" to unset per-material or per-enchantment configuration options
Fixes a bug where basePrice was set to a material name instead of the price for the material being displayed
Adds missing tab-completion for material/enchantment costs
Prevents inconsistencies in deciding if a value is null
This commit is contained in:
Kristian Knarvik 2022-10-24 13:57:58 +02:00
parent 39e164c9c8
commit cc7d66f270
8 changed files with 102 additions and 35 deletions

View File

@ -11,7 +11,6 @@ import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -81,7 +80,14 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} }
//Change the value of the specified setting //Change the value of the specified setting
if ((detectedGlobalSetting != null &&
TypeValidationHelper.isValid(detectedGlobalSetting.getValueType(), args[1], sender)) ||
(detectedNPCSetting != null &&
TypeValidationHelper.isValid(detectedNPCSetting.getValueType(), args[1], sender))) {
return changeValue(args, detectedGlobalSetting, detectedNPCSetting, settings, sender); return changeValue(args, detectedGlobalSetting, detectedNPCSetting, settings, sender);
} else {
return false;
}
} }
/** /**
@ -166,7 +172,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
private boolean displaySpecialCaseValue(String selector, CommandSender sender, GlobalSetting setting, private boolean displaySpecialCaseValue(String selector, CommandSender sender, GlobalSetting setting,
GlobalSettings settings) { GlobalSettings settings) {
if (setting == GlobalSetting.BASE_PRICE || setting == GlobalSetting.PRICE_PER_DURABILITY_POINT) { if (setting == GlobalSetting.BASE_PRICE || setting == GlobalSetting.PRICE_PER_DURABILITY_POINT) {
Material material = Material.matchMaterial(selector); Material material = InputParsingHelper.matchMaterial(selector);
if (material == null) { if (material == null) {
return false; return false;
} }
@ -176,16 +182,17 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} else { } else {
currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material)); currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material));
} }
displaySuccessMessage(sender, TranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(), ItemType.MATERIAL, displaySuccessMessage(sender, TranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
material.name(), currentValue)); ItemType.MATERIAL, material.name(), currentValue));
return true; return true;
} else if (setting == GlobalSetting.ENCHANTMENT_COST) { } else if (setting == GlobalSetting.ENCHANTMENT_COST) {
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(selector)); Enchantment enchantment = InputParsingHelper.matchEnchantment(selector);
if (enchantment == null) { if (enchantment == null) {
return false; return false;
} }
displaySuccessMessage(sender, TranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(), ItemType.ENCHANTMENT, displaySuccessMessage(sender, TranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
enchantment.toString(), String.valueOf(settings.getEnchantmentCost(enchantment)))); ItemType.ENCHANTMENT, enchantment.getKey().getKey(),
String.valueOf(settings.getEnchantmentCost(enchantment))));
return true; return true;
} else { } else {
return false; return false;
@ -215,7 +222,9 @@ public class BlackSmithConfigCommand implements CommandExecutor {
*/ */
private boolean updateSpecialCase(GlobalSettings settings, GlobalSetting detectedGlobalSetting, String[] args, private boolean updateSpecialCase(GlobalSettings settings, GlobalSetting detectedGlobalSetting, String[] args,
CommandSender sender) { CommandSender sender) {
if (!TypeValidationHelper.isValid(SettingValueType.POSITIVE_DOUBLE, args[2], sender)) { if (InputParsingHelper.isEmpty(args[2])) {
args[2] = "-1";
} else if (!TypeValidationHelper.isValid(SettingValueType.POSITIVE_DOUBLE, args[2], sender)) {
return true; return true;
} }
double newPrice = Double.parseDouble(args[2]); double newPrice = Double.parseDouble(args[2]);
@ -242,7 +251,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
return false; return false;
} }
itemType = ItemType.ENCHANTMENT; itemType = ItemType.ENCHANTMENT;
itemChanged = enchantment.toString(); itemChanged = enchantment.getKey().getKey();
settings.setEnchantmentCost(enchantment, newPrice); settings.setEnchantmentCost(enchantment, newPrice);
} else { } else {
return false; return false;

View File

@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.config.GlobalSetting; import net.knarcraft.blacksmith.config.GlobalSetting;
import net.knarcraft.blacksmith.config.NPCSetting; import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
@ -28,6 +29,8 @@ public class BlackSmithConfigTabCompleter implements TabCompleter {
return new ArrayList<>(); return new ArrayList<>();
} }
//Arguments: <setting> [new value/material or enchantment] []
if (args.length == 1) { if (args.length == 1) {
List<String> availableCommands = new ArrayList<>(); List<String> availableCommands = new ArrayList<>();
availableCommands.add("reload"); availableCommands.add("reload");
@ -40,10 +43,38 @@ public class BlackSmithConfigTabCompleter implements TabCompleter {
return filterMatchingContains(availableCommands, args[0]); return filterMatchingContains(availableCommands, args[0]);
} else if (args.length == 2) { } else if (args.length == 2) {
return tabCompleteCommandValues(args[0], args[1]); return tabCompleteCommandValues(args[0], args[1]);
} else if (args.length == 3) {
//Get per-material tab completions, or return nothing if an invalid setting was specified
for (GlobalSetting globalSetting : GlobalSetting.values()) {
if (globalSetting.getCommandName().equalsIgnoreCase(args[0])) {
return getPerTypeTabCompletions(globalSetting, args);
}
}
return new ArrayList<>();
} }
return null; return null;
} }
/**
* Gets tab-completions for a selected material or enchantment
*
* @param globalSetting <p>The global setting to get tab-completions for</p>
* @param args <p>The arguments given by the user</p>
* @return <p>The tab-completions to show to the user</p>
*/
private List<String> getPerTypeTabCompletions(GlobalSetting globalSetting, String[] args) {
//Display possible tab-completions only if a valid enchantment or material is provided
if (((globalSetting == GlobalSetting.BASE_PRICE ||
globalSetting == GlobalSetting.PRICE_PER_DURABILITY_POINT) &&
InputParsingHelper.matchMaterial(args[1]) != null) ||
(globalSetting == GlobalSetting.ENCHANTMENT_COST &&
InputParsingHelper.matchEnchantment(args[1]) != null)) {
return filterMatchingContains(getTabCompletions(globalSetting.getValueType()), args[2]);
} else {
return new ArrayList<>();
}
}
/** /**
* Tab completes the values available for the given command * Tab completes the values available for the given command
* *

View File

@ -9,6 +9,7 @@ import net.knarcraft.blacksmith.formatting.StringFormatter;
import net.knarcraft.blacksmith.formatting.TranslatableMessage; import net.knarcraft.blacksmith.formatting.TranslatableMessage;
import net.knarcraft.blacksmith.formatting.Translator; import net.knarcraft.blacksmith.formatting.Translator;
import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -72,7 +73,7 @@ public class BlackSmithEditCommand implements CommandExecutor {
displayNPCSetting(blacksmithTrait, npcSetting, sender); displayNPCSetting(blacksmithTrait, npcSetting, sender);
} else { } else {
//If an empty value or null, clear the value instead of changing it //If an empty value or null, clear the value instead of changing it
if (newValue.equalsIgnoreCase("null") || newValue.equals("\"\"") || newValue.trim().isEmpty()) { if (InputParsingHelper.isEmpty(newValue)) {
newValue = null; newValue = null;
} else { } else {
//Abort if an invalid value is given //Abort if an invalid value is given
@ -101,7 +102,7 @@ public class BlackSmithEditCommand implements CommandExecutor {
*/ */
private void displayNPCSetting(BlacksmithTrait blacksmithTrait, NPCSetting npcSetting, CommandSender sender) { private void displayNPCSetting(BlacksmithTrait blacksmithTrait, NPCSetting npcSetting, CommandSender sender) {
String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(npcSetting)); String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(npcSetting));
if (rawValue == null || rawValue.trim().isEmpty() || rawValue.equalsIgnoreCase("null")) { if (InputParsingHelper.isEmpty(rawValue)) {
//Display the default value, if no custom value has been specified //Display the default value, if no custom value has been specified
rawValue = String.valueOf(BlacksmithPlugin.getInstance().getSettings().getRawValue(npcSetting)); rawValue = String.valueOf(BlacksmithPlugin.getInstance().getSettings().getRawValue(npcSetting));
displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue)); displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue));

View File

@ -4,8 +4,8 @@ import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.YamlStorage; import net.citizensnpcs.api.util.YamlStorage;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.util.ConfigHelper; import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import java.io.File; import java.io.File;
@ -112,14 +112,18 @@ public class GlobalSettings {
* @param newEnchantmentCost <p>The new enchantment cost</p> * @param newEnchantmentCost <p>The new enchantment cost</p>
*/ */
public void setEnchantmentCost(Enchantment enchantment, double newEnchantmentCost) { public void setEnchantmentCost(Enchantment enchantment, double newEnchantmentCost) {
if (enchantment == null) {
if (newEnchantmentCost < 0) { if (newEnchantmentCost < 0) {
throw new IllegalArgumentException("Enchantment cost cannot be negative!"); throw new IllegalArgumentException("Enchantment cost cannot be negative!");
} }
if (enchantment == null) {
globalSettings.put(GlobalSetting.ENCHANTMENT_COST, newEnchantmentCost); globalSettings.put(GlobalSetting.ENCHANTMENT_COST, newEnchantmentCost);
} else {
if (newEnchantmentCost < 0) {
enchantmentCosts.put(enchantment, null);
} else { } else {
enchantmentCosts.put(enchantment, newEnchantmentCost); enchantmentCosts.put(enchantment, newEnchantmentCost);
} }
}
save(); save();
} }
@ -130,14 +134,19 @@ public class GlobalSettings {
* @param newPrice <p>The new price per durability point price</p> * @param newPrice <p>The new price per durability point price</p>
*/ */
public void setPricePerDurabilityPoint(Material material, double newPrice) { public void setPricePerDurabilityPoint(Material material, double newPrice) {
if (material == null) {
if (newPrice < 0) { if (newPrice < 0) {
throw new IllegalArgumentException("Price per durability point cannot be negative!"); throw new IllegalArgumentException("Price per durability point cannot be negative!");
} }
if (material == null) {
globalSettings.put(GlobalSetting.PRICE_PER_DURABILITY_POINT, newPrice); globalSettings.put(GlobalSetting.PRICE_PER_DURABILITY_POINT, newPrice);
} else {
//Use a negative price to unset the per-item value
if (newPrice < 0) {
materialPricePerDurabilityPoints.put(material, null);
} else { } else {
materialPricePerDurabilityPoints.put(material, newPrice); materialPricePerDurabilityPoints.put(material, newPrice);
} }
}
save(); save();
} }
@ -148,14 +157,19 @@ public class GlobalSettings {
* @param newBasePrice <p>The new base price</p> * @param newBasePrice <p>The new base price</p>
*/ */
public void setBasePrice(Material material, double newBasePrice) { public void setBasePrice(Material material, double newBasePrice) {
if (material == null) {
if (newBasePrice < 0) { if (newBasePrice < 0) {
throw new IllegalArgumentException("Base price cannot be negative!"); throw new IllegalArgumentException("Base price cannot be negative!");
} }
if (material == null) {
globalSettings.put(GlobalSetting.BASE_PRICE, newBasePrice); globalSettings.put(GlobalSetting.BASE_PRICE, newBasePrice);
} else {
//Use a negative price to unset the per-item value
if (newBasePrice < 0) {
materialBasePrices.put(material, null);
} else { } else {
materialBasePrices.put(material, newBasePrice); materialBasePrices.put(material, newBasePrice);
} }
}
save(); save();
} }
@ -292,7 +306,7 @@ public class GlobalSettings {
Map<String, String> relevantKeys = getRelevantKeys(basePriceNode); Map<String, String> relevantKeys = getRelevantKeys(basePriceNode);
for (String key : relevantKeys.keySet()) { for (String key : relevantKeys.keySet()) {
String materialName = relevantKeys.get(key); String materialName = relevantKeys.get(key);
Material material = Material.matchMaterial(materialName); Material material = InputParsingHelper.matchMaterial(materialName);
if (material != null) { if (material != null) {
materialBasePrices.put(material, basePriceNode.getDouble(key)); materialBasePrices.put(material, basePriceNode.getDouble(key));
} else { } else {
@ -306,7 +320,7 @@ public class GlobalSettings {
relevantKeys = getRelevantKeys(basePerDurabilityPriceNode); relevantKeys = getRelevantKeys(basePerDurabilityPriceNode);
for (String key : relevantKeys.keySet()) { for (String key : relevantKeys.keySet()) {
String materialName = relevantKeys.get(key); String materialName = relevantKeys.get(key);
Material material = Material.matchMaterial(materialName); Material material = InputParsingHelper.matchMaterial(materialName);
if (material != null) { if (material != null) {
materialPricePerDurabilityPoints.put(material, basePerDurabilityPriceNode.getDouble(key)); materialPricePerDurabilityPoints.put(material, basePerDurabilityPriceNode.getDouble(key));
} else { } else {
@ -320,7 +334,7 @@ public class GlobalSettings {
relevantKeys = getRelevantKeys(basePerDurabilityPriceNode); relevantKeys = getRelevantKeys(basePerDurabilityPriceNode);
for (String key : relevantKeys.keySet()) { for (String key : relevantKeys.keySet()) {
String enchantmentName = relevantKeys.get(key); String enchantmentName = relevantKeys.get(key);
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(enchantmentName)); Enchantment enchantment = InputParsingHelper.matchEnchantment(enchantmentName);
if (enchantment != null) { if (enchantment != null) {
enchantmentCosts.put(enchantment, enchantmentCostNode.getDouble(key)); enchantmentCosts.put(enchantment, enchantmentCostNode.getDouble(key));
} else { } else {
@ -407,7 +421,7 @@ public class GlobalSettings {
//Load all enchantment prices //Load all enchantment prices
DataKey enchantmentCostNode = root.getRelative(GlobalSetting.ENCHANTMENT_COST.getParent()); DataKey enchantmentCostNode = root.getRelative(GlobalSetting.ENCHANTMENT_COST.getParent());
for (Enchantment enchantment : enchantmentCosts.keySet()) { for (Enchantment enchantment : enchantmentCosts.keySet()) {
enchantmentCostNode.setRaw(unNormalizeName(enchantment.getKey().toString()), enchantmentCosts.get(enchantment)); enchantmentCostNode.setRaw(unNormalizeName(enchantment.getKey().getKey()), enchantmentCosts.get(enchantment));
} }
//Perform the actual save to disk //Perform the actual save to disk

View File

@ -4,6 +4,7 @@ import net.citizensnpcs.api.util.DataKey;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.util.ConfigHelper; import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -361,10 +362,10 @@ public class NPCSettings {
newReforgeAbleItems = (String) replaceReforgeAblePresets(newReforgeAbleItems); newReforgeAbleItems = (String) replaceReforgeAblePresets(newReforgeAbleItems);
for (String item : newReforgeAbleItems.split(",")) { for (String item : newReforgeAbleItems.split(",")) {
if (item == null || item.trim().isEmpty() || item.equalsIgnoreCase("null")) { if (InputParsingHelper.isEmpty(item)) {
continue; continue;
} }
Material material = Material.matchMaterial(item.replace('-', '_')); Material material = InputParsingHelper.matchMaterial(item);
if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) { if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) {
this.reforgeAbleItems.add(material); this.reforgeAbleItems.add(material);
} else { } else {

View File

@ -4,8 +4,8 @@ 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.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; 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;
@ -57,7 +57,7 @@ public class ReforgeSession implements Runnable {
if (enchantments[0] == null) { if (enchantments[0] == null) {
int i = 0; int i = 0;
for (Enchantment enchantment : Enchantment.values()) { for (Enchantment enchantment : Enchantment.values()) {
enchantments[i++] = enchantment.getKey().toString(); enchantments[i++] = enchantment.getKey().getKey();
} }
} }
} }
@ -140,7 +140,7 @@ public class ReforgeSession implements Runnable {
//Find usable enchantments first //Find usable enchantments first
List<Enchantment> usableEnchantments = new ArrayList<>(); List<Enchantment> usableEnchantments = new ArrayList<>();
for (String enchantmentName : enchantments) { for (String enchantmentName : enchantments) {
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(enchantmentName)); Enchantment enchantment = InputParsingHelper.matchEnchantment(enchantmentName);
if (enchantment != null && enchantment.canEnchantItem(itemToReforge)) { if (enchantment != null && enchantment.canEnchantItem(itemToReforge)) {
usableEnchantments.add(enchantment); usableEnchantments.add(enchantment);
} }

View File

@ -13,6 +13,17 @@ public final class InputParsingHelper {
} }
/**
* Gets whether the input is an "empty" value treated as null
*
* @param input <p>The input to check</p>
* @return <p>True if the value is empty</p>
*/
public static boolean isEmpty(String input) {
return input == null || input.equalsIgnoreCase("null") || input.equals("\"\"") ||
input.trim().isEmpty() || input.equals("-1");
}
/** /**
* Tries to find the material matching the given input string * Tries to find the material matching the given input string
* *

View File

@ -62,7 +62,7 @@ public final class TabCompleteValuesHelper {
private static List<String> getAllEnchantments() { private static List<String> getAllEnchantments() {
List<String> enchantments = new ArrayList<>(); List<String> enchantments = new ArrayList<>();
for (Enchantment enchantment : Enchantment.values()) { for (Enchantment enchantment : Enchantment.values()) {
enchantments.add(enchantment.toString()); enchantments.add(enchantment.getKey().getKey());
} }
return enchantments; return enchantments;
} }