Writes a lot of code necessary for the scrapper

Adds the classes necessary for the new scrapper
Partly implements some scrapper functionality
Restructures some classes to reduce code duplication
Moves some classes to make some classes easier to find
Adds a bunch of TODOs where things have an unfinished implementation
This commit is contained in:
2023-11-14 16:04:48 +01:00
parent 1938a690c6
commit f3f3f66c38
40 changed files with 1788 additions and 594 deletions

View File

@ -0,0 +1,75 @@
package net.knarcraft.blacksmith.config.scrapper;
import net.knarcraft.blacksmith.config.GlobalSetting;
import net.knarcraft.blacksmith.config.SettingValueType;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
/**
* Settings which are the same for every scrapper
*/
public enum GlobalScrapperSetting implements GlobalSetting {
/**
* Whether to display exact time in minutes and seconds when displaying a remaining cool-down
*/
SHOW_EXACT_TIME("scrapper.global.showExactTime", SettingValueType.BOOLEAN, "false",
"showExactTime"),
/**
* Whether to give experience back when salvaging an enchanted item
*/
GIVE_EXPERIENCE("scrapper.global.giveExperience", SettingValueType.BOOLEAN, "true",
"giveExperience"),
;
private final String path;
private final String parent;
private final String commandName;
private final Object value;
private final SettingValueType valueType;
/**
* Instantiates a new setting
*
* @param path <p>The full config path for this setting</p>
* @param valueType <p>The type of value used by this setting</p>
* @param value <p>The default value of this setting</p>
* @param commandName <p>The name of the command used to change this setting</p>
*/
GlobalScrapperSetting(String path, SettingValueType valueType, Object value, String commandName) {
this.path = path;
this.value = value;
this.commandName = commandName;
this.valueType = valueType;
String[] pathParts = path.split("\\.");
this.parent = String.join(".", Arrays.copyOfRange(pathParts, 0, pathParts.length - 1));
}
@Override
public @NotNull String getPath() {
return path;
}
@Override
public @NotNull String getParent() {
return parent;
}
@Override
public @NotNull Object getDefaultValue() {
return value;
}
@Override
public @NotNull String getCommandName() {
return commandName;
}
@Override
public @NotNull SettingValueType getValueType() {
return this.valueType;
}
}

View File

