Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/maven.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -260,4 +296,4 @@ public ConfigEnchantmentEntry enchant(final Enchantment enchantment) {
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ public Optional<ConfigEnchantmentEntry> 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<ConfigEnchantmentEntry.AllConfigEnchantmentEntry> 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();
}

/**
Expand All @@ -64,6 +76,10 @@ public Optional<ConfigEnchantmentEntry> 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<ConfigEnchantmentEntry> enchants;

try {
Expand Down Expand Up @@ -100,4 +116,4 @@ public void onDisable() {
allConfigEnchantment = null;
configEnchantments.clear();
}
}
}
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
Loading