From 3752a9e31a9ce3bcdcbf7819a14f7f205366e49a Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Mon, 28 Oct 2024 16:01:51 +0100 Subject: [PATCH] refactor to allow adding enchants to arbitrary tags --- .../enchantio/EnchantioBootstrap.java | 22 ++++++---------- .../enchantio/enchants/BeheadingEnchant.java | 21 ++++++++++----- .../enchantio/enchants/EnchantioEnchant.java | 10 ++++--- .../enchants/ExecutionerEnchant.java | 25 +++++++++--------- .../enchantio/enchants/ReplantingEnchant.java | 26 +++++++++---------- .../enchantio/enchants/SmeltingEnchant.java | 26 +++++++++---------- .../enchantio/enchants/SoulboundEnchant.java | 26 +++++++++---------- .../enchantio/enchants/TelepathyEnchant.java | 26 +++++++++---------- 8 files changed, 93 insertions(+), 89 deletions(-) diff --git a/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java b/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java index eb3c5d5..4439d21 100644 --- a/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java +++ b/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java @@ -6,16 +6,12 @@ import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.TypedKey; import io.papermc.paper.registry.event.RegistryEvents; -import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys; -import io.papermc.paper.tag.TagEntry; import me.youhavetrouble.enchantio.enchants.EnchantioEnchant; -import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.util.Collection; -import java.util.HashSet; import java.util.Set; import java.util.logging.Logger; @@ -26,19 +22,18 @@ public class EnchantioBootstrap implements PluginBootstrap { @Override public void bootstrap(@NotNull BootstrapContext context) { - EnchantioConfig config; try { - config = new EnchantioConfig(context.getDataDirectory(), logger); + new EnchantioConfig(context.getDataDirectory(), logger); } catch (IOException e) { throw new RuntimeException(e); } Collection enchantioEnchants = EnchantioConfig.ENCHANTS.values(); - logger.info("Registering supported item tags"); + logger.fine("Registering supported item tags"); context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM).newHandler((event) -> { for (EnchantioEnchant enchant : enchantioEnchants) { - logger.info("Registering item tag " + enchant.getTagForSupportedItems().key()); + logger.fine("Registering item tag " + enchant.getTagForSupportedItems().key()); event.registrar().addToTag( ItemTypeTagKeys.create(enchant.getTagForSupportedItems().key()), enchant.getSupportedItems() @@ -48,7 +43,7 @@ public class EnchantioBootstrap implements PluginBootstrap { context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.freeze().newHandler(event -> { for (EnchantioEnchant enchant : enchantioEnchants) { - logger.info("Registering enchantment " + enchant.getKey()); + logger.fine("Registering enchantment " + enchant.getKey()); event.registry().register(TypedKey.create(RegistryKey.ENCHANTMENT, enchant.getKey()), enchantment -> { enchantment.description(enchant.getDescription()); enchantment.anvilCost(enchant.getAnvilCost()); @@ -63,13 +58,12 @@ public class EnchantioBootstrap implements PluginBootstrap { })); context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ENCHANTMENT).newHandler((event) -> { - Set> enchantTags = new HashSet<>(enchantioEnchants.size()); for (EnchantioEnchant enchant : enchantioEnchants) { - if (!enchant.canGetFromEnchantingTable()) continue; - logger.info("Registering enchantment " + enchant.getKey() + " to enchanting table possibilities"); - enchantTags.add(enchant.getTagEntry()); + enchant.getEnchantTagKeys().forEach(enchantmentTagKey -> { + logger.fine("Registering enchantment tag " + enchantmentTagKey.key()); + event.registrar().addToTag(enchantmentTagKey, Set.of(enchant.getTagEntry())); + }); } - event.registrar().addToTag(EnchantmentTagKeys.IN_ENCHANTING_TABLE, enchantTags); })); } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/BeheadingEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/BeheadingEnchant.java index 7575c04..9cb5b1b 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/BeheadingEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/BeheadingEnchant.java @@ -2,13 +2,17 @@ package me.youhavetrouble.enchantio.enchants; import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.tag.TagKey; import io.papermc.paper.tag.TagEntry; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.ItemType; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; @SuppressWarnings("UnstableApiUsage") @@ -19,9 +23,9 @@ public class BeheadingEnchant implements EnchantioEnchant { private final int anvilCost, weight, maxLevel; private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; - private final boolean canGetFromEnchantingTable; private final Set> supportedItemTags; private final double chanceToDropHeadPerLevel; + private final Set> enchantTagKeys = new HashSet<>(); public BeheadingEnchant( int anvilCost, @@ -37,10 +41,13 @@ public class BeheadingEnchant implements EnchantioEnchant { this.weight = weight; this.minimumCost = minimumCost; this.maximumCost = maximumCost; - this.canGetFromEnchantingTable = canGetFromEnchantingTable; this.supportedItemTags = supportedItemTags; this.maxLevel = maxLevel; this.chanceToDropHeadPerLevel = chanceToDropHeadPerLevel; + + if (canGetFromEnchantingTable) { + enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE); + } } @Override @@ -87,11 +94,6 @@ public class BeheadingEnchant implements EnchantioEnchant { return Set.of(EquipmentSlotGroup.ANY); } - @Override - public boolean canGetFromEnchantingTable() { - return canGetFromEnchantingTable; - } - @Override public TagKey getTagForSupportedItems() { return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:beheading_enchantable")); @@ -102,4 +104,9 @@ public class BeheadingEnchant implements EnchantioEnchant { return supportedItemTags; } + @Override + public Set> getEnchantTagKeys() { + return Collections.unmodifiableSet(enchantTagKeys); + } + } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java index 4e5c295..d2c4e0c 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java @@ -32,12 +32,14 @@ public interface EnchantioEnchant { Iterable getActiveSlots(); - boolean canGetFromEnchantingTable(); - - TagKey getTagForSupportedItems(); - Set> getSupportedItems(); + Set> getEnchantTagKeys(); + + default TagKey getTagForSupportedItems() { + return TagKey.create(RegistryKey.ITEM, Key.key( getKey().asString() + "_enchantable")); + } + default TagEntry getTagEntry() { return TagEntry.valueEntry(TypedKey.create(RegistryKey.ENCHANTMENT, getKey())); } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/ExecutionerEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/ExecutionerEnchant.java index 026fc80..405aa35 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/ExecutionerEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/ExecutionerEnchant.java @@ -2,13 +2,17 @@ package me.youhavetrouble.enchantio.enchants; import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.tag.TagKey; import io.papermc.paper.tag.TagEntry; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.ItemType; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; @SuppressWarnings("UnstableApiUsage") @@ -19,9 +23,9 @@ public class ExecutionerEnchant implements EnchantioEnchant { private final int anvilCost, weight, maxLevel; private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; - private final boolean canGetFromEnchantingTable; private final Set> supportedItemTags; private final double damageMultiplierPerLevel, maxDamageHpThreshold; + private final Set> enchantTagKeys = new HashSet<>(); public ExecutionerEnchant( @@ -39,11 +43,13 @@ public class ExecutionerEnchant implements EnchantioEnchant { this.weight = weight; this.minimumCost = minimumCost; this.maximumCost = maximumCost; - this.canGetFromEnchantingTable = canGetFromEnchantingTable; this.supportedItemTags = supportedItemTags; this.maxLevel = maxLevel; this.damageMultiplierPerLevel = damageMultiplierPerLevel; this.maxDamageHpThreshold = maxDamageHpThreshold; + if (canGetFromEnchantingTable) { + enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE); + } } @Override @@ -94,19 +100,14 @@ public class ExecutionerEnchant implements EnchantioEnchant { return Set.of(EquipmentSlotGroup.HAND); } - @Override - public boolean canGetFromEnchantingTable() { - return canGetFromEnchantingTable; - } - - @Override - public TagKey getTagForSupportedItems() { - return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:executioner_enchantable")); - } - @Override public Set> getSupportedItems() { return supportedItemTags; } + @Override + public Set> getEnchantTagKeys() { + return Collections.unmodifiableSet(enchantTagKeys); + } + } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java index bf2ea78..d2e8da8 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java @@ -1,14 +1,17 @@ package me.youhavetrouble.enchantio.enchants; -import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.tag.TagKey; import io.papermc.paper.tag.TagEntry; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.ItemType; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; @SuppressWarnings("UnstableApiUsage") @@ -19,8 +22,8 @@ public class ReplantingEnchant implements EnchantioEnchant { private final int anvilCost, weight; private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; - private final boolean canGetFromEnchantingTable; private final Set> supportedItemTags; + private final Set> enchantTagKeys = new HashSet<>(); public ReplantingEnchant( int anvilCost, @@ -34,8 +37,10 @@ public class ReplantingEnchant implements EnchantioEnchant { this.weight = weight; this.minimumCost = minimumCost; this.maximumCost = maximumCost; - this.canGetFromEnchantingTable = canGetFromEnchantingTable; this.supportedItemTags = supportedItemTags; + if (canGetFromEnchantingTable) { + enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE); + } } @Override @@ -78,19 +83,14 @@ public class ReplantingEnchant implements EnchantioEnchant { return Set.of(EquipmentSlotGroup.HAND); } - @Override - public boolean canGetFromEnchantingTable() { - return canGetFromEnchantingTable; - } - - @Override - public TagKey getTagForSupportedItems() { - return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:replanting_enchantable")); - } - @Override public Set> getSupportedItems() { return supportedItemTags; } + @Override + public Set> getEnchantTagKeys() { + return Collections.unmodifiableSet(enchantTagKeys); + } + } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/SmeltingEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/SmeltingEnchant.java index d3e37d5..00be93c 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/SmeltingEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/SmeltingEnchant.java @@ -1,14 +1,17 @@ package me.youhavetrouble.enchantio.enchants; -import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.tag.TagKey; import io.papermc.paper.tag.TagEntry; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.ItemType; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; @SuppressWarnings("UnstableApiUsage") @@ -19,8 +22,8 @@ public class SmeltingEnchant implements EnchantioEnchant { private final int anvilCost, weight; private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; - private final boolean canGetFromEnchantingTable; private final Set> supportedItemTags; + private final Set> enchantTagKeys = new HashSet<>(); public SmeltingEnchant( int anvilCost, @@ -34,8 +37,10 @@ public class SmeltingEnchant implements EnchantioEnchant { this.weight = weight; this.minimumCost = minimumCost; this.maximumCost = maximumCost; - this.canGetFromEnchantingTable = canGetFromEnchantingTable; this.supportedItemTags = supportedItemTags; + if (canGetFromEnchantingTable) { + enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE); + } } @Override @@ -78,19 +83,14 @@ public class SmeltingEnchant implements EnchantioEnchant { return Set.of(EquipmentSlotGroup.ANY); } - @Override - public boolean canGetFromEnchantingTable() { - return canGetFromEnchantingTable; - } - - @Override - public TagKey getTagForSupportedItems() { - return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:smelting_enchantable")); - } - @Override public Set> getSupportedItems() { return supportedItemTags; } + @Override + public Set> getEnchantTagKeys() { + return Collections.unmodifiableSet(enchantTagKeys); + } + } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java index 063415c..66db140 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java @@ -1,14 +1,17 @@ package me.youhavetrouble.enchantio.enchants; -import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.tag.TagKey; import io.papermc.paper.tag.TagEntry; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.ItemType; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; @SuppressWarnings("UnstableApiUsage") @@ -19,8 +22,8 @@ public class SoulboundEnchant implements EnchantioEnchant { private final int anvilCost, weight; private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; - private final boolean canGetFromEnchantingTable; private final Set> supportedItemTags; + private final Set> enchantTagKeys = new HashSet<>(); public SoulboundEnchant( int anvilCost, @@ -34,8 +37,10 @@ public class SoulboundEnchant implements EnchantioEnchant { this.weight = weight; this.minimumCost = minimumCost; this.maximumCost = maximumCost; - this.canGetFromEnchantingTable = canGetFromEnchantingTable; this.supportedItemTags = supportedItemTags; + if (canGetFromEnchantingTable) { + enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE); + } } @Override @@ -78,19 +83,14 @@ public class SoulboundEnchant implements EnchantioEnchant { return Set.of(EquipmentSlotGroup.ANY); } - @Override - public boolean canGetFromEnchantingTable() { - return canGetFromEnchantingTable; - } - - @Override - public TagKey getTagForSupportedItems() { - return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:soulbound_enchantable")); - } - @Override public Set> getSupportedItems() { return supportedItemTags; } + @Override + public Set> getEnchantTagKeys() { + return Collections.unmodifiableSet(enchantTagKeys); + } + } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java index c8963c8..c6ad886 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java @@ -1,14 +1,17 @@ package me.youhavetrouble.enchantio.enchants; -import io.papermc.paper.registry.RegistryKey; import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.tag.TagKey; import io.papermc.paper.tag.TagEntry; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.ItemType; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; @SuppressWarnings("UnstableApiUsage") @@ -19,8 +22,8 @@ public class TelepathyEnchant implements EnchantioEnchant { private final int anvilCost, weight; private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; - private final boolean canGetFromEnchantingTable; private final Set> supportedItemTags; + private final Set> enchantTagKeys = new HashSet<>(); public TelepathyEnchant( int anvilCost, @@ -34,8 +37,10 @@ public class TelepathyEnchant implements EnchantioEnchant { this.weight = weight; this.minimumCost = minimumCost; this.maximumCost = maximumCost; - this.canGetFromEnchantingTable = canGetFromEnchantingTable; this.supportedItemTags = supportedItemTags; + if (canGetFromEnchantingTable) { + enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE); + } } @Override @@ -78,19 +83,14 @@ public class TelepathyEnchant implements EnchantioEnchant { return Set.of(EquipmentSlotGroup.HAND); } - @Override - public boolean canGetFromEnchantingTable() { - return canGetFromEnchantingTable; - } - - @Override - public TagKey getTagForSupportedItems() { - return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:telepathy_enchantable")); - } - @Override public Set> getSupportedItems() { return supportedItemTags; } + @Override + public Set> getEnchantTagKeys() { + return Collections.unmodifiableSet(enchantTagKeys); + } + }