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:
2022-10-24 13:57:58 +02:00
parent 39e164c9c8
commit cc7d66f270
8 changed files with 102 additions and 35 deletions

View File

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

View File

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