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

@ -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;