diff --git a/.github/workflows/maven.yaml b/.github/workflows/maven.yaml index 3cd9887..73400c2 100644 --- a/.github/workflows/maven.yaml +++ b/.github/workflows/maven.yaml @@ -35,4 +35,5 @@ jobs: # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive - name: Submit Dependency Snapshot + if: github.event.pull_request.head.repo.full_name == github.repository uses: advanced-security/maven-dependency-submission-action@v5 diff --git a/src/main/java/pro/cloudnode/smp/enchantbookplus/ConfigEnchantmentEntry.java b/src/main/java/pro/cloudnode/smp/enchantbookplus/ConfigEnchantmentEntry.java index a305ac3..55d442b 100644 --- a/src/main/java/pro/cloudnode/smp/enchantbookplus/ConfigEnchantmentEntry.java +++ b/src/main/java/pro/cloudnode/smp/enchantbookplus/ConfigEnchantmentEntry.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.Objects; import java.util.OptionalInt; +import java.util.logging.Level; import java.util.stream.Collectors; @NullMarked @@ -40,6 +41,21 @@ public class ConfigEnchantmentEntry { */ protected final boolean multiplyCostByLevel; + /** + * Whether to allow custom enchantments from other plugins. + * Set this from the main plugin class. + */ + private static boolean allowCustomEnchantments = true; + + /** + * Set whether custom enchantments are allowed. + * + * @param allow true to allow custom enchantments, false to only use vanilla enchantments + */ + public static void setAllowCustomEnchantments(final boolean allow) { + allowCustomEnchantments = allow; + } + /** * @param name Name of the enchantment. * @param maxLevel Maximum level of the enchantment. @@ -194,8 +210,13 @@ public final OptionalInt getMaxLevel() { return OptionalInt.empty(); } + final Enchantment enchantment = getEnchantment(); + if (enchantment == null) { + return OptionalInt.empty(); + } + if (maxLevelRelative) { - return OptionalInt.of(getEnchantment().getMaxLevel() + maxLevel); + return OptionalInt.of(enchantment.getMaxLevel() + maxLevel); } return OptionalInt.of(maxLevel); @@ -218,8 +239,12 @@ public final boolean getMultiplyCostByLevel() { /** * Get enchantment */ - public final Enchantment getEnchantment() { - return Objects.requireNonNull(Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name))); + public final @Nullable Enchantment getEnchantment() { + final Enchantment enchantment = Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)); + if (enchantment == null) { + java.util.logging.Logger.getLogger("EnchantBookPlus").warning("Enchantment '" + name + "' not found in registry"); + } + return enchantment; } /** @@ -250,7 +275,18 @@ public static AllConfigEnchantmentEntry from(final ConfigEnchantmentEntry config ); } - public ConfigEnchantmentEntry enchant(final Enchantment enchantment) { + /** + * Create a ConfigEnchantmentEntry for a specific enchantment. + * Returns null if the enchantment is custom and custom enchantments are disabled. + * + * @param enchantment The enchantment to create an entry for + * @return A new ConfigEnchantmentEntry, or null if skipped + */ + public @Nullable ConfigEnchantmentEntry enchant(final Enchantment enchantment) { + // Skip custom enchantments if they are not allowed + if (!allowCustomEnchantments && !enchantment.getKey().getNamespace().equals(NamespacedKey.MINECRAFT)) { + return null; + } return new ConfigEnchantmentEntry( enchantment.getKey().getKey(), this.maxLevel, @@ -260,4 +296,4 @@ public ConfigEnchantmentEntry enchant(final Enchantment enchantment) { ); } } -} +} \ No newline at end of file diff --git a/src/main/java/pro/cloudnode/smp/enchantbookplus/EnchantBookPlus.java b/src/main/java/pro/cloudnode/smp/enchantbookplus/EnchantBookPlus.java index 695d27e..f796db8 100644 --- a/src/main/java/pro/cloudnode/smp/enchantbookplus/EnchantBookPlus.java +++ b/src/main/java/pro/cloudnode/smp/enchantbookplus/EnchantBookPlus.java @@ -55,7 +55,19 @@ public Optional getConfigEnchantment(final Enchantment e .filter(c -> c.isEnchantment(enchantment)) .findFirst(); - return entry.isEmpty() ? getAllConfigEnchantment().map(a -> a.enchant(enchantment)) : entry; + if (entry.isPresent()) { + return entry; + } + + // If "ALL" entry exists, create a specific entry for this enchantment + final Optional allEntry = getAllConfigEnchantment(); + if (allEntry.isPresent()) { + final ConfigEnchantmentEntry specificEntry = allEntry.get().enchant(enchantment); + // If the enchantment was skipped (null), return empty + return Optional.ofNullable(specificEntry); + } + + return Optional.empty(); } /** @@ -64,6 +76,10 @@ public Optional getConfigEnchantment(final Enchantment e void reload() { reloadConfig(); + // Load the allow-custom-enchantments setting from config + final boolean allowCustom = getConfig().getBoolean("allow-custom-enchantments", true); + ConfigEnchantmentEntry.setAllowCustomEnchantments(allowCustom); + final List enchants; try { @@ -100,4 +116,4 @@ public void onDisable() { allConfigEnchantment = null; configEnchantments.clear(); } -} +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 80ee22a..ada5b85 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,3 +1,8 @@ +# Enable or disable processing of custom enchantments from other plugins (e.g., ExcellentEnchants) +# If disabled, only vanilla Minecraft enchantments will be affected by the "ALL" rule +# Set to true to allow custom enchantments, false to only use vanilla enchantments +allow-custom-enchantments: false + # List of enchantments for which to enable upgrading above the vanilla max levels using an anvil. enchantments: # Enchantment name (same as in the /enchant command).