simplify and extend enchant limit

This commit is contained in:
alexd 2026-03-23 17:57:47 +01:00
parent f59071f504
commit c96dd7d308
10 changed files with 58 additions and 86 deletions

View file

@ -87,18 +87,14 @@ permission_needed_for_color: true
# Valid values include 0 to 1000. # Valid values include 0 to 1000.
use_of_color_cost: 0 use_of_color_cost: 0
# Default limit to apply to any enchants missing from enchant_limits
#
# Valid values include 1 to 1000
default_limit: 5
# Override limits for specific enchants # Override limits for specific enchants
# #
# Enchantments not listed here will use the value of default_limit # Enchantments not listed here will use the value of default_limit
# #
# Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels # Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels
# #
# Valid range of 1 - 255 for each enchantment # Valid range of 0 - 255 for each enchantment
# -1 mean keep default
enchant_limits: enchant_limits:
minecraft:aqua_affinity: 1 minecraft:aqua_affinity: 1
minecraft:binding_curse: 1 minecraft:binding_curse: 1

View file

@ -89,18 +89,14 @@ permission_needed_for_color: true
# Valid values include 0 to 1000. # Valid values include 0 to 1000.
use_of_color_cost: 0 use_of_color_cost: 0
# Default limit to apply to any enchants missing from enchant_limits
#
# Valid values include 1 to 1000
default_limit: 5
# Override limits for specific enchants # Override limits for specific enchants
# #
# Enchantments not listed here will use the value of default_limit # Enchantments not listed here will use the value of default_limit
# #
# Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels # Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels
# #
# Valid range of 1 - 255 for each enchantment # Valid range of 0 - 255 for each enchantment
# -1 mean keep default
enchant_limits: enchant_limits:
minecraft:aqua_affinity: 1 minecraft:aqua_affinity: 1
minecraft:binding_curse: 1 minecraft:binding_curse: 1

View file

@ -87,18 +87,14 @@ permission_needed_for_color: true
# Valid values include 0 to 1000. # Valid values include 0 to 1000.
use_of_color_cost: 0 use_of_color_cost: 0
# Default limit to apply to any enchants missing from enchant_limits
#
# Valid values include 1 to 1000
default_limit: 5
# Override limits for specific enchants # Override limits for specific enchants
# #
# Enchantments not listed here will use the value of default_limit # Enchantments not listed here will use the value of default_limit
# #
# Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels # Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels
# #
# Valid range of 1 - 255 for each enchantment # Valid range of 0 - 255 for each enchantment
# -1 mean keep default
enchant_limits: enchant_limits:
minecraft:aqua_affinity: 1 minecraft:aqua_affinity: 1
minecraft:binding_curse: 1 minecraft:binding_curse: 1

View file

