Use namespace for level limit & level cost

This commit is contained in:
alexcrea 2024-09-07 15:39:46 +02:00
parent a00bb919f4
commit 33605dbaf3
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
5 changed files with 159 additions and 171 deletions

View file

@ -44,24 +44,16 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
@Override
public EnchantCostSettingsGui.EnchantCostSettingFactory createFactory(CAEnchantment enchant) {
String key = enchant.getKey().getKey().toLowerCase(Locale.ENGLISH);
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
String key = enchant.getKey().toString().toLowerCase(Locale.ENGLISH);
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key.replace(":", "_"));
// try to find rarity. default to 0 if not found
EnchantmentRarity rarity = enchant.defaultRarity();
try {
rarity = EnchantmentProperties.valueOf(key.toUpperCase(Locale.ENGLISH)).getRarity();
} catch (IllegalArgumentException ignored) {
}
return EnchantCostSettingsGui.enchantCostFactory(prettyKey + " Level Cost", this,
ConfigHolder.DEFAULT_CONFIG, SECTION_NAME + '.' + key,
return new EnchantCostSettingsGui.EnchantCostSettingFactory(prettyKey + " Cost", this,
SECTION_NAME + '.' + key, ConfigHolder.DEFAULT_CONFIG,
Arrays.asList(
"§7How many level should " + prettyKey,
"§7cost when applied by book or by another item."
),
0, 255,
rarity.getItemValue(), rarity.getBookValue(),
enchant, 0, 255,
1, 10, 50);
}

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.global;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import io.delilaheve.util.ConfigOptions;
import org.bukkit.Material;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
@ -37,17 +38,23 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
@Override
public IntSettingsGui.IntSettingFactory createFactory(CAEnchantment enchant) {
String key = enchant.getKey().getKey().toLowerCase(Locale.ROOT);
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
String key = enchant.getKey().toString().toLowerCase(Locale.ROOT);
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key.replace(":", "_"));
return new IntSettingsGui.IntSettingFactory(prettyKey + " Level Limit", this,
return new IntSettingsGui.IntSettingFactory(prettyKey + "Limit", this,
SECTION_NAME + '.' + key, ConfigHolder.DEFAULT_CONFIG,
Collections.singletonList(
"§7Maximum applied level of " + prettyKey
),
0, 255,
enchant.defaultMaxLevel(),
1, 5, 10, 50, 100);
1, 5, 10, 50, 100){
@Override
public int getConfiguredValue() {
return ConfigOptions.INSTANCE.enchantLimit(enchant);
}
};
}
@Override

View file