@ -0,0 +1,211 @@
package net.knarcraft.blacksmith.config.scrapper;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.YamlStorage;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.util.ConfigHelper;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class GlobalScrapperSettings {
private final Map<ScrapperNPCSetting, Object> defaultNPCSettings = new HashMap<>();
private final Map<GlobalScrapperSetting, Object> globalSettings = new HashMap<>();
private final YamlStorage defaultConfig;
/**
* Instantiates a new "Settings"
*
* @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"),
"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.");
}
/**
* Loads all configuration values from the config file
*/
public void load() {
//Load the config from disk
defaultConfig.load();
DataKey root = defaultConfig.getKey("");
//Just in case, clear existing values
defaultNPCSettings.clear();
globalSettings.clear();
//Load/Save NPC default settings
loadDefaultNPCSettings(root);
//Load/Save global settings
loadGlobalSettings(root);
//Save any modified values to disk
defaultConfig.save();
}
/**
* Changes the value of the given setting
*
* @param globalScrapperSetting <p>The global setting to change</p>
* @param newValue <p>The new value of the setting</p>
*/
public void changeValue(GlobalScrapperSetting globalScrapperSetting, Object newValue) {
globalSettings.put(globalScrapperSetting, newValue);
save();
}
/**
* Changes the value of the given setting
*
* @param scrapperNPCSetting <p>The default NPC setting to change</p>
* @param newValue <p>The new value for the setting</p>
*/
public void changeValue(ScrapperNPCSetting scrapperNPCSetting, Object newValue) {
if (scrapperNPCSetting.getValueType() == SettingValueType.STRING_LIST ||
scrapperNPCSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) {
//Workaround to make sure it's treated as the correct type
defaultNPCSettings.put(scrapperNPCSetting, newValue == null ? null : ConfigHelper.asStringList(newValue));
} else {
defaultNPCSettings.put(scrapperNPCSetting, newValue);
}
save();
}
/**
* Gets the current raw value of the given global setting
*
* @param globalBlacksmithSetting <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(GlobalScrapperSetting globalBlacksmithSetting) {
return globalSettings.get(globalBlacksmithSetting);
}
/**
* Gets the current raw value of the given default NPC setting
*
* @param scrapperNPCSetting <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(ScrapperNPCSetting scrapperNPCSetting) {
return defaultNPCSettings.get(scrapperNPCSetting);
}
/**
* Gets the current value of the default NPC settings
*
* @return <p>The current value of the default NPC settings</p>
*/
public Map<ScrapperNPCSetting, Object> getDefaultNPCSettings() {
return new HashMap<>(this.defaultNPCSettings);
}
/**
* Gets whether to show exact time for reforging wait-time, and for wait-time between sessions
*
* @return <p>Whether to show exact time</p>
*/
public boolean getShowExactTime() {
return asBoolean(GlobalScrapperSetting.SHOW_EXACT_TIME);
}
/**
* Gets the given value as a boolean
*
* <p>This will throw an exception if used for a non-boolean value</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a boolean</p>
*/
public boolean asBoolean(GlobalScrapperSetting setting) {
return ConfigHelper.asBoolean(getValue(setting));
}
/**
* Gets the given value as a double
*
* <p>This will throw an exception if used for a non-double setting</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a double</p>
*/
public double asDouble(GlobalScrapperSetting setting) {
return ConfigHelper.asDouble(getValue(setting));
}
/**
* Gets the value of a setting, using the default if not set
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The current value</p>
*/
private Object getValue(GlobalScrapperSetting setting) {
Object value = globalSettings.get(setting);
//If not set in config.yml, use the default value from the enum
if (value == null) {
value = setting.getDefaultValue();
}
return value;
}
/**
* Loads all global settings
*
* @param root <p>The root node of all global settings</p>
*/
private void loadGlobalSettings(DataKey root) {
for (GlobalScrapperSetting globalScrapperSetting : GlobalScrapperSetting.values()) {
if (!root.keyExists(globalScrapperSetting.getPath())) {
//If the setting does not exist in the config file, add it
root.setRaw(globalScrapperSetting.getPath(), globalScrapperSetting.getDefaultValue());
} else {
//Set the setting to the value found in the path
globalSettings.put(globalScrapperSetting, root.getRaw(globalScrapperSetting.getPath()));
}
}
}
/**
* Loads all default NPC settings
*
* @param root <p>The root node of all default NPC settings</p>
*/
private void loadDefaultNPCSettings(DataKey root) {
for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) {
if (!root.keyExists(setting.getPath())) {
//If the setting does not exist in the config file, add it
root.setRaw(setting.getPath(), setting.getDefaultValue());
} else {
//Set the setting to the value found in the path
defaultNPCSettings.put(setting, root.getRaw(setting.getPath()));
}
}
}
/**
* Saves all current settings to the config file
*/
private void save() {
DataKey root = defaultConfig.getKey("");
//Save all default NPC settings
for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) {
root.setRaw(setting.getPath(), defaultNPCSettings.get(setting));
}
//Save all normal global settings
for (GlobalScrapperSetting globalBlacksmithSetting : GlobalScrapperSetting.values()) {
root.setRaw(globalBlacksmithSetting.getPath(), globalSettings.get(globalBlacksmithSetting));
}
//Perform the actual save to disk
defaultConfig.save();
}
}

View File

