diff --git a/defaultconfigs/1.18/config.yml b/defaultconfigs/1.18/config.yml index dd24558..10d6ef9 100644 --- a/defaultconfigs/1.18/config.yml +++ b/defaultconfigs/1.18/config.yml @@ -72,18 +72,6 @@ permission_needed_for_color: true # Valid values include 0 to 1000. use_of_color_cost: 0 -# Work penalty increase the price for every anvil use. -# This config allow you to choose the comportment of work penalty. -# Vanilla work penalty formula can be represented as 2 * previous_penalty + 1. with start value equal to 0 -# See https://minecraft.wiki/w/Anvil_mechanics#Anvil_uses for more detail -# -# Valid work penalty type is: -# - default: work penalty added and increased -# - increase_only: work penalty increased but not added -# - add_only: work penalty added but not increased -# - disabled: work penalty disabled -work_penalty_type: default - # Default limit to apply to any enchants missing from enchant_limits # # Valid values include 1 to 1000 @@ -294,4 +282,4 @@ debug_log_verbose: false # ProtocoLib may also be used if the server is in an "unsupported" version even if this option is disabled. force_protocolib: false -configVersion: 1.6.2 +configVersion: 1.8.0 diff --git a/defaultconfigs/1.21/config.yml b/defaultconfigs/1.21/config.yml index dd24558..10d6ef9 100644 --- a/defaultconfigs/1.21/config.yml +++ b/defaultconfigs/1.21/config.yml @@ -72,18 +72,6 @@ permission_needed_for_color: true # Valid values include 0 to 1000. use_of_color_cost: 0 -# Work penalty increase the price for every anvil use. -# This config allow you to choose the comportment of work penalty. -# Vanilla work penalty formula can be represented as 2 * previous_penalty + 1. with start value equal to 0 -# See https://minecraft.wiki/w/Anvil_mechanics#Anvil_uses for more detail -# -# Valid work penalty type is: -# - default: work penalty added and increased -# - increase_only: work penalty increased but not added -# - add_only: work penalty added but not increased -# - disabled: work penalty disabled -work_penalty_type: default - # Default limit to apply to any enchants missing from enchant_limits # # Valid values include 1 to 1000 @@ -294,4 +282,4 @@ debug_log_verbose: false # ProtocoLib may also be used if the server is in an "unsupported" version even if this option is disabled. force_protocolib: false -configVersion: 1.6.2 +configVersion: 1.8.0 diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/WorkPenaltyTypeSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/WorkPenaltyTypeSettingGui.java index 6a3bb30..eda30d1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/WorkPenaltyTypeSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/WorkPenaltyTypeSettingGui.java @@ -156,11 +156,15 @@ public class WorkPenaltyTypeSettingGui extends AbstractSettingGui { @Override public boolean onSave() { + return saveWorkPenalty(items); + } + + public static boolean saveWorkPenalty(Map partEnum) { String path = ConfigOptions.WORK_PENALTY_ROOT; ConfigHolder configHolder = ConfigHolder.DEFAULT_CONFIG; FileConfiguration config = configHolder.getConfig(); - items.forEach((key, value) -> { + partEnum.forEach((key, value) -> { String partPath = path + "." + key.getTypeName(); if (key.getDefaultPenalty().equals(value)) { @@ -172,8 +176,7 @@ public class WorkPenaltyTypeSettingGui extends AbstractSettingGui { config.set(partPath + '.' + ConfigOptions.WORK_PENALTY_ADDITIVE, value.penaltyAdditive()); }); - configHolder.saveToDisk(true); - return true; + return configHolder.saveToDisk(true); } @Override diff --git a/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_6_7.java b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_6_7.java index f77bced..ee544dc 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_6_7.java +++ b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_6_7.java @@ -20,8 +20,6 @@ public class PUpdate_1_6_7 { toSave.add(ConfigHolder.DEFAULT_CONFIG); } - - } } diff --git a/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_8_0.java b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_8_0.java new file mode 100644 index 0000000..0f396a0 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_8_0.java @@ -0,0 +1,64 @@ +package xyz.alexcrea.cuanvil.update.plugin; + +import io.delilaheve.util.ConfigOptions; +import org.bukkit.configuration.file.FileConfiguration; +import xyz.alexcrea.cuanvil.config.ConfigHolder; +import xyz.alexcrea.cuanvil.config.WorkPenaltyType; +import xyz.alexcrea.cuanvil.gui.config.settings.WorkPenaltyTypeSettingGui; +import xyz.alexcrea.cuanvil.util.AnvilUseType; + +import javax.annotation.Nonnull; +import java.util.EnumMap; +import java.util.Set; + +public class PUpdate_1_8_0 { + + private static final String WORK_PENALTY_TYPE = "work_penalty_type"; + + public static void handleUpdate(@Nonnull Set toSave) { + FileConfiguration config = ConfigHolder.DEFAULT_CONFIG.getConfig(); + + // We migrate the work penalty type if it exists + String penaltyTypeValue = config.getString(WORK_PENALTY_TYPE); + if (penaltyTypeValue == null) return; + + EnumMap partEnum; + partEnum = new EnumMap<>(ConfigOptions.INSTANCE.getWorkPenaltyType().getPartMap()); + + boolean keepIncrease; + boolean keepAdditive; + + switch (penaltyTypeValue.toLowerCase()) { + case "add_only": + keepIncrease = false; + keepAdditive = true; + break; + case "increase_only": + keepIncrease = true; + keepAdditive = false; + break; + case "disabled": + keepIncrease = false; + keepAdditive = false; + break; + default: + keepIncrease = true; + keepAdditive = true; + } + + for (AnvilUseType type : partEnum.keySet()) { + WorkPenaltyType.WorkPenaltyPart part = partEnum.get(type); + part = new WorkPenaltyType.WorkPenaltyPart( + keepIncrease & part.penaltyIncrease(), + keepAdditive & part.penaltyAdditive()); + partEnum.replace(type, part); + } + + if(WorkPenaltyTypeSettingGui.saveWorkPenalty(partEnum)){ + config.set(WORK_PENALTY_TYPE, null); + } + + toSave.add(ConfigHolder.DEFAULT_CONFIG); + } + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PluginUpdates.java b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PluginUpdates.java index 2069b03..548c6d1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PluginUpdates.java +++ b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PluginUpdates.java @@ -12,21 +12,26 @@ public class PluginUpdates { private static final String CONFIG_VERSION_PATH = "configVersion"; - public static void handlePluginUpdate(){ + public static void handlePluginUpdate() { String versionString = ConfigHolder.DEFAULT_CONFIG.getConfig().getString(CONFIG_VERSION_PATH); Version current = versionString == null ? new Version(0) : Version.fromString(versionString); Set toSave = new HashSet<>(); - if(new Version(1, 6, 2).greaterThan(current)){ + if (new Version(1, 6, 2).greaterThan(current)) { PUpdate_1_6_2.handleUpdate(toSave); // We assume 1.6.7 will run. TODO a better system instead of that I guess } - if(new Version(1, 6, 7).greaterThan(current)){ + if (new Version(1, 6, 7).greaterThan(current)) { PUpdate_1_6_7.handleUpdate(toSave); - finishConfiguration("1.6.7", toSave); + // We assume 1.8.0 will run. + } + if (new Version(1, 8, 0).greaterThan(current)) { + PUpdate_1_8_0.handleUpdate(toSave); + + finishConfiguration("1.8.0", toSave); } } diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt index 80d7521..21e7822 100644 --- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt +++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt @@ -37,7 +37,6 @@ object ConfigOptions { const val PERMISSION_NEEDED_FOR_COLOR = "permission_needed_for_color" const val USE_OF_COLOR_COST = "use_of_color_cost" - //const val WORK_PENALTY_TYPE = "work_penalty_type" TODO move old value to config migration const val WORK_PENALTY_ROOT = "work_penalty" const val WORK_PENALTY_INCREASE = "increase" const val WORK_PENALTY_ADDITIVE = "additive" diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ebab437..10d6ef9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -72,18 +72,6 @@ permission_needed_for_color: true # Valid values include 0 to 1000. use_of_color_cost: 0 -# Work penalty increase the price for every anvil use. -# This config allow you to choose the comportment of work penalty. -# Vanilla work penalty formula can be represented as 2 * previous_penalty + 1. with start value equal to 0 -# See https://minecraft.wiki/w/Anvil_mechanics#Anvil_uses for more detail -# -# Valid work penalty type is: -# - default: work penalty added and increased -# - increase_only: work penalty increased but not added -# - add_only: work penalty added but not increased -# - disabled: work penalty disabled -work_penalty_type: default - # Default limit to apply to any enchants missing from enchant_limits # # Valid values include 1 to 1000 @@ -294,4 +282,4 @@ debug_log_verbose: false # ProtocoLib may also be used if the server is in an "unsupported" version even if this option is disabled. force_protocolib: false -configVersion: 1.6.7 +configVersion: 1.8.0