@ -5,6 +5,7 @@ import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
import io.delilaheve.util.ConfigOptions;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemFlag;
@ -13,6 +14,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
@ -237,43 +239,13 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
return super.hadChange() || nowBook != beforeBook;
}
/**
* Create an int setting factory from setting's parameters.
*
* @param title The title of the gui.
* @param parent Parent gui to go back when completed.
* @param config Configuration holder of this setting.
* @param configPath Configuration path of this setting.
* @param displayLore Gui display item lore.
* @param min Minimum value of this setting.
* @param max Maximum value of this setting.
* @param defaultItemVal Default item value if not found on the config.
* @param defaultBookVal Default book value if not found on the config.
* @param steps List of step the value can increment/decrement.
* List's size should be between 1 (included) and 3 (included).
* it is visually preferable to have an odd number of step.
* If step only contain 1 value, no step item should be displayed.
* @return A factory for an enchant cost setting gui.
*/
public static EnchantCostSettingFactory enchantCostFactory(
@NotNull String title, @NotNull ValueUpdatableGui parent,
@NotNull ConfigHolder config, @NotNull String configPath,
@Nullable List<String> displayLore,
int min, int max, int defaultItemVal, int defaultBookVal,
int... steps) {
return new EnchantCostSettingFactory(
title, parent,
configPath, config,
displayLore,
min, max, defaultItemVal, defaultBookVal, steps);
}
/**
* A factory for an enchantment cost setting gui that hold setting's information.
*/
public static class EnchantCostSettingFactory extends IntSettingsGui.IntSettingFactory {
int defaultBookVal;
@NotNull CAEnchantment enchantment;
/**
* Constructor for an enchantment cost setting gui factory.
@ -285,25 +257,27 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
* @param displayLore Gui display item lore.
* @param min Minimum value of this setting.
* @param max Maximum value of this setting.
* @param defaultItemVal Default item value if not found on the config.
* @param defaultBookVal Default book value if not found on the config.
* @param enchantment Enchantment to change the cost to
* @param steps List of step the value can increment/decrement.
* List's size should be between 1 (included) and 3 (included).
* it is visually preferable to have an odd number of step.
* If step only contain 1 value, no step item should be displayed.
*/
protected EnchantCostSettingFactory(
public EnchantCostSettingFactory(
@NotNull String title, ValueUpdatableGui parent,
@NotNull String configPath, @NotNull ConfigHolder config,
@Nullable List<String> displayLore,
int min, int max, int defaultItemVal, int defaultBookVal,
int... steps) {
@NotNull CAEnchantment enchantment,
int min, int max, int... steps) {
super(title, parent,
configPath, config,
displayLore,
min, max, defaultItemVal, steps);
this.defaultBookVal = defaultBookVal;
min, max, enchantment.defaultRarity().getItemValue(),
steps);
this.defaultBookVal = enchantment.defaultRarity().getBookValue();
this.enchantment = enchantment;
}
/**
@ -311,14 +285,14 @@ public class EnchantCostSettingsGui extends IntSettingsGui {
*/
@Override
public int getConfiguredValue() {
return this.config.getConfig().getInt(this.configPath + ITEM_PATH, this.defaultVal);
return ConfigOptions.INSTANCE.enchantmentValue(enchantment, false);
}
/**
* @return The configured value for the enchant setting book value.
*/
public int getConfiguredBookValue() {
return this.config.getConfig().getInt(this.configPath + BOOK_PATH, this.defaultBookVal);
return ConfigOptions.INSTANCE.enchantmentValue(enchantment, true);
}
@Override

View file

@ -301,21 +301,28 @@ object ConfigOptions {
* Get the given [enchantment]'s limit
*/
fun enchantLimit(enchantment: CAEnchantment): Int {
return enchantLimit(enchantment.enchantmentName)
// Test namespace
var limit = enchantLimit(enchantment.key.toString())
if(limit != null) return limit;
// Test legacy (name only)
limit = enchantLimit(enchantment.enchantmentName)
if(limit != null) return limit;
// get default (and test old legacy if present)
return getDefaultLevel(enchantment.enchantmentName)
}
/**
* Get the given [enchantmentName]'s limit
*/
private fun enchantLimit(enchantmentName: String): Int {
val default = getDefaultLevel(enchantmentName)
private fun enchantLimit(enchantmentName: String): Int? {
val path = "${ENCHANT_LIMIT_ROOT}.$enchantmentName"
return CustomAnvil.instance
.config
.getInt(path, default)
.getInt(path, ENCHANT_LIMIT_RANGE.first-1)
.takeIf { it in ENCHANT_LIMIT_RANGE }
?: default
}
/**
@ -324,7 +331,9 @@ object ConfigOptions {
private fun getDefaultLevel(enchantmentName: String, // compatibility with 1.20.5. TODO better update system
) : Int {
if(enchantmentName == "sweeping_edge"){
return enchantLimit("sweeping")
val limit = enchantLimit("sweeping")
if(limit != null) return limit
}
return defaultEnchantLimit
}
@ -337,7 +346,17 @@ object ConfigOptions {
enchantment: CAEnchantment,
isFromBook: Boolean
): Int {
return enchantmentValue(enchantment.enchantmentName, isFromBook)
// Test namespace
var limit = enchantmentValue(enchantment.key.toString(), isFromBook)
if(limit != null) return limit;
// Test legacy (name only)
limit = enchantmentValue(enchantment.enchantmentName, isFromBook)
if(limit != null) return limit;
// get default (and test old legacy if present)
return getDefaultValue(enchantment, isFromBook)
}
/**
@ -347,36 +366,32 @@ object ConfigOptions {
private fun enchantmentValue(
enchantmentName: String,
isFromBook: Boolean
): Int {
val default = getDefaultValue(enchantmentName, isFromBook)
): Int? {
val typeKey = if (isFromBook) KEY_BOOK else KEY_ITEM
val path = "${ENCHANT_VALUES_ROOT}.${enchantmentName}.$typeKey"
return CustomAnvil.instance
.config
.getInt(path, default)
.getInt(path, DEFAULT_ENCHANT_VALUE - 1)
.takeIf { it >= DEFAULT_ENCHANT_VALUE }
?: DEFAULT_ENCHANT_VALUE
}
/**
* Get default value if enchantment do not exist on config
*/
private fun getDefaultValue(enchantmentName: String, // compatibility with 1.20.5. TODO better update system
private fun getDefaultValue(enchantment: CAEnchantment, // compatibility with 1.20.5. TODO better update system
isFromBook: Boolean) : Int {
val enchantmentName = enchantment.enchantmentName
if(enchantmentName == "sweeping_edge"){
return enchantmentValue("sweeping", isFromBook)
val limit = enchantmentValue("sweeping", isFromBook)
if(limit != null) return limit
}
val enchantment = CAEnchantment.getByName(enchantmentName)
if(enchantment != null){
val rarity = enchantment.defaultRarity()
return if(isFromBook) rarity.bookValue
else rarity.itemValue
}
return DEFAULT_ENCHANT_VALUE
return if(isFromBook)
rarity.bookValue
else
rarity.itemValue
}
/**

View file

@ -97,46 +97,46 @@ default_limit: 5
#
# Valid range of 1 - 255 for each enchantment
enchant_limits:
aqua_affinity: 1
binding_curse: 1
channeling: 1
flame: 1
infinity: 1
mending: 1
multishot: 1
silk_touch: 1
vanishing_curse: 1
depth_strider: 3 # anything more than 3 is treated as 3 by the game
protection: 4
fire_protection: 4
blast_protection: 4
projectile_protection: 4
feather_falling: 4
thorns: 3
respiration: 3
sharpness: 5
smite: 5
bane_of_arthropods: 5
knockback: 2
fire_aspect: 2
looting: 3
sweeping: 3
sweeping_edge: 3
efficiency: 5
unbreaking: 3
fortune: 3
power: 5
punch: 2
luck_of_the_sea: 3
lure: 3
frost_walker: 2
impaling: 5
riptide: 3
loyalty: 3
piercing: 4
quick_charge: 3
soul_speed: 3
swift_sneak: 3
minecraft:aqua_affinity: 1
minecraft:binding_curse: 1
minecraft:channeling: 1
minecraft:flame: 1
minecraft:infinity: 1
minecraft:mending: 1
minecraft:multishot: 1
minecraft:silk_touch: 1
minecraft:vanishing_curse: 1
minecraft:depth_strider: 3 # anything more than 3 is treated as 3 by the game
minecraft:protection: 4
minecraft:fire_protection: 4
minecraft:blast_protection: 4
minecraft:projectile_protection: 4
minecraft:feather_falling: 4
minecraft:thorns: 3
minecraft:respiration: 3
minecraft:sharpness: 5
minecraft:smite: 5
minecraft:bane_of_arthropods: 5
minecraft:knockback: 2
minecraft:fire_aspect: 2
minecraft:looting: 3
minecraft:sweeping: 3
minecraft:sweeping_edge: 3
minecraft:efficiency: 5
minecraft:unbreaking: 3
minecraft:fortune: 3
minecraft:power: 5
minecraft:punch: 2
minecraft:luck_of_the_sea: 3
minecraft:lure: 3
minecraft:frost_walker: 2
minecraft:impaling: 5
minecraft:riptide: 3
minecraft:loyalty: 3
minecraft:piercing: 4
minecraft:quick_charge: 3
minecraft:soul_speed: 3
minecraft:swift_sneak: 3
# Multipliers used to calculate the enchantment's value in repair/combining
#
@ -150,124 +150,124 @@ enchant_limits:
# With default values protection 4 would have a value of 4 when
# coming from either a book (4 * 1) or an item (4 * 1)
enchant_values:
aqua_affinity:
minecraft:aqua_affinity:
item: 4
book: 2
bane_of_arthropods:
minecraft:bane_of_arthropods:
item: 2
book: 1
binding_curse:
minecraft:binding_curse:
item: 8
book: 4
blast_protection:
minecraft:blast_protection:
item: 4
book: 2
channeling:
minecraft:channeling:
item: 8
book: 4
depth_strider:
minecraft:depth_strider:
item: 4
book: 2
efficiency:
minecraft:efficiency:
item: 1
book: 1
flame:
minecraft:flame:
item: 4
book: 2
feather_falling:
minecraft:feather_falling:
item: 2
book: 1
fire_aspect:
minecraft:fire_aspect:
item: 4
book: 2
fire_protection:
minecraft:fire_protection:
item: 2
book: 1
fortune:
minecraft:fortune:
item: 4
book: 2
frost_walker:
minecraft:frost_walker:
item: 4
book: 2
impaling:
minecraft:impaling:
item: 4
book: 2
infinity:
minecraft:infinity:
item: 8
book: 4
knockback:
minecraft:knockback:
item: 2
book: 1
looting:
minecraft:looting:
item: 4
book: 2
loyalty:
minecraft:loyalty:
item: 1
book: 1
luck_of_the_sea:
minecraft:luck_of_the_sea:
item: 4
book: 2
lure:
minecraft:lure:
item: 4
book: 2
mending:
minecraft:mending:
item: 4
book: 2
multishot:
minecraft:multishot:
item: 4
book: 2
piercing:
minecraft:piercing:
item: 1
book: 1
power:
minecraft:power:
item: 1
book: 1
projectile_protection:
minecraft:projectile_protection:
item: 2
book: 1
protection:
minecraft:protection:
item: 1
book: 1
punch:
minecraft:punch:
item: 4
book: 2
quick_charge:
minecraft:quick_charge:
item: 2
book: 1
respiration:
minecraft:respiration:
item: 4
book: 2
riptide:
minecraft:riptide:
item: 4
book: 2
silk_touch:
minecraft:silk_touch:
item: 8
book: 4
sharpness:
minecraft:sharpness:
item: 1
book: 1
smite:
minecraft:smite:
item: 2
book: 1
soul_speed:
minecraft:soul_speed:
item: 8
book: 4
swift_sneak:
minecraft:swift_sneak:
item: 8
book: 4
sweeping:
minecraft:sweeping:
item: 4
book: 2
sweeping_edge:
minecraft:sweeping_edge:
item: 4
book: 2
thorns:
minecraft:thorns:
item: 8
book: 4
unbreaking:
minecraft:unbreaking:
item: 2
book: 1
vanishing_curse:
minecraft:vanishing_curse:
item: 8
book: 4