@ -2,6 +2,7 @@ package net.knarcraft.blacksmith.config.scrapper;
import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.config.SettingValueType;
import org.jetbrains.annotations.NotNull;
public enum ScrapperNPCSetting implements NPCSetting {
@ -21,6 +22,21 @@ public enum ScrapperNPCSetting implements NPCSetting {
* The setting for which items a scrapper is able to salvage
*/
SALVAGE_ABLE_ITEMS("salvageAbleItems", SettingValueType.REFORGE_ABLE_ITEMS, "", "salvageAbleItems"),
/**
* The maximum amount of seconds a player may need to wait for the reforging to finish
*/
MAX_SALVAGE_DELAY("delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, "maxReforgeDelay"),
/**
* The minimum amount of seconds a player may need to wait for the reforging to finish
*/
MIN_SALVAGE_DELAY("delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, "minReforgeDelay"),
/**
* The setting for number of seconds a player has to wait between each usage of the blacksmith
*/
SALVAGE_COOL_DOWN("delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, "reforgeCoolDown"),
/*-----------
| Messages |
@ -31,6 +47,50 @@ public enum ScrapperNPCSetting implements NPCSetting {
*/
BUSY_WITH_PLAYER_MESSAGE("messages.busyPlayerMessage", SettingValueType.STRING,
"&cI'm busy at the moment. Come back later!", "busyPlayerMessage"),
/**
* The message displayed when the scrapper is busy salvaging the player's item
*/
BUSY_WITH_SALVAGE_MESSAGE("messages.busySalvageMessage", SettingValueType.STRING,
"&cI'm working on it. Be patient! I'll finish {time}!", "busySalvageMessage"),
/**
* The message displayed if the player needs to wait for the cool-down to expire
*/
COOL_DOWN_UNEXPIRED_MESSAGE("messages.coolDownUnexpiredMessage", SettingValueType.STRING,
"&cYou've already had your chance! Give me a break! I'll be ready {time}!",
"coolDownUnexpiredMessage"),
/**
* The message displayed if presented with an item that cannot be salvaged by the NPC
*/
CANNOT_SALVAGE_MESSAGE("messages.cannotSalvageMessage", SettingValueType.STRING,
"&cI'm unable to salvage that item", "cannotSalvageMessage"),
/**
* The message displayed if salvaging an item would return in no items
*/
TOO_DAMAGED_FOR_SALVAGE_MESSAGE("messages.tooDamagedForSalvageMessage", SettingValueType.STRING,
"&cThat item is too damaged to be salvaged into anything useful",
"tooDamagedForSalvageMessage"),
/**
* The message displayed if a salvage is successful
*/
SUCCESS_SALVAGE_MESSAGE("messages.successSalvagedMessage", SettingValueType.STRING, "&cThere you go!",
"successSalvagedMessage"),
/**
* The message displayed if a player presents a different item after seeing the price to reforge an item
*/
ITEM_UNEXPECTEDLY_CHANGED_MESSAGE("messages.itemChangedMessage", SettingValueType.STRING,
"&cThat's not the item you wanted to reforge before!", "itemChangedMessage"),
/**
* The message displayed when the scrapper starts salvaging an item
*/
START_SALVAGE_MESSAGE("messages.startSalvageMessage", SettingValueType.STRING,
"&eOk, let's see what I can do...", "startSalvageMessage"),
;
private final String path;
@ -48,7 +108,7 @@ public enum ScrapperNPCSetting implements NPCSetting {
* @param commandName <p>The name of the command used to change this setting</p>
*/
ScrapperNPCSetting(String path, SettingValueType valueType, Object value, String commandName) {
this.path = "defaults." + path;
this.path = "scrapper.defaults." + path;
this.value = value;
this.valueType = valueType;
this.childPath = path;
@ -56,28 +116,28 @@ public enum ScrapperNPCSetting implements NPCSetting {
}
@Override
public String getPath() {
public @NotNull String getPath() {
return path;
}
@Override
public String getChildPath() {
public @NotNull String getChildPath() {
return childPath;
}
@Override
public Object getDefaultValue() {
public @NotNull Object getDefaultValue() {
return value;
}
@Override
public String getCommandName() {
public @NotNull String getCommandName() {
return commandName;
}
@Override
public SettingValueType getValueType() {
public @NotNull SettingValueType getValueType() {
return this.valueType;
}
}

View File

@ -1,4 +1,239 @@
package net.knarcraft.blacksmith.config.scrapper;
public class ScrapperNPCSettings {
import net.citizensnpcs.api.util.DataKey;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ScrapperNPCSettings implements TraitSettings {
private final Map<ScrapperNPCSetting, Object> currentValues = new HashMap<>();
private final GlobalScrapperSettings globalScrapperSettings;
/**
* Instantiates a new scrapper NPC settings object
*
* @param globalScrapperSettings <p>The global settings object to get default values from</p>
*/
public ScrapperNPCSettings(GlobalScrapperSettings globalScrapperSettings) {
this.globalScrapperSettings = globalScrapperSettings;
}
/**
* Loads variables from the given data key
*
* @param key <p>The data key to load variables from</p>
*/
public void loadVariables(DataKey key) {
for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) {
if (key.keyExists(setting.getChildPath())) {
currentValues.put(setting, key.getRaw(setting.getChildPath()));
}
}
}
/**
* Saves variables to the given data key
*
* @param key <p>The data key to save variables to</p>
*/
public void saveVariables(DataKey key) {
for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) {
key.setRaw(setting.getChildPath(), currentValues.get(setting));
}
}
/**
* Changes one setting to the given value
*
* @param setting <p>The setting to change</p>
* @param newValue <p>The new value of the setting</p>
*/
public void changeSetting(ScrapperNPCSetting setting, Object newValue) {
if (setting.getValueType() == SettingValueType.STRING_LIST ||
setting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) {
//Workaround to make sure it's treated as the correct type
currentValues.put(setting, newValue == null ? null : ConfigHelper.asStringList(newValue));
} else {
currentValues.put(setting, newValue);
}
}
/**
* Gets the raw current value of a setting
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The current value of the setting</p>
*/
public Object getRawValue(ScrapperNPCSetting setting) {
return currentValues.get(setting);
}
@Override
public String getBusyWithPlayerMessage() {
return asString(ScrapperNPCSetting.BUSY_WITH_PLAYER_MESSAGE);
}
@Override
public String getBusyWorkingMessage() {
return asString(ScrapperNPCSetting.BUSY_WITH_SALVAGE_MESSAGE);
}
@Override
public String getStartWorkingMessage() {
return asString(ScrapperNPCSetting.START_SALVAGE_MESSAGE);
}
/**
* Gets the message to display when a blacksmith has successfully repaired an item
*
* @return <p>The reforge success message</p>
*/
public String getSuccessMessage() {
return asString(ScrapperNPCSetting.SUCCESS_SALVAGE_MESSAGE);
}
@Override
public String getCoolDownUnexpiredMessage() {
return asString(ScrapperNPCSetting.COOL_DOWN_UNEXPIRED_MESSAGE);
}
/**
* The message displayed if a player presents a different item after seeing the price to salvage an item
*/
public String getItemChangedMessage() {
return asString(ScrapperNPCSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE);
}
/**
* Gets the minimum delay used to wait for a salvaging to finish.
*
* @return <p>The minimum salvage delay</p>
*/
public int getMinSalvageDelay() {
return asInt(ScrapperNPCSetting.MIN_SALVAGE_DELAY);
}
/**
* Gets the maximum delay used to wait for a salvaging to finish
*
* @return <p>The maximum salvage delay</p>
*/
public int getMaxSalvageDelay() {
return asInt(ScrapperNPCSetting.MAX_SALVAGE_DELAY);
}
/**
* Gets the cool-down between each salvaging
*
* @return <p>The salvage cool-down</p>
*/
public int getSalvageCoolDown() {
return asInt(ScrapperNPCSetting.SALVAGE_COOL_DOWN);
}
/**
* Gets whether an item should be dropped on the ground instead of being given to the player
*
* @return <p>Whether to drop reforged items on the ground</p>
*/
public boolean getDropItem() {
return asBoolean(ScrapperNPCSetting.DROP_ITEM);
}
@Override
public boolean getDisableCoolDown() {
return asInt(ScrapperNPCSetting.SALVAGE_COOL_DOWN) <= 0;
}
/**
* Gets whether to disable the delay between starting reforging and the reforging finishing
*
* @return <p>Whether to disable the reforge delay</p>
*/
public boolean getDisableDelay() {
return asInt(ScrapperNPCSetting.MAX_SALVAGE_DELAY) <= 0;
}
/**
* Gets the given value as an integer
*
* <p>This will throw an exception if used for a non-integer setting</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as an integer</p>
*/
private int asInt(ScrapperNPCSetting setting) {
return ConfigHelper.asInt(getValue(setting));
}
/**
* Gets the string value of the given setting
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a string</p>
*/
private String asString(ScrapperNPCSetting setting) {
return getValue(setting).toString();
}
/**
* Gets the boolean value of the given setting
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a boolean</p>
*/
private boolean asBoolean(ScrapperNPCSetting setting) {
return ConfigHelper.asBoolean(getValue(setting));
}
/**
* Gets the value of a setting, using the default if not set
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The current value</p>
*/
private Object getValue(ScrapperNPCSetting setting) {
Object value = currentValues.get(setting);
//If not set, use the default value from the config.yml file
if (value == null) {
Map<ScrapperNPCSetting, Object> defaultNPCSettings = globalScrapperSettings.getDefaultNPCSettings();
if (defaultNPCSettings.containsKey(setting)) {
value = defaultNPCSettings.get(setting);
}
}
//If not set in config.yml, use the default value from the enum
if (value == null) {
value = setting.getDefaultValue();
}
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;
}
}