From 460114ac0159ed82679df93904ada3b302bdf4dd Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 14:37:59 +0200 Subject: [PATCH 01/14] add book on book --- .../settings/EnchantCostSettingsGui.java | 4 +-- .../io/delilaheve/util/ConfigOptions.kt | 25 +++++++++++++------ .../cuanvil/util/anvil/AnvilXpUtil.kt | 24 ++++++++++-------- .../cuanvil/config/DefaultConfigTests.java | 4 +-- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java index 3bd923bf..2fbc47ad 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java @@ -285,14 +285,14 @@ public class EnchantCostSettingsGui extends IntSettingsGui { */ @Override public int getConfiguredValue() { - return ConfigOptions.INSTANCE.enchantmentValue(enchantment, false); + return ConfigOptions.INSTANCE.enchantmentValue(enchantment, false, false); } /** * @return The configured value for the enchant setting book value. */ public int getConfiguredBookValue() { - return ConfigOptions.INSTANCE.enchantmentValue(enchantment, true); + return ConfigOptions.INSTANCE.enchantmentValue(enchantment, false, true); } @Override diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt index dddb64d7..9b0c4cd0 100644 --- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt +++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt @@ -86,6 +86,7 @@ object ConfigOptions { // Keys for specific enchantment values private const val KEY_BOOK = "book" + private const val KEY_BOOK_ON_BOOK = "book_on_book" private const val KEY_ITEM = "item" // Debug flag @@ -578,14 +579,15 @@ object ConfigOptions { */ fun enchantmentValue( enchantment: CAEnchantment, - isFromBook: Boolean + isToBook: Boolean, + isFromBook: Boolean, ): Int { // Test namespace - var limit = enchantmentValue(enchantment.key.toString(), isFromBook) + var limit = enchantmentValue(enchantment.key.toString(), isToBook, isFromBook) if (limit != null) return limit // Test legacy (name only) - limit = enchantmentValue(enchantment.enchantmentName, isFromBook) + limit = enchantmentValue(enchantment.enchantmentName, isToBook, isFromBook) if (limit != null) return limit // get default (and test old legacy if present) @@ -599,13 +601,22 @@ object ConfigOptions { */ private fun enchantmentValue( enchantmentName: String, - isFromBook: Boolean + isToBook: Boolean, + isFromBook: Boolean, ): Int? { val typeKey = if (isFromBook) KEY_BOOK else KEY_ITEM val path = "${ENCHANT_VALUES_ROOT}.${enchantmentName}.$typeKey" - return CustomAnvil.instance - .config - .getInt(path, DEFAULT_ENCHANT_VALUE - 1) + + val config = ConfigHolder.DEFAULT_CONFIG.config + if(isFromBook && isToBook) { + val specialPath = "${ENCHANT_VALUES_ROOT}.${enchantmentName}.$KEY_BOOK_ON_BOOK" + + if(config.isInt(specialPath)) + return config.getInt(specialPath) + .takeIf { it >= DEFAULT_ENCHANT_VALUE } + } + + return config.getInt(path, DEFAULT_ENCHANT_VALUE - 1) .takeIf { it >= DEFAULT_ENCHANT_VALUE } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/anvil/AnvilXpUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/anvil/AnvilXpUtil.kt index 9d2f5420..78ea8830 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/anvil/AnvilXpUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/anvil/AnvilXpUtil.kt @@ -23,6 +23,7 @@ import xyz.alexcrea.cuanvil.dependency.economy.EconomyManager import xyz.alexcrea.cuanvil.group.ConflictType import xyz.alexcrea.cuanvil.util.AnvilTitleUtil import xyz.alexcrea.cuanvil.util.dialog.AnvilRenameDialogUtil +import kotlin.math.sqrt object AnvilXpUtil { @@ -35,8 +36,9 @@ object AnvilXpUtil { inventory: AnvilInventory, view: InventoryView, player: Player, - result: AnvilResult) { - if(result.item == null) { + result: AnvilResult + ) { + if (result.item == null) { onNoResult(player, view) return } @@ -246,7 +248,8 @@ object AnvilXpUtil { fun getRightValues(left: ItemStack, right: ItemStack, result: ItemStack, cost: AnvilCost) { // Calculate right value and illegal enchant penalty - val rightIsFormBook = right.isEnchantedBook() + val isToBook = left.isEnchantedBook() + val isFromBook = right.isEnchantedBook() val rightEnchs = EnchantmentApi.getEnchantments(right) val resultEnchs = EnchantmentApi.getEnchantments(result) val resultEnchsKeys = HashMap(resultEnchs) @@ -274,22 +277,21 @@ object AnvilXpUtil { // We know "enchantment.key in resultEnchs" true val resultLevel = resultEnchs[enchantment.key]!! - val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, rightIsFormBook) + val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, isFromBook, isToBook) val value = resultLevel * enchantmentMultiplier CustomAnvil.log("Value for sacrifice item ${enchantment.key.enchantmentName} level ${enchantment.value} is $value ($resultLevel * $enchantmentMultiplier)") rightValue += value } - if(ConfigOptions.includeLeftEnchantmentForCost) { - val leftIsFormBook = left.isEnchantedBook() + if (ConfigOptions.includeLeftEnchantmentForCost) { val leftEnchs = EnchantmentApi.getEnchantments(left) for (enchantment in leftEnchs) { // Do not process enchantment that are present on the sacrifice - if(rightEnchs.contains(enchantment.key)) continue + if (rightEnchs.contains(enchantment.key)) continue val resultLevel = resultEnchs.getOrDefault(enchantment.key, 0) - val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, leftIsFormBook) + val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, isFromBook, isToBook) val value = resultLevel * enchantmentMultiplier CustomAnvil.log("Value for left item ${enchantment.key.enchantmentName} level ${enchantment.value} is $value ($resultLevel * $enchantmentMultiplier)") leftValue += value @@ -312,15 +314,15 @@ object AnvilXpUtil { */ fun calculateLevelForXp(xp: Int): Int { return when { - xp <= 352 -> (Math.sqrt((xp + 9).toDouble()) - 3).toInt() + xp <= 352 -> (sqrt((xp + 9).toDouble()) - 3).toInt() xp <= 1507 -> { val inner = (2.0 / 5.0) * (xp - 7839.0 / 40.0) - (81.0 / 10.0 + Math.sqrt(inner)).toInt() + (81.0 / 10.0 + sqrt(inner)).toInt() } else -> { val inner = (2.0 / 9.0) * (xp - 54215.0 / 72.0) - (325.0 / 18.0 + Math.sqrt(inner)).toInt() + (325.0 / 18.0 + sqrt(inner)).toInt() } } } diff --git a/src/test/java/xyz/alexcrea/cuanvil/config/DefaultConfigTests.java b/src/test/java/xyz/alexcrea/cuanvil/config/DefaultConfigTests.java index ac3f73ea..a92b1d71 100644 --- a/src/test/java/xyz/alexcrea/cuanvil/config/DefaultConfigTests.java +++ b/src/test/java/xyz/alexcrea/cuanvil/config/DefaultConfigTests.java @@ -49,8 +49,8 @@ public class DefaultConfigTests extends SharedCustomAnvilTest { CAEnchantment enchantment = CAEnchantmentRegistry.getInstance().getByKey(key); Assertions.assertNotNull(enchantment, "Enchantment was somehow not found"); - int itemValue = ConfigOptions.INSTANCE.enchantmentValue(enchantment, false); - int bookValue = ConfigOptions.INSTANCE.enchantmentValue(enchantment, true); + int itemValue = ConfigOptions.INSTANCE.enchantmentValue(enchantment, false, false); + int bookValue = ConfigOptions.INSTANCE.enchantmentValue(enchantment, false, true); EnchantmentRarity rarity = enchantment.defaultRarity(); From f6f68d19714a70bfa35b44f2990ccf2f5afd885c Mon Sep 17 00:00:00 2001 From: alexcrea Date: Tue, 7 Jul 2026 11:51:04 +0200 Subject: [PATCH 02/14] add current minecraft version to update checker [ci skip] --- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 321165e5..a5cac722 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -24,6 +24,7 @@ import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener import xyz.alexcrea.cuanvil.update.ModrinthUpdateChecker import xyz.alexcrea.cuanvil.update.PluginSetDefault import xyz.alexcrea.cuanvil.update.UpdateHandler +import xyz.alexcrea.cuanvil.update.UpdateUtils import xyz.alexcrea.cuanvil.util.MetricsUtil import java.io.File import java.io.FileReader @@ -223,7 +224,8 @@ open class CustomAnvil : JavaPlugin() { val version = description.version val featured = if(version.contains("dev")) null else true - ModrinthUpdateChecker(modrinthPluginID, loader, null) + ModrinthUpdateChecker(modrinthPluginID, loader, + UpdateUtils.currentMinecraftVersion().toString()) .setFeatured(featured) .setOnError { logger.log(Level.WARNING, "error trying to fetch latest update", it) From 4c3b3b09ad9c5fae17578d92b18122eb4da04dda Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 24 Jun 2026 03:02:10 +0200 Subject: [PATCH 03/14] replace gui to config subcommand --- .../xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt | 9 +++++++-- .../xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt index 8bd1d827..648dbc1d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt @@ -20,6 +20,7 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { private val helpCommand = HelpExecutor() private val commands = ImmutableMap.of( "gui", editConfigCommand, + "config", editConfigCommand, "reload", ReloadExecutor(), "diagnostic", DiagnosticExecutor(), "debug", DebugToggleExecutor(), @@ -42,12 +43,16 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { ): Boolean { // Find sub command to execute based on the provided command name val subcmd: CASubCommand? + val subcmdStr: String + val newargs: Array if(args.isEmpty()) { + subcmdStr = "config" subcmd = editConfigCommand newargs = args }else { - subcmd = commands[args[0].lowercase()] + subcmdStr = args[0].lowercase() + subcmd = commands[subcmdStr] newargs = args.copyOfRange(1, args.size) } @@ -57,7 +62,7 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { } try { - return subcmd.executeCommand(sender, cmd, cmdstr, newargs) + return subcmd.executeCommand(sender, cmd, subcmdStr, newargs) } catch (e: Throwable) { MetricsUtil.trackError(e) sender.sendMessage("§cError running this command") diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index d26db8c2..138ae870 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -29,6 +29,10 @@ class EditConfigExecutor: CASubCommand() { return false } + if ("gui".equals(cmdstr, ignoreCase = true)) { + sender.sendMessage("§c/ca gui has been moved to /ca config") + } + MainConfigGui.getInstance().show(sender) return true From a79e341ea85b47af6ef25f0613770286f34eb4d7 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sat, 27 Jun 2026 08:26:54 +0200 Subject: [PATCH 04/14] add list gui filter --- .../global/AbstractEnchantConfigGui.java | 5 +-- .../config/global/CustomRecipeConfigGui.java | 2 +- .../gui/config/global/EnchantConflictGui.java | 2 +- .../gui/config/global/GroupConfigGui.java | 2 +- .../config/global/UnitRepairConfigGui.java | 2 +- .../gui/config/list/ElementListConfigGui.java | 38 ++++++++++++------- .../config/list/MappedGuiListConfigGui.java | 2 +- .../config/list/UnitRepairElementListGui.java | 2 +- .../settings/EnchantSelectSettingGui.java | 2 +- .../settings/MaterialSelectSettingGui.java | 2 +- 10 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/AbstractEnchantConfigGui.java index 6bd7ea34..b4f732d4 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/AbstractEnchantConfigGui.java @@ -5,7 +5,6 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import org.bukkit.event.inventory.InventoryClickEvent; import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry; -import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui; import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; @@ -37,7 +36,7 @@ public abstract class AbstractEnchantConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { return CAEnchantmentRegistry.getInstance().getNameSortedEnchantments(); } @@ -73,7 +72,7 @@ public abstract class AbstractEnchantConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { return ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().getRecipeList(); } } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java index 912e6cb3..79f52923 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java @@ -97,7 +97,7 @@ public class EnchantConflictGui extends MappedGuiListConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { return ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList(); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java index 8e20751b..fa53174c 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java @@ -62,7 +62,7 @@ public class GroupConfigGui extends MappedGuiListConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { ArrayList includeGroups = new ArrayList<>(); for (AbstractMaterialGroup group : ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().values()) { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java index 0e366ae6..c3e8bcb0 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java @@ -77,7 +77,7 @@ public class UnitRepairConfigGui extends } @Override - protected Collection getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { ArrayList materials = new ArrayList<>(); for (String matName : ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getKeys(false)) { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java index f3cc0b46..a9e499b0 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java @@ -23,18 +23,21 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.UUID; +import java.util.function.Predicate; -public abstract class ElementListConfigGui< T > extends ChestGui implements ValueUpdatableGui { - - private final String namePrefix; - - protected PatternPane backgroundPane; +public abstract class ElementListConfigGui extends ChestGui implements ValueUpdatableGui { public static final int LIST_FILLER_START_X = 1; public static final int LIST_FILLER_START_Y = 1; public static final int LIST_FILLER_LENGTH = 7; public static final int LIST_FILLER_HEIGHT = 4; + private final String namePrefix; + + protected PatternPane backgroundPane; + + private Predicate filter = (t) -> true; + protected ElementListConfigGui(@NotNull String title, Gui parent) { super(6, title, CustomAnvil.instance); this.namePrefix = title; @@ -46,7 +49,11 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu } - protected Pattern getBackgroundPattern(){ + public void setFilter(Predicate filter) { + this.filter = filter; + } + + protected Pattern getBackgroundPattern() { return new Pattern( GuiSharedConstant.UPPER_FILLER_FULL_PLANE, GuiSharedConstant.EMPTY_FILLER_FULL_LINE, @@ -81,7 +88,7 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu protected GuiItem goLeftItem; protected GuiItem goRightItem; - protected void prepareStaticValues(){ + protected void prepareStaticValues() { // Left item creation for consumer & bind this.goLeftItem = new GuiItem(new ItemStack(Material.RED_STAINED_GLASS_PANE), event -> { HumanEntity viewer = event.getWhoClicked(); @@ -113,16 +120,17 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu }, CustomAnvil.instance); GuiItem createNew = prepareCreateNewItem(); - if(createNew != null){ + if (createNew != null) { this.backgroundPane.bindItem('C', createNew); } } - protected void reloadValues(){ + + protected void reloadValues() { this.firstPage.clear(); this.pages.clear(); this.pages.add(this.firstPage); - for (T generic : getEveryDisplayableInstanceOfGeneric()) { + for (T generic : getDisplayableInstanceOfGeneric()) { updateValueForGeneric(generic, false); } @@ -247,7 +255,7 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu // set title StringBuilder title = new StringBuilder(this.namePrefix); int pagesSize = this.pages.size(); - if(pagesSize > 1){ + if (pagesSize > 1) { title.append(" (").append(pageID + 1).append('/').append(pagesSize).append(')'); } setTitle(title.toString()); @@ -288,7 +296,7 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu public void removeGeneric(T generic) { GuiItem item = findGuiItemForRemoval(generic); - if(item == null) return; + if (item == null) return; removeFromPage(item); update(); @@ -300,7 +308,11 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu protected abstract void updateGeneric(T generic, ItemStack usedItem); - protected abstract Collection getEveryDisplayableInstanceOfGeneric(); + protected abstract Collection getEveryInstanceOfGeneric(); + + protected Collection getDisplayableInstanceOfGeneric() { + return getEveryInstanceOfGeneric().stream().filter(filter).toList(); + } @Override public void updateGuiValues() { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java index 3aa18e0b..18a5f864 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java @@ -80,7 +80,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu // Try to find if it already exists in a for loop // Not the most efficient on large number of conflict, but it should not run often. - for (T generic : getEveryDisplayableInstanceOfGeneric()) { + for (T generic : getDisplayableInstanceOfGeneric()) { if (generic.toString().equalsIgnoreCase(message)) { player.sendMessage("§cPlease enter a "+genericDisplayedName()+" name that do not already exist..."); // wait next message. diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java index 35f8ebb9..c9f2ada2 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java @@ -134,7 +134,7 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { ArrayList keys = new ArrayList<>(); if(!this.shouldWork){ return keys; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java index 176da55b..397ae6f5 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java @@ -63,7 +63,7 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { Stream toDisplayStream; if(this.displayUnselected){ toDisplayStream = CAEnchantmentRegistry.getInstance().getNameSortedEnchantments().stream(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java index fc519ff1..1869ad34 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java @@ -217,7 +217,7 @@ public class MaterialSelectSettingGui extends MappedElementListConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryInstanceOfGeneric() { return this.defaultMaterials; } From 5f0a601e5b8d925e305d58eee9798861b169ccc2 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sun, 28 Jun 2026 23:01:56 +0200 Subject: [PATCH 05/14] add edit config tab completer --- .../cuanvil/command/EditConfigExecutor.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index 138ae870..ac059e9e 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -1,10 +1,12 @@ package xyz.alexcrea.cuanvil.command +import com.willfp.eco.core.lookup.matches import io.delilaheve.CustomAnvil import org.bukkit.command.Command import org.bukkit.command.CommandSender import org.bukkit.entity.HumanEntity import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil +import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.gui.config.MainConfigGui import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions @@ -46,4 +48,28 @@ class EditConfigExecutor: CASubCommand() { return "Gui to edit the plugin's config" } + private fun allEnchantmentsByName(): Collection{ + val names = mutableSetOf() + for (enchantment in CAEnchantmentRegistry.getInstance().values()) { + names.add(enchantment.name) + names.add(enchantment.key.toString()) + } + + return names + } + + override fun tabCompleter(sender: CommandSender, args: Array, list: MutableList) { + list.addAll(when (args.size) { + 1 -> listOf("item", "enchant", "open") + 2 -> { + when(args[0].lowercase()) { + "enchant" -> allEnchantmentsByName() + else -> listOf() + } + } + else -> listOf() + }) + + } + } From 554fba008ec075f24155245005bb49d7c21a5325 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Tue, 30 Jun 2026 03:36:22 +0200 Subject: [PATCH 06/14] made main command logic --- .../cuanvil/command/EditConfigExecutor.kt | 90 ++++++++++++++----- 1 file changed, 69 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index ac059e9e..4be32bc3 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -1,28 +1,39 @@ package xyz.alexcrea.cuanvil.command -import com.willfp.eco.core.lookup.matches import io.delilaheve.CustomAnvil import org.bukkit.command.Command import org.bukkit.command.CommandSender import org.bukkit.entity.HumanEntity +import xyz.alexcrea.cuanvil.api.EnchantmentApi import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil +import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.gui.config.MainConfigGui import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions -class EditConfigExecutor: CASubCommand() { +class EditConfigExecutor : CASubCommand() { - override fun executeCommand(sender: CommandSender, - cmd: Command, - cmdstr: String, - args: Array): Boolean { + override fun allowed(sender: CommandSender): Boolean { + return sender.hasPermission(CustomAnvil.editConfigPermission) + } + + override fun description(): String { + return "Gui to edit the plugin's config" + } + + override fun executeCommand( + sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array + ): Boolean { if (sender !is HumanEntity) return false if (!allowed(sender)) { sender.sendMessage(GuiGlobalActions.NO_EDIT_PERM) return false } - if(PlatformUtil.isFolia){ + if (PlatformUtil.isFolia) { sender.sendMessage("§cIt look like you are using Folia. Sadly Custom Anvil do not support Config gui for Folia.") sender.sendMessage("§eIt is may come in a future version.") sender.sendMessage("") @@ -35,20 +46,54 @@ class EditConfigExecutor: CASubCommand() { sender.sendMessage("§c/ca gui has been moved to /ca config") } - MainConfigGui.getInstance().show(sender) + if (args.isEmpty()) + processOpen(sender) + else when (args[0].lowercase()) { + "open" -> processOpen(sender) + "enchant" -> processEnchant(sender, args) + "item" -> processItem(sender) + else -> sender.sendMessage("Unknown subcommand \"${args[0]}\"") + } return true } - override fun allowed(sender: CommandSender): Boolean { - return sender.hasPermission(CustomAnvil.editConfigPermission) + private fun processEnchant(sender: HumanEntity, args: Array) { + val enchantToFilter: Set + if (args.size <= 1) { + val item = sender.inventory.itemInMainHand + + enchantToFilter = EnchantmentApi.getEnchantments(item).keys + if (enchantToFilter.isEmpty()) { + sender.sendMessage("No enchantment found in the item you are holding") + return + } + } else { + enchantToFilter = HashSet(EnchantmentApi.getListByName(args[1].lowercase())) + + if (enchantToFilter.isEmpty()) { + sender.sendMessage("No enchantment found with the name \"${args[1]}\"") + return + } + } + + // TODO open enchantments edit gui + } - override fun description(): String { - return "Gui to edit the plugin's config" + private fun processItem(sender: HumanEntity) { + // TODO open item edit gui } - private fun allEnchantmentsByName(): Collection{ + private fun processOpen(sender: HumanEntity) { + MainConfigGui.getInstance().show(sender) + } + + // --------------- + // Tab completer + // --------------- + + private fun allEnchantmentsByName(): Collection { val names = mutableSetOf() for (enchantment in CAEnchantmentRegistry.getInstance().values()) { names.add(enchantment.name) @@ -59,16 +104,19 @@ class EditConfigExecutor: CASubCommand() { } override fun tabCompleter(sender: CommandSender, args: Array, list: MutableList) { - list.addAll(when (args.size) { - 1 -> listOf("item", "enchant", "open") - 2 -> { - when(args[0].lowercase()) { - "enchant" -> allEnchantmentsByName() - else -> listOf() + list.addAll( + when (args.size) { + 1 -> listOf("item", "enchant", "open") + 2 -> { + when (args[0].lowercase()) { + "enchant" -> allEnchantmentsByName() + else -> listOf() + } } + + else -> listOf() } - else -> listOf() - }) + ) } From 25f676f7f3388d9154de83109c3c1a38b3a16436 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Tue, 30 Jun 2026 19:26:52 +0200 Subject: [PATCH 07/14] enchant conflict working --- .../gui/config/global/EnchantConflictGui.java | 15 ++- .../gui/config/global/ItemConfigGui.java | 106 ++++++++++++++++++ .../gui/config/list/ElementListConfigGui.java | 2 +- .../list/MappedElementListConfigGui.java | 26 +++-- .../config/list/MappedGuiListConfigGui.java | 23 ++-- .../cuanvil/command/EditConfigExecutor.kt | 11 +- 6 files changed, 157 insertions(+), 26 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java index 79f52923..3c28e7c5 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java @@ -1,6 +1,7 @@ package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; @@ -24,26 +25,30 @@ public class EnchantConflictGui extends MappedGuiListConfigGui + group.getCantConflictGroup().contain(material) + ); + enchantConflictGui.init(); + } + + return enchantConflictGui; + } +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java index a9e499b0..1386b31d 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java @@ -68,7 +68,7 @@ public abstract class ElementListConfigGui extends ChestGui implements ValueU protected ArrayList pages; protected HashMap pageMap; - public void init() { // Why I'm using an init function ? //TODO determine why is it using a init function and not used on constructor. + public void init() { GuiGlobalItems.addBackgroundItem(this.backgroundPane); this.backgroundPane.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); addPane(this.backgroundPane); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedElementListConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedElementListConfigGui.java index 31ab7184..9b6c6100 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedElementListConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedElementListConfigGui.java @@ -1,6 +1,7 @@ package xyz.alexcrea.cuanvil.gui.config.list; import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import io.delilaheve.CustomAnvil; import org.bukkit.Material; import org.bukkit.entity.HumanEntity; @@ -14,27 +15,32 @@ import java.util.Arrays; import java.util.HashMap; import java.util.function.Consumer; -public abstract class MappedElementListConfigGui< T, S > extends ElementListConfigGui< T > { +public abstract class MappedElementListConfigGui extends ElementListConfigGui { protected final HashMap elementGuiMap; - protected MappedElementListConfigGui(@NotNull String title) { - super(title, MainConfigGui.getInstance()); + + protected MappedElementListConfigGui(@NotNull String title, @NotNull Gui parent) { + super(title, parent); this.elementGuiMap = new HashMap<>(); } + protected MappedElementListConfigGui(@NotNull String title) { + this(title, MainConfigGui.getInstance()); + } + @Override - protected GuiItem prepareCreateNewItem(){ + protected GuiItem prepareCreateNewItem() { // Create new conflict item ItemStack createItem = new ItemStack(Material.PAPER); ItemMeta createMeta = createItem.getItemMeta(); assert createMeta != null; - createMeta.setDisplayName("§aCreate new "+genericDisplayedName()); + createMeta.setDisplayName("§aCreate new " + genericDisplayedName()); createMeta.setLore(Arrays.asList( - "§7Create a new "+genericDisplayedName()+".", - "§7You will be asked to name the "+genericDisplayedName()+" in chat.", - "§7Then, you should edit the "+genericDisplayedName()+" config as you need" + "§7Create a new " + genericDisplayedName() + ".", + "§7You will be asked to name the " + genericDisplayedName() + " in chat.", + "§7Then, you should edit the " + genericDisplayedName() + " config as you need" )); createItem.setItemMeta(createMeta); @@ -51,8 +57,8 @@ public abstract class MappedElementListConfigGui< T, S > extends ElementListConf } player.closeInventory(); - player.sendMessage("§eWrite the "+genericDisplayedName()+" name you want to create in the chat.\n" + - "§eOr write §ccancel §eto go back to "+genericDisplayedName()+" config menu"); + player.sendMessage("§eWrite the " + genericDisplayedName() + " name you want to create in the chat.\n" + + "§eOr write §ccancel §eto go back to " + genericDisplayedName() + " config menu"); CustomAnvil.Companion.getChatListener().setListenedCallback(player, prepareCreateItemConsumer(player)); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java index 18a5f864..8180694e 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MappedGuiListConfigGui.java @@ -1,6 +1,7 @@ package xyz.alexcrea.cuanvil.gui.config.list; import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import io.delilaheve.CustomAnvil; import org.bukkit.entity.HumanEntity; import org.bukkit.event.inventory.InventoryClickEvent; @@ -14,19 +15,22 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Supplier; -public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGui.LazyElement> - extends MappedElementListConfigGui< T, S > { +public abstract class MappedGuiListConfigGui> + extends MappedElementListConfigGui { protected MappedGuiListConfigGui(@NotNull String title) { super(title); + } + protected MappedGuiListConfigGui(@NotNull String title, @NotNull Gui parent) { + super(title, parent); } @Override public void reloadValues() { this.elementGuiMap.forEach((conflict, element) -> { ElementMappedToListGui gui = element.getStored(); - if(gui != null) gui.cleanAndBeUnusable(); + if (gui != null) gui.cleanAndBeUnusable(); }); this.elementGuiMap.clear(); @@ -49,7 +53,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu @Override protected void updateElement(T generic, S element) { ElementMappedToListGui gui = element.getStored(); - if(gui != null) gui.updateLocal(); + if (gui != null) gui.updateLocal(); } @Override @@ -58,7 +62,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu } @Override - protected Consumer prepareCreateItemConsumer(HumanEntity player){ + protected Consumer prepareCreateItemConsumer(HumanEntity player) { AtomicReference> selfRef = new AtomicReference<>(); Consumer selfCallback = (message) -> { if (message == null) return; @@ -71,7 +75,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu message = message.toLowerCase(Locale.ROOT); if ("cancel".equalsIgnoreCase(message)) { - player.sendMessage(genericDisplayedName()+" creation cancelled..."); + player.sendMessage(genericDisplayedName() + " creation cancelled..."); show(player); return; } @@ -82,7 +86,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu // Not the most efficient on large number of conflict, but it should not run often. for (T generic : getDisplayableInstanceOfGeneric()) { if (generic.toString().equalsIgnoreCase(message)) { - player.sendMessage("§cPlease enter a "+genericDisplayedName()+" name that do not already exist..."); + player.sendMessage("§cPlease enter a " + genericDisplayedName() + " name that do not already exist..."); // wait next message. CustomAnvil.Companion.getChatListener().setListenedCallback(player, selfRef.get()); return; @@ -90,7 +94,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu } T generic = createAndSaveNewEmptyGeneric(message); - if(generic == null) {// we don't know what to do. so we back up by opening this gui. + if (generic == null) {// we don't know what to do. so we back up by opening this gui. this.show(player); return; } @@ -117,6 +121,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu private final GuiItem parentItem; private final LazyValue> lazyOpenConsumer; + public LazyElement(GuiItem parentItem, Supplier valueSupplier) { super(valueSupplier); this.parentItem = parentItem; @@ -131,7 +136,7 @@ public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGu } @NotNull - public Consumer openAction(){ + public Consumer openAction() { return event -> lazyOpenConsumer.get().accept(event); } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index 4be32bc3..8aa54a96 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -9,7 +9,10 @@ import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.gui.config.MainConfigGui +import xyz.alexcrea.cuanvil.gui.config.global.ItemConfigGui import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions +import xyz.alexcrea.cuanvil.util.MaterialUtil.customType +import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir class EditConfigExecutor : CASubCommand() { @@ -82,7 +85,13 @@ class EditConfigExecutor : CASubCommand() { } private fun processItem(sender: HumanEntity) { - // TODO open item edit gui + val item = sender.inventory.itemInMainHand + if(item.isAir) { + sender.sendMessage("Cannot configure the item in hand") + return + } + + ItemConfigGui(item.type, item.customType).show(sender) } private fun processOpen(sender: HumanEntity) { From a78f83f6f4b5c08ed37292cfeca833134584c06c Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 00:56:47 +0200 Subject: [PATCH 08/14] item config group config gui --- .../gui/config/global/GroupConfigGui.java | 5 ++++ .../gui/config/global/ItemConfigGui.java | 24 +++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java index fa53174c..7f85278e 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/GroupConfigGui.java @@ -1,6 +1,7 @@ package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; @@ -43,6 +44,10 @@ public class GroupConfigGui extends MappedGuiListConfigGui @@ -103,4 +106,17 @@ public class ItemConfigGui extends ChestGui { return enchantConflictGui; } + + private GroupConfigGui getGroupConfigGui(NamespacedKey material) { + if (groupConfigGui == null) { + groupConfigGui = new GroupConfigGui(this); + groupConfigGui.setFilter(group -> + group.contain(material) + ); + groupConfigGui.init(); + } + + return groupConfigGui; + } + } From 799008f6529237ed7f96fb58560db01a1601cb30 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 02:06:14 +0200 Subject: [PATCH 09/14] reworked unit repair --- .../alexcrea/cuanvil/api/UnitRepairApi.java | 188 ++++++++++++++---- .../alexcrea/cuanvil/util/UnitRepairUtil.kt | 58 +++--- 2 files changed, 172 insertions(+), 74 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java index bc50c162..f5df41bc 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java @@ -3,18 +3,16 @@ package xyz.alexcrea.cuanvil.api; import io.delilaheve.CustomAnvil; import kotlin.Triple; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.dependency.DependencyManager; import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui; -import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui; import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; /** * Custom Anvil api for unit repair. @@ -22,7 +20,8 @@ import java.util.List; @SuppressWarnings("unused") public class UnitRepairApi { - private UnitRepairApi(){} + private UnitRepairApi() { + } private static Object saveChangeTask = null; @@ -35,7 +34,20 @@ public class UnitRepairApi { * @param repairable The item to be repaired. * @return true if successful. */ - public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable){ + public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable) { + return addUnitRepair(unit, repairable, 0.25, false); + } + + /** + * Write and add a custom anvil unit repair recipe. + * Will not write the recipe if it already exists or was deleted. + * Set the value to minecraft default value (0.25 = 25%) + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item to be repaired. + * @return true if successful. + */ + public static boolean addUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable) { return addUnitRepair(unit, repairable, 0.25, false); } @@ -48,7 +60,7 @@ public class UnitRepairApi { * @param value The amount to be repaired by every unit. (1% = 0.01) * @return true if successful. */ - public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value){ + public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value) { return addUnitRepair(unit, repairable, value, false); } @@ -62,12 +74,26 @@ public class UnitRepairApi { * @param overrideDeleted If we should write even if the recipe was previously deleted. * @return true if successful. */ - public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted){ - FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); - String path = unit.name().toLowerCase() + "." + repairable.name().toLowerCase(); + public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted) { + return addUnitRepair(unit.getKey(), repairable.getKey(), value, overrideDeleted); + } - if(!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false; - if(config.contains(path)) return false; + /** + * Write and add a custom anvil unit repair recipe. + * Will not write the recipe if it already exists. + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item to be repaired. + * @param value The amount to be repaired by every unit. (1% = 0.01) + * @param overrideDeleted If we should write even if the recipe was previously deleted. + * @return true if successful. + */ + public static boolean addUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable, double value, boolean overrideDeleted) { + FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); + String path = unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase(); + + if (!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false; + if (config.contains(path)) return false; // Set unit repair return setUnitRepair(unit, repairable, value); @@ -82,11 +108,24 @@ public class UnitRepairApi { * @param value The amount to be repaired by every unit. (1% = 0.01) * @return true if successful. */ - public static boolean setUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value){ + public static boolean setUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value) { + return setUnitRepair(unit.getKey(), repairable.getKey(), value); + } + + /** + * Write and add a custom anvil unit repair recipe. + * Do not check if it previously existed or exist. + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item to be repaired. + * @param value The amount to be repaired by every unit. (1% = 0.01) + * @return true if successful. + */ + public static boolean setUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable, double value) { FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); - String repairableName = repairable.name().toLowerCase(); - String path = unit.name().toLowerCase() + "." + repairableName; + String repairableName = repairable.toString().toLowerCase(); + String path = unit.toString().toLowerCase() + "." + repairableName; // Add to config then prepare save config.set(path, value); @@ -94,10 +133,10 @@ public class UnitRepairApi { // Add to gui UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance(); - if(repairConfigGui != null) { + if (repairConfigGui != null) { UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored(); - if(elementGui != null) elementGui.updateValueForGeneric(repairableName, true); + if (elementGui != null) elementGui.updateValueForGeneric(repairable, true); repairConfigGui.updateValueForGeneric(unit, true); } @@ -111,48 +150,56 @@ public class UnitRepairApi { * @param repairable The item used to be repaired. * @return true if successful. */ - public static boolean removeUnitRepair(@NotNull Material unit, @NotNull Material repairable){ + public static boolean removeUnitRepair(@NotNull Material unit, @NotNull Material repairable) { + return removeUnitRepair(unit.getKey(), repairable.getKey()); + } + + /** + * Remove a custom anvil unit repair recipe. + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item used to be repaired. + * @return true if successful. + */ + public static boolean removeUnitRepair(@NotNull NamespacedKey unit, @NotNull NamespacedKey repairable) { // Delete every possible variation and save to file - String unitName = unit.name(); - String repairableName = repairable.name(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); - config.set(unitName.toLowerCase() + "." + repairableName.toUpperCase(), null); - config.set(unitName.toUpperCase() + "." + repairableName.toLowerCase(), null); - config.set(unitName.toUpperCase() + "." + repairableName.toUpperCase(), null); - config.set(unitName.toLowerCase() + "." + repairableName.toLowerCase(), null); + config.set(unit.getKey() + "." + repairable.getKey(), null); + config.set(unit.getKey() + "." + repairable, null); + config.set(unit + "." + repairable.getKey(), null); + config.set(unit + "." + repairable, null); // Test if it was the last value of this section boolean lastValue = false; - if(config.isConfigurationSection(unitName.toLowerCase())) { - ConfigurationSection section = config.getConfigurationSection(unitName.toLowerCase()); + if (config.isConfigurationSection(unit.toString())) { + ConfigurationSection section = config.getConfigurationSection(unit.toString()); - if(section != null && section.getKeys(false).isEmpty()) { + if (section != null && section.getKeys(false).isEmpty()) { lastValue = true; - config.set(unitName.toLowerCase(), null); + config.set(unit.toString(), null); } - } else if (config.isConfigurationSection(unitName.toUpperCase())) { - ConfigurationSection section = config.getConfigurationSection(unitName.toUpperCase()); - if(section != null && section.getKeys(false).isEmpty()) { + } else if (config.isConfigurationSection(unit.getKey())) { + ConfigurationSection section = config.getConfigurationSection(unit.getKey()); + if (section != null && section.getKeys(false).isEmpty()) { lastValue = true; - config.set(unitName.toUpperCase(), null); + config.set(unit.getKey(), null); } } else lastValue = true; - // We only need to "delete" as the lower case to be counted as deleted - ConfigHolder.UNIT_REPAIR_HOLDER.delete(unitName.toLowerCase() + "." + repairableName.toLowerCase()); + ConfigHolder.UNIT_REPAIR_HOLDER.delete(unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase()); prepareSaveTask(); // Remove from gui UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance(); - if(repairConfigGui != null) { + if (repairConfigGui != null) { UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored(); - if(elementGui != null) elementGui.removeGeneric(repairableName); - if(lastValue){ + if (elementGui != null) elementGui.removeGeneric(repairable); + if (lastValue) { repairConfigGui.removeGeneric(unit); } } @@ -164,9 +211,9 @@ public class UnitRepairApi { * Prepare a task to save custom unit repair recipe configuration. */ private static void prepareSaveTask() { - if(saveChangeTask != null) return; + if (saveChangeTask != null) return; - saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{ + saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> { ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(true); saveChangeTask = null; }); @@ -174,6 +221,7 @@ public class UnitRepairApi { /** * Get every unit repair recipes. + * * @return An immutable collection of unit repair recipes. *

* Each element of the provided triple represent a part of the recipe @@ -182,29 +230,32 @@ public class UnitRepairApi { *

  • Second object is the item to be repaired. *
  • Last object is the amount to be repaired by every unit. (1% = 0.01) * + * @deprecated some may be missing. use {@link #getModernUnitRepairs()} + * */ + @Deprecated @NotNull - public static List> getUnitRepairs(){ + public static List> getUnitRepairs() { List> mutableList = new ArrayList<>(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); for (String unitKey : config.getKeys(false)) { // Test if config section exist - if(!config.isConfigurationSection(unitKey)) continue; + if (!config.isConfigurationSection(unitKey)) continue; // Test if unit is a material Material unit = Material.getMaterial(unitKey.toUpperCase()); - if(unit == null) continue; + if (unit == null) continue; // Iterate over reparable items ConfigurationSection section = config.getConfigurationSection(unitKey); for (String repairableKey : section.getKeys(false)) { // Test if value section exist - if(!section.isDouble(repairableKey)) continue; + if (!section.isDouble(repairableKey)) continue; // Test if repairable is valid a material Material repairable = Material.getMaterial(repairableKey.toUpperCase()); - if(repairable == null) continue; + if (repairable == null) continue; // Add the values mutableList.add(new Triple<>(unit, repairable, section.getDouble(repairableKey))); @@ -215,4 +266,53 @@ public class UnitRepairApi { return Collections.unmodifiableList(mutableList); } + /** + * Get every unit repair recipes. + * + * @return An immutable collection of unit repair recipes. + *

    + *

  • First map contain a key the unit material used to repair the bellow item and value the second map + *
  • Second map contain as key the item to be repaired and as value the amount to be repaired by every unit. (1% = 0.01) + * + */ + @NotNull + public static Map> getModernUnitRepairs() { + Map> mutableList = new HashMap<>(); + + FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); + for (String unitKey : config.getKeys(false)) { + // Test if config section exist + if (!config.isConfigurationSection(unitKey)) continue; + + // Test if unit is a material + NamespacedKey unit = NamespacedKey.fromString(unitKey.toLowerCase()); + if (unit == null) continue; + + Map map; + if (!mutableList.containsKey(unit)) { + map = new HashMap<>(); + mutableList.put(unit, map); + } else { + map = mutableList.get(unit); + } + + // Iterate over reparable items + ConfigurationSection section = config.getConfigurationSection(unitKey); + for (String repairableKey : section.getKeys(false)) { + // Test if value section exist + if (!section.isDouble(repairableKey)) continue; + + // Test if repairable is valid a material + NamespacedKey repairable = NamespacedKey.fromString(repairableKey.toLowerCase()); + if (repairable == null) continue; + + // Add the values + map.put(repairable, section.getDouble(repairableKey)); + + } + } + + return mutableList; + } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt index 8463e27e..85c32f93 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt @@ -1,6 +1,8 @@ package xyz.alexcrea.cuanvil.util +import org.bukkit.NamespacedKey import org.bukkit.configuration.ConfigurationSection +import org.bukkit.configuration.file.FileConfiguration import org.bukkit.inventory.ItemStack import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.util.MaterialUtil.customType @@ -22,40 +24,36 @@ object UnitRepairUtil { ): Double? { if (other == null) return null val config = ConfigHolder.UNIT_REPAIR_HOLDER.config - // Get configuration section if exist - val otherName = other.customType.key.lowercase() - var section = config.getConfigurationSection(otherName) - if (section == null) { - section = config.getConfigurationSection(otherName.uppercase()) - if (section == null) return null - } - // Get repair amount - var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR) - if (userDefault <= 0) { - userDefault = DEFAULT_DEFAULT_UNIT_REPAIR - } + val material = other.customType + val selfType = this.customType - return getRepairAmount(this, section, userDefault) + var result = checkSection(config, material.toString(), selfType) + if (result != null) return result + + result = checkSection(config, material.key, selfType) + if (result != null) return result + + // Get default + val userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR) + if (userDefault <= 0) + return DEFAULT_DEFAULT_UNIT_REPAIR + return userDefault } - /** - * Get the item % repaired by this configuration section of a unit repair. - * null if not found. - * If value is set to less than or equal to 0 then it will be set to default - */ - private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double? { - val itemName = item.customType.key.lowercase() - val repairValue = if (section.isDouble(itemName)) { - section.getDouble(itemName) - } else if (section.isDouble(itemName.uppercase())) { - section.getDouble(itemName.uppercase()) - } else { - return null - } - if (repairValue <= 0) - return default - return repairValue + fun checkSection( + config: FileConfiguration, + path: String, + material: NamespacedKey + ): Double? { + val section = config.getConfigurationSection(path) ?: return null + + if (section.isDouble(material.toString())) + return section.getDouble(material.toString()) + if (section.isDouble(material.key)) + return section.getDouble(material.key) + + return null } } \ No newline at end of file From d1d01f90b834a732a38bcfed3989ccc40082fcb7 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 02:39:14 +0200 Subject: [PATCH 10/14] unit repair gui and internal update --- .../gui/config/global/ItemConfigGui.java | 13 ++- .../config/global/UnitRepairConfigGui.java | 74 ++++++++++------ .../config/list/UnitRepairElementListGui.java | 85 +++++++++++-------- .../xyz/alexcrea/cuanvil/util/MaterialUtil.kt | 3 + .../alexcrea/cuanvil/util/UnitRepairUtil.kt | 25 ++++-- 5 files changed, 129 insertions(+), 71 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java index e2144413..3bc19d5f 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java @@ -2,7 +2,6 @@ package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; -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; @@ -119,4 +118,16 @@ public class ItemConfigGui extends ChestGui { return groupConfigGui; } + /*private UnitRepairConfigGui getUnitRepairConfigGui(NamespacedKey material) { + if (unitRepairConfigGui == null) { + unitRepairConfigGui = new UnitRepairConfigGui(this); + unitRepairConfigGui.setFilter(otherMat -> + group.contain(material) //TODO check material & what inside + ); + unitRepairConfigGui.init(); + } + + return unitRepairConfigGui; + }*/ + } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java index c3e8bcb0..cc4a981f 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java @@ -1,9 +1,10 @@ package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import io.delilaheve.CustomAnvil; import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; @@ -13,24 +14,25 @@ import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui; import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui; import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui; import xyz.alexcrea.cuanvil.util.CasedStringUtil; +import xyz.alexcrea.cuanvil.util.MaterialUtil; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; public class UnitRepairConfigGui extends - MappedGuiListConfigGui> { + MappedGuiListConfigGui> { private static UnitRepairConfigGui INSTANCE; @Nullable - public static UnitRepairConfigGui getCurrentInstance(){ + public static UnitRepairConfigGui getCurrentInstance() { return INSTANCE; } @NotNull - public static UnitRepairConfigGui getInstance(){ - if(INSTANCE == null) INSTANCE = new UnitRepairConfigGui(); + public static UnitRepairConfigGui getInstance() { + if (INSTANCE == null) INSTANCE = new UnitRepairConfigGui(); return INSTANCE; } @@ -41,8 +43,12 @@ public class UnitRepairConfigGui extends init(); } + public UnitRepairConfigGui(Gui parent) { + super("Unit Repair Config", parent); + } + @Override - protected LazyElement newInstanceOfGui(Material material, GuiItem item) { + protected LazyElement newInstanceOfGui(NamespacedKey material, GuiItem item) { return new LazyElement<>(item, () -> { UnitRepairElementListGui element = new UnitRepairElementListGui(material, this); element.init(); @@ -51,23 +57,34 @@ public class UnitRepairConfigGui extends } @Override - protected ItemStack createItemForGeneric(Material material) { - ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(material.name().toLowerCase()); - String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()); + protected ItemStack createItemForGeneric(@NotNull NamespacedKey material) { + var unitConfig = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); + var section = unitConfig.getConfigurationSection(material.toString().toLowerCase()); + var legacySection = unitConfig.getConfigurationSection(material.toString().toLowerCase()); - if(material.isAir()){ - material = Material.BARRIER; + String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.getKey().toLowerCase()); + + var display = MaterialUtil.INSTANCE.getMatFromKey(material); + + if (display == null || display.isAir()) { + display = Material.BARRIER; } - int reparableItemCount = materialSection == null ? 0 : materialSection.getKeys(false).size(); // Probably an expensive call but... why not + var reparable = new HashSet(); + if (section != null) + reparable.addAll(section.getKeys(false)); + if (legacySection != null) + reparable.addAll(legacySection.getKeys(false)); - ItemStack item = new ItemStack(material); + var reparableItemCount = reparable.size(); + + ItemStack item = new ItemStack(display); ItemMeta meta = item.getItemMeta(); assert meta != null; - meta.setDisplayName("§eRepaired by " +materialName); + meta.setDisplayName("§eRepaired by " + materialName); meta.setLore(Arrays.asList( - "§7There is currently §e" +reparableItemCount+ " §7reparable item with "+materialName, + "§7There is currently §e" + reparableItemCount + " §7reparable item with " + materialName, "§7Click here to open the menu to edit reparable item by " + materialName )); @@ -77,13 +94,16 @@ public class UnitRepairConfigGui extends } @Override - protected Collection getEveryInstanceOfGeneric() { - ArrayList materials = new ArrayList<>(); + protected Collection getEveryInstanceOfGeneric() { + var materials = new HashSet(); + var config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); - for (String matName : ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getKeys(false)) { - Material mat = Material.getMaterial(matName.toUpperCase()); - if(mat != null){ - materials.add(mat); + for (String matName : config.getKeys(false)) { + if(!config.isConfigurationSection(matName)) continue; + + NamespacedKey material = NamespacedKey.fromString(matName.toLowerCase()); + if (material != null) { + materials.add(material); } } return materials; @@ -113,7 +133,7 @@ public class UnitRepairConfigGui extends "§7You like to be an unit repair item", this, (itemStack, player) -> { - Material type = itemStack.getType(); + NamespacedKey type = MaterialUtil.INSTANCE.getCustomType(itemStack); // Add new material updateValueForGeneric(type, true); @@ -126,9 +146,9 @@ public class UnitRepairConfigGui extends } @NotNull - public LazyElement getInstanceOrCreate(Material mat){ + public LazyElement getInstanceOrCreate(NamespacedKey mat) { LazyElement element = this.elementGuiMap.get(mat); - if(element == null){ + if (element == null) { updateValueForGeneric(mat, false); element = this.elementGuiMap.get(mat); @@ -141,8 +161,10 @@ public class UnitRepairConfigGui extends protected String genericDisplayedName() { return "this function Should not be used."; } + @Override // Not used in this implementation. - protected Material createAndSaveNewEmptyGeneric(String name) { + protected NamespacedKey createAndSaveNewEmptyGeneric(String name) { return null; } + } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java index c9f2ada2..9b879fdc 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/UnitRepairElementListGui.java @@ -3,12 +3,14 @@ package xyz.alexcrea.cuanvil.gui.config.list; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.HumanEntity; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.meta.Damageable; 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.gui.config.ask.SelectItemTypeGui; import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui; @@ -17,26 +19,25 @@ import xyz.alexcrea.cuanvil.gui.config.settings.DoubleSettingGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.util.CasedStringUtil; +import xyz.alexcrea.cuanvil.util.MaterialUtil; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; +import java.util.*; import java.util.function.Consumer; -public class UnitRepairElementListGui extends SettingGuiListConfigGui implements ElementMappedToListGui { +public class UnitRepairElementListGui extends SettingGuiListConfigGui implements ElementMappedToListGui { - private final Material parentMaterial; + private final NamespacedKey parentMaterial; private final UnitRepairConfigGui parentGui; private final String materialName; private boolean shouldWork = true; - public UnitRepairElementListGui(@NotNull Material parentMaterial, + + public UnitRepairElementListGui(@NotNull NamespacedKey parentMaterial, @NotNull UnitRepairConfigGui parentGui) { - super("§e" + CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase()) + " §rUnit repair"); + super("§e" + CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.getKey().toLowerCase()) + " §rUnit repair"); this.parentMaterial = parentMaterial; this.parentGui = parentGui; - this.materialName = CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase()); + this.materialName = CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.getKey().toLowerCase()); GuiGlobalItems.addBackItem(this.backgroundPane, parentGui); } @@ -54,7 +55,7 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui getCreateClickConsumer() { return event -> { event.setCancelled(true); - if(!this.shouldWork){ + if (!this.shouldWork) { return; } event.setCancelled(true); @@ -66,28 +67,28 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui { ItemMeta meta = itemStack.getItemMeta(); - Material type = itemStack.getType(); + NamespacedKey type = MaterialUtil.INSTANCE.getCustomType(itemStack); - if(!(meta instanceof Damageable) || (type.getMaxDurability() <= 0)) { + if (!(meta instanceof Damageable)) { player.sendMessage("§cThis item can't be damaged, so it can't be repaired."); return; } - if(type == this.parentMaterial){ + if (type.equals(this.parentMaterial)) { player.sendMessage("§cItem can't repair something of the same type."); return; } - String materialName = type.name().toLowerCase(); + String materialName = type.toString(); // Add new material - ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().set(parentMaterial.name().toLowerCase() + "." + materialName,0.25); + ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().set(parentMaterial.toString().toLowerCase() + "." + materialName, 0.25); if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } // Update gui - updateValueForGeneric(materialName, true); + updateValueForGeneric(type, true); this.parentGui.updateValueForGeneric(this.parentMaterial, true); @@ -106,17 +107,17 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui getEveryInstanceOfGeneric() { - ArrayList keys = new ArrayList<>(); - if(!this.shouldWork){ + protected Collection getEveryInstanceOfGeneric() { + Set keys = new HashSet<>(); + if (!this.shouldWork) { return keys; } - ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.name().toLowerCase()); - if(materialSection == null){ - return keys; - } - keys.addAll(materialSection.getKeys(false)); + ConfigurationSection legacySection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.getKey().toLowerCase()); + ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.toString().toLowerCase()); + + addAllKeys(legacySection, keys); + addAllKeys(materialSection, keys); + return keys; } - private Material materialFromName(String materialName){ - Material mat = Material.getMaterial(materialName.toUpperCase()); - if(mat == null || mat.isAir()) return Material.BARRIER; + private void addAllKeys(@Nullable ConfigurationSection section, @NotNull Set keys) { + if (section == null) return; + for (var key : section.getKeys(false)) { + var material = NamespacedKey.fromString(key); + if (material == null) continue; + + keys.add(material); + } + } + + private Material materialFromName(NamespacedKey material) { + Material mat = MaterialUtil.INSTANCE.getMatFromKey(material); + if (mat == null || mat.isAir()) return Material.BARRIER; return mat; } @@ -163,7 +175,8 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui 0) return result // Get default val userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR) @@ -41,6 +36,20 @@ object UnitRepairUtil { return userDefault } + private fun findRepairValue( + self: ItemStack, + other: ItemStack, + config: FileConfiguration + ): Double? { + val material = other.customType + val selfType = self.customType + + val result = checkSection(config, material.toString(), selfType) + if (result != null) return result + + return checkSection(config, material.key, selfType) + } + fun checkSection( config: FileConfiguration, path: String, From 6178282386451a2433b44fe87c660fce6b22a9f3 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 10:29:10 +0200 Subject: [PATCH 11/14] append new unit repair as minecraft namespace --- .../cuanvil/update/minecraft/Update_1_21.java | 1 - .../update/minecraft/Update_1_21_11.java | 36 +++++++++---------- .../update/minecraft/Update_1_21_9.java | 18 +++++----- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21.java b/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21.java index 3aa60730..bbe35589 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21.java +++ b/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21.java @@ -1,6 +1,5 @@ package xyz.alexcrea.cuanvil.update.minecraft; -import io.delilaheve.CustomAnvil; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.update.UpdateUtils; import xyz.alexcrea.cuanvil.update.Version; diff --git a/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_11.java b/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_11.java index d6395966..6dd8b5ba 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_11.java +++ b/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_11.java @@ -38,26 +38,26 @@ public class Update_1_21_11 extends MCUpdate{ addAbsentToList(conflictConfig, "restriction_fire_aspect.notAffectedGroups", "spears"); // Unit repair for spears - unitConfig.set("gold_ingot.golden_spear", 0.25); - unitConfig.set("copper_ingot.copper_spear", 0.25); - unitConfig.set("iron_ingot.iron_spear", 0.25); - unitConfig.set("diamond.diamond_spear", 0.25); - unitConfig.set("netherite_ingot.netherite_spear", 0.25); + unitConfig.set("minecraft:gold_ingot.minecraft:golden_spear", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_spear", 0.25); + unitConfig.set("minecraft:iron_ingot.minecraft:iron_spear", 0.25); + unitConfig.set("minecraft:diamond.minecraft:diamond_spear", 0.25); + unitConfig.set("minecraft:netherite_ingot.minecraft:netherite_spear", 0.25); - unitConfig.set("cobblestone.stone_spear", 0.25); - unitConfig.set("cobbled_deepslate.stone_spear", 0.25); + unitConfig.set("minecraft:cobblestone.stone_spear", 0.25); + unitConfig.set("minecraft:cobbled_deepslate.stone_spear", 0.25); - unitConfig.set("oak_planks.wooden_spear", 0.25); - unitConfig.set("spruce_planks.wooden_spear", 0.25); - unitConfig.set("birch_planks.wooden_spear", 0.25); - unitConfig.set("jungle_planks.wooden_spear", 0.25); - unitConfig.set("acacia_planks.wooden_spear", 0.25); - unitConfig.set("dark_oak_planks.wooden_spear", 0.25); - unitConfig.set("mangrove_planks.wooden_spear", 0.25); - unitConfig.set("cherry_planks.wooden_spear", 0.25); - unitConfig.set("bamboo_planks.wooden_spear", 0.25); - unitConfig.set("crimson_planks.wooden_spear", 0.25); - unitConfig.set("warped_planks.wooden_spear", 0.25); + unitConfig.set("minecraft:oak_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:spruce_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:birch_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:jungle_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:acacia_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:dark_oak_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:mangrove_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:cherry_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:bamboo_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:crimson_planks.minecraft:wooden_spear", 0.25); + unitConfig.set("minecraft:warped_planks.minecraft:wooden_spear", 0.25); // Create lunge enchant value and group baseConfig.set("enchant_limits.minecraft:lunge", 3); diff --git a/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_9.java b/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_9.java index d289b9bf..ec464ba1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_9.java +++ b/src/main/java/xyz/alexcrea/cuanvil/update/minecraft/Update_1_21_9.java @@ -49,16 +49,16 @@ public class Update_1_21_9 extends MCUpdate{ public static void addCopperUnitRepair(FileConfiguration unitConfig) { // Add unit repair - unitConfig.set("copper_ingot.copper_helmet", 0.25); - unitConfig.set("copper_ingot.copper_chestplate", 0.25); - unitConfig.set("copper_ingot.copper_leggings", 0.25); - unitConfig.set("copper_ingot.copper_boots", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_helmet", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_chestplate", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_leggings", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_boots", 0.25); - unitConfig.set("copper_ingot.copper_pickaxe", 0.25); - unitConfig.set("copper_ingot.copper_shovel", 0.25); - unitConfig.set("copper_ingot.copper_hoe", 0.25); - unitConfig.set("copper_ingot.copper_axe", 0.25); - unitConfig.set("copper_ingot.copper_sword", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_pickaxe", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_shovel", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_hoe", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_axe", 0.25); + unitConfig.set("minecraft:copper_ingot.minecraft:copper_sword", 0.25); } } From bc9ac1cc138dbe232f25b70602b2f7df770f407e Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 13:21:18 +0200 Subject: [PATCH 12/14] add unit repair to item config --- .../cuanvil/gui/config/global/ItemConfigGui.java | 14 ++++++++------ .../gui/config/global/UnitRepairConfigGui.java | 2 +- .../xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java index 3bc19d5f..35633289 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java @@ -9,8 +9,10 @@ import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.util.UnitRepairUtil; import java.util.Collections; @@ -40,7 +42,7 @@ public class ItemConfigGui extends ChestGui { displayMeta.setDisplayName("§aConfiguring " + material); displayItemstack.setItemMeta(displayMeta); - pane.bindItem('D', new GuiItem(displayItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); + pane.bindItem('D', new GuiItem(displayItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); // Enchantment Conflicts item ItemStack enchantConflictItemstack = new ItemStack(Material.OAK_FENCE); @@ -76,8 +78,7 @@ public class ItemConfigGui extends ChestGui { unitRepairMeta.setLore(Collections.singletonList("§7Click here to open anvil unit repair menu")); unirRepairItemstack.setItemMeta(unitRepairMeta); - //TODO - GuiItem unitRepairItem = GuiGlobalItems.goToGuiItem(unirRepairItemstack, UnitRepairConfigGui.getInstance()); + GuiItem unitRepairItem = GuiGlobalItems.goToGuiItem(unirRepairItemstack, getUnitRepairConfigGui(material)); pane.bindItem('7', unitRepairItem); // Custom recipe item @@ -118,16 +119,17 @@ public class ItemConfigGui extends ChestGui { return groupConfigGui; } - /*private UnitRepairConfigGui getUnitRepairConfigGui(NamespacedKey material) { + private UnitRepairConfigGui getUnitRepairConfigGui(NamespacedKey material) { if (unitRepairConfigGui == null) { unitRepairConfigGui = new UnitRepairConfigGui(this); unitRepairConfigGui.setFilter(otherMat -> - group.contain(material) //TODO check material & what inside + otherMat.equals(material) || UnitRepairUtil.INSTANCE.findRawRepairValue( + material, otherMat, ConfigHolder.UNIT_REPAIR_HOLDER.getConfig()) != null ); unitRepairConfigGui.init(); } return unitRepairConfigGui; - }*/ + } } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java index cc4a981f..54c4ce13 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/UnitRepairConfigGui.java @@ -60,7 +60,7 @@ public class UnitRepairConfigGui extends protected ItemStack createItemForGeneric(@NotNull NamespacedKey material) { var unitConfig = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); var section = unitConfig.getConfigurationSection(material.toString().toLowerCase()); - var legacySection = unitConfig.getConfigurationSection(material.toString().toLowerCase()); + var legacySection = unitConfig.getConfigurationSection(material.getKey().toLowerCase()); String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.getKey().toLowerCase()); diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt index 642a047d..5d059870 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt @@ -25,7 +25,7 @@ object UnitRepairUtil { if (other == null) return null val config = ConfigHolder.UNIT_REPAIR_HOLDER.config - val result = findRepairValue(this, other, config) ?: return null + val result = findRawRepairValue(this, other, config) ?: return null if(result > 0) return result @@ -36,7 +36,7 @@ object UnitRepairUtil { return userDefault } - private fun findRepairValue( + private fun findRawRepairValue( self: ItemStack, other: ItemStack, config: FileConfiguration @@ -50,6 +50,17 @@ object UnitRepairUtil { return checkSection(config, material.key, selfType) } + fun findRawRepairValue( + self: NamespacedKey, + other: NamespacedKey, + config: FileConfiguration + ): Double? { + val result = checkSection(config, other.toString(), self) + if (result != null) return result + + return checkSection(config, other.key, self) + } + fun checkSection( config: FileConfiguration, path: String, From 8ea1db382f7dbace0f790d2d68443859be1e0f19 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 13:35:46 +0200 Subject: [PATCH 13/14] add custom recipe filter --- .../config/global/CustomRecipeConfigGui.java | 5 +++ .../gui/config/global/ItemConfigGui.java | 35 +++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/CustomRecipeConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/CustomRecipeConfigGui.java index ba094126..aafb87f8 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/CustomRecipeConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/CustomRecipeConfigGui.java @@ -1,6 +1,7 @@ package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import org.bukkit.Material; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; @@ -40,6 +41,10 @@ public class CustomRecipeConfigGui extends MappedGuiListConfigGui @@ -107,7 +109,7 @@ public class ItemConfigGui extends ChestGui { return enchantConflictGui; } - private GroupConfigGui getGroupConfigGui(NamespacedKey material) { + private GroupConfigGui getGroupConfigGui(@NotNull NamespacedKey material) { if (groupConfigGui == null) { groupConfigGui = new GroupConfigGui(this); groupConfigGui.setFilter(group -> @@ -119,7 +121,7 @@ public class ItemConfigGui extends ChestGui { return groupConfigGui; } - private UnitRepairConfigGui getUnitRepairConfigGui(NamespacedKey material) { + private UnitRepairConfigGui getUnitRepairConfigGui(@NotNull NamespacedKey material) { if (unitRepairConfigGui == null) { unitRepairConfigGui = new UnitRepairConfigGui(this); unitRepairConfigGui.setFilter(otherMat -> @@ -132,4 +134,25 @@ public class ItemConfigGui extends ChestGui { return unitRepairConfigGui; } + private CustomRecipeConfigGui getCustomRecipeConfigGui(@NotNull NamespacedKey material) { + if (customRecipeConfigGui == null) { + customRecipeConfigGui = new CustomRecipeConfigGui(this); + customRecipeConfigGui.setFilter(recipe -> { + if(isMaterial(recipe.getLeftItem(), material)) return true; + if(isMaterial(recipe.getRightItem(), material)) return true; + return isMaterial(recipe.getResultItem(), material); + } + ); + customRecipeConfigGui.init(); + } + + return customRecipeConfigGui; + } + + private boolean isMaterial(@Nullable ItemStack item, @NotNull NamespacedKey material) { + if(item == null) return false; + + return material.equals(MaterialUtil.INSTANCE.getCustomType(item)); + } + } From 23c7b0c75346b5abe8854f63f42f68595bd1fe81 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 1 Jul 2026 13:40:06 +0200 Subject: [PATCH 14/14] disable create item if has filter --- .../cuanvil/gui/config/global/ItemConfigGui.java | 3 ++- .../cuanvil/gui/config/list/ElementListConfigGui.java | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java index 063e7cd0..456e7e03 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/ItemConfigGui.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.MaterialUtil; import xyz.alexcrea.cuanvil.util.UnitRepairUtil; @@ -27,7 +28,7 @@ public class ItemConfigGui extends ChestGui { public CustomRecipeConfigGui customRecipeConfigGui; public ItemConfigGui(@NotNull Material display, @NotNull NamespacedKey material) { - super(3, material.getKey() + " Config", CustomAnvil.instance); + super(3, CasedStringUtil.snakeToUpperSpacedCase(material.getKey().toLowerCase()) + " Config", CustomAnvil.instance); Pattern pattern = new Pattern( "0000D0000", diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java index 1386b31d..19d31d9f 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/ElementListConfigGui.java @@ -37,6 +37,7 @@ public abstract class ElementListConfigGui extends ChestGui implements ValueU protected PatternPane backgroundPane; private Predicate filter = (t) -> true; + private boolean hasDefaultFilter = true; protected ElementListConfigGui(@NotNull String title, Gui parent) { super(6, title, CustomAnvil.instance); @@ -51,6 +52,7 @@ public abstract class ElementListConfigGui extends ChestGui implements ValueU public void setFilter(Predicate filter) { this.filter = filter; + this.hasDefaultFilter = false; } protected Pattern getBackgroundPattern() { @@ -119,7 +121,12 @@ public abstract class ElementListConfigGui extends ChestGui implements ValueU viewer.setItemOnCursor(cursor); }, CustomAnvil.instance); - GuiItem createNew = prepareCreateNewItem(); + GuiItem createNew; + if(hasDefaultFilter) + createNew = prepareCreateNewItem(); + else + createNew = GuiSharedConstant.SECONDARY_BACKGROUND_ITEM; + if (createNew != null) { this.backgroundPane.bindItem('C', createNew); }