Implements the scrapper global edit command
Changes setting quite a bit to avoid code duplication
This commit is contained in:
@ -2,17 +2,15 @@ package net.knarcraft.blacksmith.config.scrapper;
|
||||
|
||||
import net.citizensnpcs.api.util.DataKey;
|
||||
import net.knarcraft.blacksmith.config.SettingValueType;
|
||||
import net.knarcraft.blacksmith.config.SmithPreset;
|
||||
import net.knarcraft.blacksmith.config.Settings;
|
||||
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<>();
|
||||
public class ScrapperNPCSettings implements TraitSettings, Settings<ScrapperSetting> {
|
||||
private final Map<ScrapperSetting, Object> currentValues = new HashMap<>();
|
||||
private final GlobalScrapperSettings globalScrapperSettings;
|
||||
|
||||
/**
|
||||
@ -30,7 +28,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @param key <p>The data key to load variables from</p>
|
||||
*/
|
||||
public void loadVariables(DataKey key) {
|
||||
for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) {
|
||||
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
||||
if (key.keyExists(setting.getChildPath())) {
|
||||
currentValues.put(setting, key.getRaw(setting.getChildPath()));
|
||||
}
|
||||
@ -43,7 +41,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @param key <p>The data key to save variables to</p>
|
||||
*/
|
||||
public void saveVariables(DataKey key) {
|
||||
for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) {
|
||||
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
||||
key.setRaw(setting.getChildPath(), currentValues.get(setting));
|
||||
}
|
||||
}
|
||||
@ -54,7 +52,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @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) {
|
||||
public void changeValue(ScrapperSetting 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
|
||||
@ -70,23 +68,23 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @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) {
|
||||
public Object getRawValue(ScrapperSetting setting) {
|
||||
return currentValues.get(setting);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBusyWithPlayerMessage() {
|
||||
return asString(ScrapperNPCSetting.BUSY_WITH_PLAYER_MESSAGE);
|
||||
return asString(ScrapperSetting.BUSY_WITH_PLAYER_MESSAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBusyWorkingMessage() {
|
||||
return asString(ScrapperNPCSetting.BUSY_WITH_SALVAGE_MESSAGE);
|
||||
return asString(ScrapperSetting.BUSY_WITH_SALVAGE_MESSAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStartWorkingMessage() {
|
||||
return asString(ScrapperNPCSetting.START_SALVAGE_MESSAGE);
|
||||
return asString(ScrapperSetting.START_SALVAGE_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,19 +93,19 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @return <p>The reforge success message</p>
|
||||
*/
|
||||
public String getSuccessMessage() {
|
||||
return asString(ScrapperNPCSetting.SUCCESS_SALVAGE_MESSAGE);
|
||||
return asString(ScrapperSetting.SUCCESS_SALVAGE_MESSAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCoolDownUnexpiredMessage() {
|
||||
return asString(ScrapperNPCSetting.COOL_DOWN_UNEXPIRED_MESSAGE);
|
||||
return asString(ScrapperSetting.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);
|
||||
return asString(ScrapperSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,7 +114,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @return <p>The minimum salvage delay</p>
|
||||
*/
|
||||
public int getMinSalvageDelay() {
|
||||
return asInt(ScrapperNPCSetting.MIN_SALVAGE_DELAY);
|
||||
return asInt(ScrapperSetting.MIN_SALVAGE_DELAY);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,7 +123,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @return <p>The maximum salvage delay</p>
|
||||
*/
|
||||
public int getMaxSalvageDelay() {
|
||||
return asInt(ScrapperNPCSetting.MAX_SALVAGE_DELAY);
|
||||
return asInt(ScrapperSetting.MAX_SALVAGE_DELAY);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +132,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @return <p>The salvage cool-down</p>
|
||||
*/
|
||||
public int getSalvageCoolDown() {
|
||||
return asInt(ScrapperNPCSetting.SALVAGE_COOL_DOWN);
|
||||
return asInt(ScrapperSetting.SALVAGE_COOL_DOWN);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -143,12 +141,12 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @return <p>Whether to drop reforged items on the ground</p>
|
||||
*/
|
||||
public boolean getDropItem() {
|
||||
return asBoolean(ScrapperNPCSetting.DROP_ITEM);
|
||||
return asBoolean(ScrapperSetting.DROP_ITEM);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDisableCoolDown() {
|
||||
return asInt(ScrapperNPCSetting.SALVAGE_COOL_DOWN) <= 0;
|
||||
return asInt(ScrapperSetting.SALVAGE_COOL_DOWN) <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -157,7 +155,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @return <p>Whether to disable the reforge delay</p>
|
||||
*/
|
||||
public boolean getDisableDelay() {
|
||||
return asInt(ScrapperNPCSetting.MAX_SALVAGE_DELAY) <= 0;
|
||||
return asInt(ScrapperSetting.MAX_SALVAGE_DELAY) <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -168,7 +166,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @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) {
|
||||
private int asInt(ScrapperSetting setting) {
|
||||
return ConfigHelper.asInt(getValue(setting));
|
||||
}
|
||||
|
||||
@ -178,7 +176,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @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) {
|
||||
private String asString(ScrapperSetting setting) {
|
||||
return getValue(setting).toString();
|
||||
}
|
||||
|
||||
@ -188,7 +186,7 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @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) {
|
||||
private boolean asBoolean(ScrapperSetting setting) {
|
||||
return ConfigHelper.asBoolean(getValue(setting));
|
||||
}
|
||||
|
||||
@ -198,11 +196,11 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
* @param setting <p>The setting to get the value of</p>
|
||||
* @return <p>The current value</p>
|
||||
*/
|
||||
private Object getValue(ScrapperNPCSetting setting) {
|
||||
private Object getValue(ScrapperSetting 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();
|
||||
Map<ScrapperSetting, Object> defaultNPCSettings = globalScrapperSettings.getDefaultNPCSettings();
|
||||
if (defaultNPCSettings.containsKey(setting)) {
|
||||
value = defaultNPCSettings.get(setting);
|
||||
}
|
||||
@ -214,26 +212,4 @@ public class ScrapperNPCSettings implements TraitSettings {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user