Implements some necessary code for the scrapper
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

Implements ignored salvage
Implements salvage-able items
Adds some TODOs
Implements salvage fail chance
Moves several methods to ItemHelper
Adds option for allowing extended salvage
This commit is contained in:
2023-12-30 13:56:33 +01:00
parent 6872dadca8
commit dfae68050e
13 changed files with 482 additions and 165 deletions

View File

@ -3,21 +3,17 @@ package net.knarcraft.blacksmith.config.blacksmith;
import net.citizensnpcs.api.util.DataKey;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.SmithPreset;
import net.knarcraft.blacksmith.config.TraitSettings;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
/**
@ -185,7 +181,7 @@ public class BlacksmithNPCSettings implements TraitSettings<BlacksmithSetting> {
*
* @return <p>All items reforge-able by this NPC</p>
*/
public List<Material> getReforgeAbleItems() {
public List<Material> getItems() {
Object currentValue = currentValues.get(BlacksmithSetting.REFORGE_ABLE_ITEMS);
if (currentValue == null || String.valueOf(currentValue).isEmpty()) {
return globalBlacksmithSettings.getReforgeAbleItems();
@ -357,28 +353,6 @@ public class BlacksmithNPCSettings implements TraitSettings<BlacksmithSetting> {
return value;
}
/**
* Replaces placeholders in the given reforge-able value
*
* @param stringList <p>The value specified by a user</p>
* @return <p>The value with placeholders replaced</p>
*/
private static List<String> replaceReforgeAblePresets(List<String> stringList) {
List<String> newStrings = new ArrayList<>();
for (String item : stringList) {
if (item == null) {
continue;
}
String replaced = SmithPreset.replacePreset(item);
if (!replaced.equals(item)) {
newStrings.addAll(List.of(replaced.split(",")));
} else {
newStrings.add(item);
}
}
return newStrings;
}
/**
* Updates the list of blocked enchantments
*/
@ -422,55 +396,8 @@ public class BlacksmithNPCSettings implements TraitSettings<BlacksmithSetting> {
this.reforgeAbleItems.clear();
List<String> materialStrings = ConfigHelper.asStringList(getValue(BlacksmithSetting.REFORGE_ABLE_ITEMS));
if (materialStrings != null) {
this.reforgeAbleItems.addAll(getReforgeAbleItems(materialStrings));
this.reforgeAbleItems.addAll(ItemHelper.getItems(materialStrings, true));
}
}
/**
* Gets a list of the reforgeAbleItems described in the given item list
*
* @param itemList <p>The list of items defined by the user</p>
* @return <p>The materials contained in the item list</p>
*/
public static List<Material> getReforgeAbleItems(List<String> itemList) {
List<Material> reforgeAbleItems = new ArrayList<>();
if (itemList == null) {
return null;
}
//Convert any presets with a list of materials
itemList = replaceReforgeAblePresets(itemList);
Set<Material> blacklisted = new HashSet<>();
//Parse every material, and add to reforgeAble items
for (String item : itemList) {
//Ignore ,,
if (InputParsingHelper.isEmpty(item)) {
continue;
}
boolean blacklist = false;
if (item.startsWith("-")) {
blacklist = true;
item = item.substring(1);
}
Material material = InputParsingHelper.matchMaterial(item);
if (material != null && ItemHelper.isRepairable(new ItemStack(material, 1))) {
if (!blacklist) {
reforgeAbleItems.add(material);
} else {
blacklisted.add(material);
}
} else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid reforge-able item");
}
}
//Remove any blacklisted materials at the end to make sure order of arguments won't matter
reforgeAbleItems.removeAll(blacklisted);
return reforgeAbleItems;
}
}

View File

@ -10,6 +10,7 @@ import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.ArrayList;
@ -19,7 +20,7 @@ import java.util.Map;
import java.util.logging.Level;
/**
* A class which keeps track of all default NPC settings and all global settings
* A class which keeps track of all default blacksmith NPC settings and all global blacksmith settings
*/
public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
@ -38,7 +39,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* @param plugin <p>A reference to the blacksmith plugin</p>
*/
public GlobalBlacksmithSettings(BlacksmithPlugin plugin) {
defaultConfig = new YamlStorage(new File(plugin.getDataFolder() + File.separator + "config.yml"),
this.defaultConfig = new YamlStorage(new File(plugin.getDataFolder() + File.separator + "config.yml"),
"Blacksmith Configuration\nWarning: The values under defaults are the values set for a " +
"blacksmith upon creation. To change any values for existing NPCs, edit the citizens NPC file.");
}
@ -48,20 +49,20 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
*/
public void load() {
// Load the config from disk
defaultConfig.load();
DataKey root = defaultConfig.getKey("");
this.defaultConfig.load();
DataKey root = this.defaultConfig.getKey("");
// Just in case, clear existing values
settings.clear();
materialBasePrices.clear();
materialPricePerDurabilityPoints.clear();
enchantmentCosts.clear();
this.settings.clear();
this.materialBasePrices.clear();
this.materialPricePerDurabilityPoints.clear();
this.enchantmentCosts.clear();
// Load/Save settings
loadGlobalSettings(root);
loadSettings(root);
// Save any modified values to disk
defaultConfig.save();
this.defaultConfig.save();
}
/**
@ -74,9 +75,9 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
if (blacksmithSetting.getValueType() == SettingValueType.STRING_LIST ||
blacksmithSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) {
//Workaround to make sure it's treated as the correct type
settings.put(blacksmithSetting, newValue == null ? null : ConfigHelper.asStringList(newValue));
this.settings.put(blacksmithSetting, newValue == null ? null : ConfigHelper.asStringList(newValue));
} else {
settings.put(blacksmithSetting, newValue);
this.settings.put(blacksmithSetting, newValue);
}
save();
if (blacksmithSetting == BlacksmithSetting.REFORGE_ABLE_ITEMS) {
@ -93,7 +94,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(BlacksmithSetting blacksmithSetting) {
return settings.get(blacksmithSetting);
return this.settings.get(blacksmithSetting);
}
/**
@ -107,12 +108,12 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
if (newEnchantmentCost < 0) {
throw new IllegalArgumentException("Enchantment cost cannot be negative!");
}
settings.put(BlacksmithSetting.ENCHANTMENT_COST, newEnchantmentCost);
this.settings.put(BlacksmithSetting.ENCHANTMENT_COST, newEnchantmentCost);
} else {
if (newEnchantmentCost < 0) {
enchantmentCosts.put(enchantment, null);
this.enchantmentCosts.put(enchantment, null);
} else {
enchantmentCosts.put(enchantment, newEnchantmentCost);
this.enchantmentCosts.put(enchantment, newEnchantmentCost);
}
}
save();
@ -129,13 +130,13 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
if (newPrice < 0) {
throw new IllegalArgumentException("Price per durability point cannot be negative!");
}
settings.put(BlacksmithSetting.PRICE_PER_DURABILITY_POINT, newPrice);
this.settings.put(BlacksmithSetting.PRICE_PER_DURABILITY_POINT, newPrice);
} else {
//Use a negative price to unset the per-item value
if (newPrice < 0) {
materialPricePerDurabilityPoints.put(material, null);
this.materialPricePerDurabilityPoints.put(material, null);
} else {
materialPricePerDurabilityPoints.put(material, newPrice);
this.materialPricePerDurabilityPoints.put(material, newPrice);
}
}
save();
@ -156,9 +157,9 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
} else {
//Use a negative price to unset the per-item value
if (newBasePrice < 0) {
materialBasePrices.put(material, null);
this.materialBasePrices.put(material, null);
} else {
materialBasePrices.put(material, newBasePrice);
this.materialBasePrices.put(material, newBasePrice);
}
}
save();
@ -201,8 +202,8 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* @return <p>The base price for the material</p>
*/
public double getBasePrice(Material material) {
if (materialBasePrices.containsKey(material) && materialBasePrices.get(material) != null) {
return materialBasePrices.get(material);
if (this.materialBasePrices.containsKey(material) && this.materialBasePrices.get(material) != null) {
return this.materialBasePrices.get(material);
} else {
return asDouble(BlacksmithSetting.BASE_PRICE);
}
@ -215,9 +216,9 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* @return <p>The durability point price for the material</p>
*/
public double getPricePerDurabilityPoint(Material material) {
if (materialPricePerDurabilityPoints.containsKey(material) &&
materialPricePerDurabilityPoints.get(material) != null) {
return materialPricePerDurabilityPoints.get(material);
if (this.materialPricePerDurabilityPoints.containsKey(material) &&
this.materialPricePerDurabilityPoints.get(material) != null) {
return this.materialPricePerDurabilityPoints.get(material);
} else {
return asDouble(BlacksmithSetting.PRICE_PER_DURABILITY_POINT);
}
@ -230,8 +231,8 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* @return <p>The cost of each enchantment level</p>
*/
public double getEnchantmentCost(Enchantment enchantment) {
if (enchantmentCosts.containsKey(enchantment) && enchantmentCosts.get(enchantment) != null) {
return enchantmentCosts.get(enchantment);
if (this.enchantmentCosts.containsKey(enchantment) && this.enchantmentCosts.get(enchantment) != null) {
return this.enchantmentCosts.get(enchantment);
} else {
return asDouble(BlacksmithSetting.ENCHANTMENT_COST);
}
@ -242,7 +243,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
*
* @return <p>The value of reforgeAbleItems</p>
*/
public List<Material> getReforgeAbleItems() {
public @NotNull List<Material> getReforgeAbleItems() {
return this.defaultReforgeAbleMaterials;
}
@ -251,7 +252,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
*
* @return <p>The value of enchantmentBlocklist</p>
*/
public List<Enchantment> getEnchantmentBlocklist() {
public @NotNull List<Enchantment> getEnchantmentBlocklist() {
return this.defaultEnchantmentBlocklist;
}
@ -302,7 +303,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* @return <p>The current value</p>
*/
private Object getValue(BlacksmithSetting setting) {
Object value = settings.get(setting);
Object value = this.settings.get(setting);
//If not set in config.yml, use the default value from the enum
if (value == null) {
value = setting.getDefaultValue();
@ -315,7 +316,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
*
* @param root <p>The root node of all global settings</p>
*/
private void loadGlobalSettings(DataKey root) {
private void loadSettings(DataKey root) {
for (BlacksmithSetting blacksmithSetting : BlacksmithSetting.values()) {
if (!root.keyExists(blacksmithSetting.getPath())) {
//If the setting does not exist in the config file, add it
@ -340,7 +341,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
for (String key : relevantKeys.keySet()) {
String enchantmentName = relevantKeys.get(key);
Enchantment enchantment = InputParsingHelper.matchEnchantment(enchantmentName);
setItemPrice(enchantmentCosts, enchantmentName, enchantment, enchantmentCostNode.getDouble(key));
setItemPrice(this.enchantmentCosts, enchantmentName, enchantment, enchantmentCostNode.getDouble(key));
}
}
@ -358,10 +359,10 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
double price = basePerDurabilityPriceNode.getDouble(key);
if (materialName.contains("*")) {
//Treat *CHESTPLATE as a regular expression to match all chest-plates
setMatchedMaterialPrices(materialPricePerDurabilityPoints, materialName, price);
setMatchedMaterialPrices(this.materialPricePerDurabilityPoints, materialName, price);
} else {
Material material = InputParsingHelper.matchMaterial(materialName);
setItemPrice(materialPricePerDurabilityPoints, materialName, material, price);
setItemPrice(this.materialPricePerDurabilityPoints, materialName, material, price);
}
}
}
@ -380,10 +381,10 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
double price = basePriceNode.getDouble(key);
if (materialName.contains("*")) {
//Treat *CHESTPLATE as a regular expression to match all chest-plates
setMatchedMaterialPrices(materialBasePrices, materialName, price);
setMatchedMaterialPrices(this.materialBasePrices, materialName, price);
} else {
Material material = InputParsingHelper.matchMaterial(materialName);
setItemPrice(materialBasePrices, materialName, material, price);
setItemPrice(this.materialBasePrices, materialName, material, price);
}
}
}
@ -455,11 +456,11 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* Loads reforgeAble items from the current value
*/
private void loadReforgeAbleItems() {
defaultReforgeAbleMaterials.clear();
this.defaultReforgeAbleMaterials.clear();
List<String> materialNames = ConfigHelper.asStringList(settings.get(
BlacksmithSetting.REFORGE_ABLE_ITEMS));
if (materialNames != null) {
defaultReforgeAbleMaterials.addAll(BlacksmithNPCSettings.getReforgeAbleItems(materialNames));
this.defaultReforgeAbleMaterials.addAll(ItemHelper.getItems(materialNames, true));
}
}
@ -467,11 +468,11 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* Loads the enchantment blocklist from the current value
*/
private void loadEnchantmentBlocklist() {
defaultEnchantmentBlocklist.clear();
this.defaultEnchantmentBlocklist.clear();
List<String> enchantmentNames = ConfigHelper.asStringList(settings.get(
BlacksmithSetting.ENCHANTMENT_BLOCKLIST));
if (enchantmentNames != null) {
defaultEnchantmentBlocklist.addAll(BlacksmithNPCSettings.getEnchantmentBlocklist(enchantmentNames));
this.defaultEnchantmentBlocklist.addAll(BlacksmithNPCSettings.getEnchantmentBlocklist(enchantmentNames));
}
}
@ -479,32 +480,32 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* Saves all current settings to the config file
*/
private void save() {
DataKey root = defaultConfig.getKey("");
DataKey root = this.defaultConfig.getKey("");
//Save all default settings
for (BlacksmithSetting setting : BlacksmithSetting.values()) {
root.setRaw(setting.getPath(), settings.get(setting));
root.setRaw(setting.getPath(), this.settings.get(setting));
}
//Save all base prices
DataKey basePriceNode = root.getRelative(BlacksmithSetting.BASE_PRICE.getPath());
for (Material material : materialBasePrices.keySet()) {
basePriceNode.setRaw(unNormalizeName(material.name()), materialBasePrices.get(material));
for (Material material : this.materialBasePrices.keySet()) {
basePriceNode.setRaw(unNormalizeName(material.name()), this.materialBasePrices.get(material));
}
//Save all per-durability-point prices
DataKey basePerDurabilityPriceNode = root.getRelative(BlacksmithSetting.PRICE_PER_DURABILITY_POINT.getPath());
for (Material material : materialPricePerDurabilityPoints.keySet()) {
basePerDurabilityPriceNode.setRaw(unNormalizeName(material.name()), materialPricePerDurabilityPoints.get(material));
for (Material material : this.materialPricePerDurabilityPoints.keySet()) {
basePerDurabilityPriceNode.setRaw(unNormalizeName(material.name()), this.materialPricePerDurabilityPoints.get(material));
}
//Load all enchantment prices
DataKey enchantmentCostNode = root.getRelative(BlacksmithSetting.ENCHANTMENT_COST.getPath());
for (Enchantment enchantment : enchantmentCosts.keySet()) {
enchantmentCostNode.setRaw(unNormalizeName(enchantment.getKey().getKey()), enchantmentCosts.get(enchantment));
for (Enchantment enchantment : this.enchantmentCosts.keySet()) {
enchantmentCostNode.setRaw(unNormalizeName(enchantment.getKey().getKey()), this.enchantmentCosts.get(enchantment));
}
//Perform the actual save to disk
defaultConfig.save();
this.defaultConfig.save();
}
}

View File

@ -6,14 +6,28 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
/**
* A class which keeps track of all default scrapper NPC settings and all global scrapper settings
*/
public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
private final Map<ScrapperSetting, Object> settings = new HashMap<>();
private final List<Material> defaultSalvageableMaterials = new ArrayList<>();
private final Map<Material, Set<Material>> ignoredSalvage = new HashMap<>();
private final YamlStorage defaultConfig;
@ -23,7 +37,7 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
* @param plugin <p>A reference to the blacksmith plugin</p>
*/
public GlobalScrapperSettings(BlacksmithPlugin plugin) {
defaultConfig = new YamlStorage(new File(plugin.getDataFolder() + File.separator + "config.yml"),
this.defaultConfig = new YamlStorage(new File(plugin.getDataFolder() + File.separator + "config.yml"),
"Scrapper Configuration\nWarning: The values under defaults are the values set for a " +
"scrapper upon creation. To change any values for existing NPCs, edit the citizens NPC file.");
}
@ -33,17 +47,17 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
*/
public void load() {
//Load the config from disk
defaultConfig.load();
DataKey root = defaultConfig.getKey("");
this.defaultConfig.load();
DataKey root = this.defaultConfig.getKey("");
//Just in case, clear existing values
settings.clear();
this.settings.clear();
//Load/Save global settings
loadSettings(root);
//Save any modified values to disk
defaultConfig.save();
this.defaultConfig.save();
}
/**
@ -52,13 +66,16 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
* @param scrapperSetting <p>The default NPC setting to change</p>
* @param newValue <p>The new value for the setting</p>
*/
public void changeValue(ScrapperSetting scrapperSetting, Object newValue) {
public void changeValue(@NotNull ScrapperSetting scrapperSetting, @Nullable Object newValue) {
if (scrapperSetting.getValueType() == SettingValueType.STRING_LIST ||
scrapperSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) {
//Workaround to make sure it's treated as the correct type
settings.put(scrapperSetting, newValue == null ? null : ConfigHelper.asStringList(newValue));
this.settings.put(scrapperSetting, newValue == null ? null : ConfigHelper.asStringList(newValue));
} else {
settings.put(scrapperSetting, newValue);
this.settings.put(scrapperSetting, newValue);
}
if (scrapperSetting == ScrapperSetting.SALVAGE_ABLE_ITEMS) {
loadSalvageAbleItems();
}
save();
}
@ -70,7 +87,7 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(ScrapperSetting scrapperSetting) {
return settings.get(scrapperSetting);
return this.settings.get(scrapperSetting);
}
/**
@ -110,7 +127,7 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
* @return <p>The current value</p>
*/
private Object getValue(ScrapperSetting setting) {
Object value = settings.get(setting);
Object value = this.settings.get(setting);
//If not set in config.yml, use the default value from the enum
if (value == null) {
value = setting.getDefaultValue();
@ -130,23 +147,97 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
root.setRaw(setting.getPath(), setting.getDefaultValue());
} else {
//Set the setting to the value found in the path
settings.put(setting, root.getRaw(setting.getPath()));
this.settings.put(setting, root.getRaw(setting.getPath()));
}
}
loadSalvageAbleItems();
loadIgnoredSalvage();
}
/**
* Saves all current settings to the config file
*/
private void save() {
DataKey root = defaultConfig.getKey("");
DataKey root = this.defaultConfig.getKey("");
//Save all default settings
for (ScrapperSetting setting : ScrapperSetting.values()) {
root.setRaw(setting.getPath(), settings.get(setting));
root.setRaw(setting.getPath(), this.settings.get(setting));
}
//Perform the actual save to disk
defaultConfig.save();
this.defaultConfig.save();
}
/**
* Gets the value of salvageAbleItems
*
* @return <p>The value of salvageAbleItems</p>
*/
public List<Material> getSalvageAbleItems() {
return this.defaultSalvageableMaterials;
}
/**
* Gets ignored salvage for the given material
*
* <p>The ignored salvage should be ignored when calculating item salvage, in order to increase the probability of
* getting the more expensive materials back.</p>
*
* @param material <p>The material to get ignored salvage for</p>
* @return <p>The ignored salvage</p>
*/
public @Nullable Set<Material> getIgnoredSalvage(@NotNull Material material) {
return this.ignoredSalvage.get(material);
}
/**
* Loads reforgeAble items from the current value
*/
private void loadSalvageAbleItems() {
this.defaultSalvageableMaterials.clear();
List<String> materialNames = ConfigHelper.asStringList(this.settings.get(ScrapperSetting.SALVAGE_ABLE_ITEMS));
if (materialNames != null) {
this.defaultSalvageableMaterials.addAll(ItemHelper.getItems(materialNames, true));
}
}
/**
* Loads all ignored salvage from the configuration file
*/
private void loadIgnoredSalvage() {
this.ignoredSalvage.clear();
List<String> allIgnoredSalvage = ConfigHelper.asStringList(this.settings.get(ScrapperSetting.IGNORED_SALVAGE));
if (allIgnoredSalvage == null) {
return;
}
for (String ignoredSalvageInfo : allIgnoredSalvage) {
// Ignore invalid lines
if (!ignoredSalvageInfo.contains(":")) {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, String.format("The ignored salvage " +
"configuration line %s is invalid", ignoredSalvageInfo));
continue;
}
// Parse all material names
String[] data = ignoredSalvageInfo.split(":");
String[] materialStrings = data[0].split(",");
List<Material> materials = new ArrayList<>();
for (String materialString : materialStrings) {
materials.addAll(ItemHelper.getWildcardMatch(materialString));
}
String[] ignoredSalvageStrings = data[1].split(",");
List<Material> ignored = new ArrayList<>();
for (String ignoredSalvageString : ignoredSalvageStrings) {
ignored.addAll(ItemHelper.getWildcardMatch(ignoredSalvageString));
}
// Add the ignored salvage to all the matched materials
for (Material material : materials) {
ignoredSalvage.computeIfAbsent(material, k -> new HashSet<>());
ignoredSalvage.get(material).addAll(ignored);
}
}
}
}

View File

@ -4,13 +4,19 @@ import net.citizensnpcs.api.util.DataKey;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.TraitSettings;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
private final Map<ScrapperSetting, Object> currentValues = new HashMap<>();
private final GlobalScrapperSettings globalScrapperSettings;
private final List<Material> salvageAbleItems = new ArrayList<>();
/**
* Instantiates a new scrapper NPC settings object
@ -59,6 +65,20 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
} else {
currentValues.put(setting, newValue);
}
if (setting == ScrapperSetting.SALVAGE_ABLE_ITEMS) {
updateSalvageAbleItems();
}
}
/**
* Updates the reforge-able items according to the current value of the setting
*/
private void updateSalvageAbleItems() {
this.salvageAbleItems.clear();
List<String> materialStrings = ConfigHelper.asStringList(getValue(ScrapperSetting.SALVAGE_ABLE_ITEMS));
if (materialStrings != null) {
this.salvageAbleItems.addAll(ItemHelper.getItems(materialStrings, !extendedSalvageEnabled()));
}
}
/**
@ -87,14 +107,23 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
}
/**
* Gets the message to display when a blacksmith has successfully repaired an item
* Gets the message to display when a scrapper has successfully scrapped an item
*
* @return <p>The reforge success message</p>
* @return <p>The salvage success message</p>
*/
public String getSuccessMessage() {
return asString(ScrapperSetting.SUCCESS_SALVAGE_MESSAGE);
}
/**
* Gets the message to display when a scrapper fails to salvage an item
*
* @return <p>The salvage fail message</p>
*/
public String getFailMessage() {
return asString(ScrapperSetting.FAIL_SALVAGE_MESSAGE);
}
@Override
public String getCoolDownUnexpiredMessage() {
return asString(ScrapperSetting.COOL_DOWN_UNEXPIRED_MESSAGE);
@ -157,6 +186,15 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
return asInt(ScrapperSetting.MAX_SALVAGE_DELAY) <= 0;
}
/**
* Gets the chance of a salvaging to fail
*
* @return <p>The chance of a salvaging to fail</p>
*/
public int getFailChance() {
return asInt(ScrapperSetting.FAIL_SALVAGE_CHANCE);
}
/**
* Gets the given value as an integer
*
@ -201,4 +239,51 @@ public class ScrapperNPCSettings implements TraitSettings<ScrapperSetting> {
return value;
}
/**
* Gets all item types this NPC is able to salvage
*
* @return <p>All salvageable items</p>
*/
public List<Material> getSalvageAbleItems() {
Object currentValue = currentValues.get(ScrapperSetting.SALVAGE_ABLE_ITEMS);
if (currentValue == null || String.valueOf(currentValue).isEmpty()) {
return globalScrapperSettings.getSalvageAbleItems();
} else {
return new ArrayList<>(this.salvageAbleItems);
}
}
/**
* Whether extended salvaging is enabled
*
* <p>When not extended, only repairable items can be salvaged. When extended, any item produced using a crafting
* recipe or a smithing transformation can be salvaged. This does not include smelting or similar.</p>
*
* @return <p>True if extended salvaging is enabled</p>
*/
public boolean extendedSalvageEnabled() {
return ConfigHelper.asBoolean(getValue(ScrapperSetting.EXTENDED_SALVAGE_ENABLED));
}
/**
* Gets the title of this scrapper NPC
*
* <p>The title is used to specify scrapper sub-types in order to make it clear which items a scrapper can salvage.
* For example, an armor-scrapper would only be able to salvage armor pieces.</p>
*
* @return <p>The title of this scrapper NPC</p>
*/
public String getScrapperTitle() {
return asString(ScrapperSetting.SCRAPPER_TITLE);
}
/**
* Gets the message to display if this NPC is given an item it cannot salvage
*
* @return <p>The message to display</p>
*/
public String getInvalidItemMessage() {
return asString(ScrapperSetting.INVALID_ITEM_MESSAGE);
}
}

View File

@ -5,6 +5,9 @@ import net.knarcraft.blacksmith.config.SettingValueType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* An enum representing all scrapper-related settings
*/
@ -63,6 +66,12 @@ public enum ScrapperSetting implements Setting {
*/
SCRAPPER_TITLE("scrapperTitle", SettingValueType.STRING, "scrapper",
"The title describing the scrapper's usage/speciality", true, false),
/**
* The setting for whether the NPC should allow salvaging of any craft-able item instead of just repairable items
*/
EXTENDED_SALVAGE_ENABLED("extendedSalvageEnabled", SettingValueType.BOOLEAN, false,
"Whether to enable salvaging of non-repairable items, such as planks", true, false),
/*-----------
| Messages |
@ -91,11 +100,12 @@ public enum ScrapperSetting implements Setting {
"on a cool-down from the previous re-forging", true, true),
/**
* The message displayed if presented with an item that cannot be salvaged by the NPC
* The message displayed if the scrapper encounters an item they cannot salvage
*/
CANNOT_SALVAGE_MESSAGE("cannotSalvageMessage", SettingValueType.STRING,
"&cI'm unable to salvage that item", "The message to display if the player tries to salvage" +
" an item the blacksmith cannot salvage", true, true),
INVALID_ITEM_MESSAGE("invalidItemMessage", SettingValueType.STRING,
"&cI'm sorry, but I'm a/an {title}, I don't know how to salvage that!",
"The message to display if the player tries to salvage" +
" an item the scrapper cannot salvage", true, true),
/**
* The message displayed if salvaging an item would return in no items
@ -146,6 +156,17 @@ public enum ScrapperSetting implements Setting {
*/
GIVE_EXPERIENCE("giveExperience", SettingValueType.BOOLEAN, "true", "Whether enchanted " +
"salvaged items should return some amount of exp upon salvage", false, false),
/**
* Which items are ignored when calculating salvage for a given material
*/
IGNORED_SALVAGE("ignoredSalvage", SettingValueType.STRING_LIST,
new ArrayList<>(List.of("*_SHOVEL,*_PICKAXE,*_AXE,*_HOE,*_SWORD:STICK")),
"Items ignored during salvage calculation. This follows the format: " +
"\"MATERIAL[,MATERIAL2][,MATERIAL3]:IGNORED\", so the material or materials listed will ignore " +
"the material specified after the \":\" when calculating salvage (* matches any character). This " +
"causes the player to lose some items during salvaging, but can prevent cases where a diamond " +
"pickaxe is salvaged and only sticks are returned.", false, false),
;
private final String path;