@ -87,18 +87,14 @@ permission_needed_for_color: true
# Valid values include 0 to 1000. # Valid values include 0 to 1000.
use_of_color_cost: 0 use_of_color_cost: 0
# Default limit to apply to any enchants missing from enchant_limits
#
# Valid values include 1 to 1000
default_limit: 5
# Override limits for specific enchants # Override limits for specific enchants
# #
# Enchantments not listed here will use the value of default_limit # Enchantments not listed here will use the value of default_limit
# #
# Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels # Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels
# #
# Valid range of 1 - 255 for each enchantment # Valid range of 0 - 255 for each enchantment
# -1 mean keep default
enchant_limits: enchant_limits:
minecraft:aqua_affinity: 1 minecraft:aqua_affinity: 1
minecraft:binding_curse: 1 minecraft:binding_curse: 1

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.api; package xyz.alexcrea.cuanvil.api;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import io.delilaheve.util.ConfigOptions;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -180,13 +181,13 @@ public class EnchantmentApi {
private static boolean tryWriteDefaultConfig(FileConfiguration defaultConfig, CAEnchantment enchantment, boolean override) { private static boolean tryWriteDefaultConfig(FileConfiguration defaultConfig, CAEnchantment enchantment, boolean override) {
boolean hasChange = false; boolean hasChange = false;
String levelPath = "enchant_limits." + enchantment.getKey(); String levelPath = ConfigOptions.ENCHANT_LIMIT_ROOT + "." + enchantment.getKey();
if(override || !defaultConfig.isSet(levelPath)){ if(override || !defaultConfig.isSet(levelPath)){
defaultConfig.set(levelPath, enchantment.defaultMaxLevel()); defaultConfig.set(levelPath, enchantment.defaultMaxLevel());
hasChange = true; hasChange = true;
} }
String basePath = "enchant_values." + enchantment.getKey(); String basePath = ConfigOptions.ENCHANT_VALUES_ROOT + "." + enchantment.getKey();
EnchantmentRarity rarity = enchantment.defaultRarity(); EnchantmentRarity rarity = enchantment.defaultRarity();
String itemPath = basePath + ".item"; String itemPath = basePath + ".item";

View file

@ -17,7 +17,7 @@ import java.util.Locale;
*/ */
public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsGui.IntSettingFactory> { public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsGui.IntSettingFactory> {
private static final String SECTION_NAME = "enchant_limits"; private static final String SECTION_NAME = ConfigOptions.ENCHANT_LIMIT_ROOT;
private static EnchantLimitConfigGui INSTANCE = null; private static EnchantLimitConfigGui INSTANCE = null;
@ -41,18 +41,26 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
String key = enchant.getKey().toString().toLowerCase(Locale.ROOT); String key = enchant.getKey().toString().toLowerCase(Locale.ROOT);
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key.replace(":", "_")); String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key.replace(":", "_"));
var defaultValue = enchant.defaultMaxLevel();
return new IntSettingsGui.IntSettingFactory(prettyKey + " Limit", this, return new IntSettingsGui.IntSettingFactory(prettyKey + " Limit", this,
SECTION_NAME + '.' + key, ConfigHolder.DEFAULT_CONFIG, SECTION_NAME + '.' + key, ConfigHolder.DEFAULT_CONFIG,
Collections.singletonList( Collections.singletonList(
"§7Maximum applied level of " + prettyKey "§7Maximum applied level of " + prettyKey
), ),
0, 255, -1, 255, -1,
enchant.defaultMaxLevel(),
1, 5, 10, 50, 100){ 1, 5, 10, 50, 100){
@Override @Override
public int getConfiguredValue() { public int getConfiguredValue() {
return ConfigOptions.INSTANCE.enchantLimit(enchant); var value = ConfigOptions.INSTANCE.rawEnchantLimit(enchant);
return Math.min(value, ConfigOptions.ENCHANT_LIMIT);
}
@Override
public String valueDisplayName(int value) {
if(value == -1) return "Default (" + defaultValue + ")";
return super.valueDisplayName(value);
} }
}; };
} }

View file

@ -88,6 +88,7 @@ public class IntSettingsGui extends AbstractSettingGui {
protected void updateValueDisplay() { protected void updateValueDisplay() {
PatternPane pane = getPane(); PatternPane pane = getPane();
var displayNow = holder.valueDisplayName(now);
// minus item // minus item
GuiItem minusItem; GuiItem minusItem;
@ -97,7 +98,7 @@ public class IntSettingsGui extends AbstractSettingGui {
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
assert meta != null; assert meta != null;
meta.setDisplayName("§e" + now + " §f-> §e" + planned + " §r(§c-" + (now - planned) + "§r)"); meta.setDisplayName("§e" + displayNow + " §f-> §e" + planned + " §r(§c-" + holder.valueDisplayName(now - planned) + "§r)");
meta.setLore(Collections.singletonList(AbstractSettingGui.CLICK_LORE)); meta.setLore(Collections.singletonList(AbstractSettingGui.CLICK_LORE));
item.setItemMeta(meta); item.setItemMeta(meta);
@ -116,7 +117,7 @@ public class IntSettingsGui extends AbstractSettingGui {
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
assert meta != null; assert meta != null;
meta.setDisplayName("§e" + now + " §f-> §e" + planned + " §r(§a+" + (planned - now) + "§r)"); meta.setDisplayName("§e" + displayNow + " §f-> §e" + planned + " §r(§a+" + holder.valueDisplayName(planned - now) + "§r)");
meta.setLore(Collections.singletonList(AbstractSettingGui.CLICK_LORE)); meta.setLore(Collections.singletonList(AbstractSettingGui.CLICK_LORE));
item.setItemMeta(meta); item.setItemMeta(meta);
@ -131,7 +132,7 @@ public class IntSettingsGui extends AbstractSettingGui {
ItemMeta resultMeta = resultPaper.getItemMeta(); ItemMeta resultMeta = resultPaper.getItemMeta();
assert resultMeta != null; assert resultMeta != null;
resultMeta.setDisplayName("§fValue: §e" + now); resultMeta.setDisplayName("§fValue: §e" + displayNow);
resultMeta.setLore(holder.displayLore); resultMeta.setLore(holder.displayLore);
resultPaper.setItemMeta(resultMeta); resultPaper.setItemMeta(resultMeta);
@ -389,6 +390,10 @@ public class IntSettingsGui extends AbstractSettingGui {
return getItem(itemMat, CasedStringUtil.detectToUpperSpacedCase(configPath)); return getItem(itemMat, CasedStringUtil.detectToUpperSpacedCase(configPath));
} }
public String valueDisplayName(int value) {
return String.valueOf(value);
}
} }
} }

View file

@ -30,7 +30,6 @@ public class PluginSetDefault {
nbSet += trySetDefault(config, ALLOW_HEXADECIMAL_COLOR, DEFAULT_ALLOW_HEXADECIMAL_COLOR); nbSet += trySetDefault(config, ALLOW_HEXADECIMAL_COLOR, DEFAULT_ALLOW_HEXADECIMAL_COLOR);
nbSet += trySetDefault(config, PERMISSION_NEEDED_FOR_COLOR, DEFAULT_PERMISSION_NEEDED_FOR_COLOR); nbSet += trySetDefault(config, PERMISSION_NEEDED_FOR_COLOR, DEFAULT_PERMISSION_NEEDED_FOR_COLOR);
nbSet += trySetDefault(config, USE_OF_COLOR_COST, DEFAULT_USE_OF_COLOR_COST); nbSet += trySetDefault(config, USE_OF_COLOR_COST, DEFAULT_USE_OF_COLOR_COST);
nbSet += trySetDefault(config, DEFAULT_LIMIT_PATH, DEFAULT_ENCHANT_LIMIT);
// Lore Edit defaults // Lore Edit defaults
for (@NotNull LoreEditType value : LoreEditType.values()) { for (@NotNull LoreEditType value : LoreEditType.values()) {

View file

@ -57,8 +57,6 @@ object ConfigOptions {
const val ENCHANT_COUNT_LIMIT_DEFAULT = "$ENCHANT_COUNT_LIMIT_ROOT.default" const val ENCHANT_COUNT_LIMIT_DEFAULT = "$ENCHANT_COUNT_LIMIT_ROOT.default"
const val ENCHANT_COUNT_LIMIT_ITEMS = "$ENCHANT_COUNT_LIMIT_ROOT.items" const val ENCHANT_COUNT_LIMIT_ITEMS = "$ENCHANT_COUNT_LIMIT_ROOT.items"
const val DEFAULT_LIMIT_PATH = "default_limit"
const val ENCHANT_LIMIT_ROOT = "enchant_limits" const val ENCHANT_LIMIT_ROOT = "enchant_limits"
const val ENCHANT_VALUES_ROOT = "enchant_values" const val ENCHANT_VALUES_ROOT = "enchant_values"
@ -102,8 +100,6 @@ object ConfigOptions {
const val DEFAULT_PERMISSION_NEEDED_FOR_COLOR = true const val DEFAULT_PERMISSION_NEEDED_FOR_COLOR = true
const val DEFAULT_USE_OF_COLOR_COST = 0 const val DEFAULT_USE_OF_COLOR_COST = 0
const val DEFAULT_ENCHANT_LIMIT = 5
// Debug flag // Debug flag
private const val DEFAULT_DEBUG_LOG = false private const val DEFAULT_DEBUG_LOG = false
private const val DEFAULT_VERBOSE_DEBUG_LOG = false private const val DEFAULT_VERBOSE_DEBUG_LOG = false
@ -133,8 +129,7 @@ object ConfigOptions {
val USE_OF_COLOR_COST_RANGE = 0..1000 val USE_OF_COLOR_COST_RANGE = 0..1000
// Valid range for an enchantment limit // Valid range for an enchantment limit
@JvmField const val ENCHANT_LIMIT = 255
val ENCHANT_LIMIT_RANGE = 1..255
// Valid range for an enchantment count limit // Valid range for an enchantment count limit
@JvmField @JvmField
@ -348,16 +343,6 @@ object ConfigOptions {
return WorkPenaltyPart(penaltyIncrease, penaltyAdditive, exclusivePenaltyIncrease, exclusivePenaltyAdditive) return WorkPenaltyPart(penaltyIncrease, penaltyAdditive, exclusivePenaltyIncrease, exclusivePenaltyAdditive)
} }
/**
* Default enchantment limit
*/
private val defaultEnchantLimit: Int
get() {
return ConfigHolder.DEFAULT_CONFIG
.config
.getInt(DEFAULT_LIMIT_PATH, DEFAULT_ENCHANT_LIMIT)
}
/** /**
* Get material enchantment count limit * Get material enchantment count limit
* *
@ -422,42 +407,37 @@ object ConfigOptions {
* Get the given [enchantment]'s limit * Get the given [enchantment]'s limit
*/ */
fun enchantLimit(enchantment: CAEnchantment): Int { fun enchantLimit(enchantment: CAEnchantment): Int {
val limit = rawEnchantLimit(enchantment)
if(limit >= 0) return limit.coerceAtMost(ENCHANT_LIMIT)
// get default
return enchantment.defaultMaxLevel()
}
/**
* Get the given [enchantment]'s limit
*/
fun rawEnchantLimit(enchantment: CAEnchantment): Int {
// Test namespace // Test namespace
var limit = enchantLimit(enchantment.key.toString()) var limit = enchantLimit(enchantment.key.toString())
if (limit != null) return limit if (limit >= 0) return limit
// Test legacy (name only) // Test legacy (name only)
limit = enchantLimit(enchantment.enchantmentName) limit = enchantLimit(enchantment.enchantmentName)
if (limit != null) return limit if (limit >= 0) return limit
// get default (and test old legacy if present) // Default to negative
return getDefaultLevel(enchantment.enchantmentName) return -1
} }
/** /**
* Get the given [enchantmentName]'s limit * Get the given [enchantmentName]'s limit
*/ */
private fun enchantLimit(enchantmentName: String): Int? { private fun enchantLimit(enchantmentName: String): Int {
val path = "${ENCHANT_LIMIT_ROOT}.$enchantmentName" val path = "${ENCHANT_LIMIT_ROOT}.$enchantmentName"
return CustomAnvil.instance return CustomAnvil.instance.config
.config .getInt(path, -1)
.getInt(path, ENCHANT_LIMIT_RANGE.first - 1)
.takeIf { it in ENCHANT_LIMIT_RANGE }
}
/**
* Get default value if enchantment do not exist on config
*/
private fun getDefaultLevel(
enchantmentName: String, // compatibility with 1.20.5. TODO better update system
): Int {
if (enchantmentName == "sweeping_edge") {
val limit = enchantLimit("sweeping")
if (limit != null) return limit
}
return defaultEnchantLimit
} }
/** /**
@ -529,20 +509,20 @@ object ConfigOptions {
fun maxBeforeMergeDisabled(enchantment: CAEnchantment): Int { fun maxBeforeMergeDisabled(enchantment: CAEnchantment): Int {
val key = enchantment.key.toString() val key = enchantment.key.toString()
var value = maxBeforeMergeDisabled(key) var value = maxBeforeMergeDisabled(key)
if (value != null) return value if (value >= 0) return value
// Legacy name // Legacy name
val legacy = enchantment.enchantmentName val legacy = enchantment.enchantmentName
value = maxBeforeMergeDisabled(legacy) value = maxBeforeMergeDisabled(legacy)
if (value != null) return value if (value >= 0) return value
if (key == "minecraft:sweeping_edge") { if (key == "minecraft:sweeping_edge") {
value = maxBeforeMergeDisabled("minecraft:sweeping") value = maxBeforeMergeDisabled("minecraft:sweeping")
if (value != null) return value if (value >= 0) return value
// legacy name of legacy enchantment name // legacy name of legacy enchantment name
value = maxBeforeMergeDisabled("sweeping") value = maxBeforeMergeDisabled("sweeping")
if (value != null) return value if (value >= 0) return value
} }
return DEFAULT_MAX_BEFORE_MERGE_DISABLED return DEFAULT_MAX_BEFORE_MERGE_DISABLED
@ -552,14 +532,13 @@ object ConfigOptions {
* Get the given [enchantmentName]'s level before merge is disabled * Get the given [enchantmentName]'s level before merge is disabled
* a negative value would mean never disabled * a negative value would mean never disabled
*/ */
private fun maxBeforeMergeDisabled(enchantmentName: String): Int? { private fun maxBeforeMergeDisabled(enchantmentName: String): Int {
// find if set // find if set
val path = "${DISABLE_MERGE_OVER_ROOT}.$enchantmentName" val path = "${DISABLE_MERGE_OVER_ROOT}.$enchantmentName"
return CustomAnvil.instance return CustomAnvil.instance
.config .config
.getInt(path, ENCHANT_LIMIT_RANGE.min() - 1) .getInt(path, -1)
.takeIf { it in ENCHANT_LIMIT_RANGE }
} }
fun isImmutable(key: NamespacedKey): Boolean { fun isImmutable(key: NamespacedKey): Boolean {

View file

@ -89,18 +89,14 @@ permission_needed_for_color: true
# Valid values include 0 to 1000. # Valid values include 0 to 1000.
use_of_color_cost: 0 use_of_color_cost: 0
# Default limit to apply to any enchants missing from enchant_limits
#
# Valid values include 1 to 1000
default_limit: 5
# Override limits for specific enchants # Override limits for specific enchants
# #
# Enchantments not listed here will use the value of default_limit # Enchantments not listed here will use the value of default_limit
# #
# Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels # Overrides provided default from aqua_affinity to depth_strider won't change effect with extra levels
# #
# Valid range of 1 - 255 for each enchantment # Valid range of 0 - 255 for each enchantment
# -1 mean keep default
enchant_limits: enchant_limits:
minecraft:aqua_affinity: 1 minecraft:aqua_affinity: 1
minecraft:binding_curse: 1 minecraft:binding_curse: 1