From f261e3c7957cc70e600a9834476fe6bb635c73e0 Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Sun, 16 Jun 2024 02:16:38 +0200 Subject: [PATCH] Start the implementation of a custom wrapper for enchantment. This will aim to allow to implement support for custom implementation of enchantment --- .../cuanvil/enchant/WrappedEnchantment.java | 139 ++++++++++++++++++ .../enchant/wrapped/VanillaEnchant.java | 30 ++++ src/main/kotlin/io/delilaheve/CustomAnvil.kt | 4 + 3 files changed, 173 insertions(+) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java create mode 100644 src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/VanillaEnchant.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java new file mode 100644 index 0000000..51c5d79 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/WrappedEnchantment.java @@ -0,0 +1,139 @@ +package xyz.alexcrea.cuanvil.enchant; + +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import xyz.alexcrea.cuanvil.enchant.wrapped.VanillaEnchant; + +import java.util.HashMap; + +public abstract class WrappedEnchantment { + + @NotNull + private final NamespacedKey key; + @NotNull + private final String name; + @NotNull + private final EnchantmentRarity defaultRarity; + + /** + * Constructor of Wrapped Enchantment. + * @param key the enchantment's key. + * @param name the enchantment's name. + * @param defaultRarity default rarity the enchantment should be. + */ + public WrappedEnchantment( + @NotNull NamespacedKey key, + @NotNull String name, + @Nullable EnchantmentRarity defaultRarity){ + this.key = key; + this.name = name; + + if(defaultRarity == null) this.defaultRarity = EnchantmentRarity.COMMON; + else this.defaultRarity = defaultRarity; + } + + /** + * Get the default rarity of this enchant. + * @return the default rarity of this enchant. + */ + public final EnchantmentRarity defaultRarity(){ + return defaultRarity; + } + + /** + * Get the enchantment key. + * @return the enchantment key. + */ + @NotNull + public final NamespacedKey getKey(){ + return key; + } + + /** + * Get the enchantment name. + * @return the enchantment name. + */ + @NotNull + public String getName() { + return name; + } + + /** + * Get the default maximum level of this enchantment. + * @return the default maximum level of this enchantment. + */ + public abstract int defaultMaxLevel(); + + + public abstract int enchantmentLevel(ItemStack item, ItemMeta meta); + + + + + // Static functions + private static HashMap BY_KEY; + //private static HashMap BY_NAME; //TODO decide if I should implement it. + + /** + * This should only be called on main of custom anvil. + * If called more than one time, chance of thing being broken will be high. + */ + public static void registerEnchantments(){ + BY_KEY = new HashMap<>(); + //BY_NAME = new HashMap<>(); + + for (Enchantment enchantment : Enchantment.values()) { + register(new VanillaEnchant(enchantment)); + } + + } + + /** + * Can be used to register new enchantment. + *

+ * No guarantee that the enchantment will be present on the config gui if registered late. + * (by late I mean after custom anvil startup.) + * @param enchantment the enchantment to register. + */ + public static void register(@NotNull WrappedEnchantment enchantment){ + BY_KEY.put(enchantment.getKey(), enchantment); + //BY_NAME.put(enchantment.getName(), enchantment); + } + + /** + * Can be used to unregister new enchantment. + * Please be cautious with this function. + * It should probably rarely be used. + *

+ * No guarantee that the enchantment will absent if the config guis if unregistered late. + * (by late I mean after custom anvil startup.) + * @param enchantment the enchantment to unregister. + */ + public static void unregister(@NotNull WrappedEnchantment enchantment){ + BY_KEY.remove(enchantment.getKey()); + //BY_NAME.remove(enchantment.getName()); + } + + /** + * Gets the enchantment by the provided key. + * @param key key to fetch. + * @return registered enchantment. null if absent. + */ + public static @Nullable WrappedEnchantment getByKey(@NotNull NamespacedKey key){ + return BY_KEY.get(key); + } + + /** + * Gets an array of all the registered enchantments. + * @return array of enchantment. + */ + @NotNull + public static WrappedEnchantment[] values() { + return BY_KEY.values().toArray(new WrappedEnchantment[BY_KEY.size()]); + } + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/VanillaEnchant.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/VanillaEnchant.java new file mode 100644 index 0000000..2ca3ebe --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/VanillaEnchant.java @@ -0,0 +1,30 @@ +package xyz.alexcrea.cuanvil.enchant.wrapped; + +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; +import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; +import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment; + +public class VanillaEnchant extends WrappedEnchantment { + + private final @NotNull Enchantment enchantment; + public VanillaEnchant(@NotNull Enchantment enchantment){ + super(enchantment.getKey(), + enchantment.getName(), + EnchantmentRarity.COMMON);//TODO determine rarity + this.enchantment = enchantment; + } + + @Override + public int defaultMaxLevel() { + return this.enchantment.getMaxLevel(); + } + + @Override + public int enchantmentLevel(ItemStack item, ItemMeta meta) { + throw new UnsupportedOperationException("Not yet implemented"); + } +} diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index ec0ed16..212b82e 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -7,6 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin import xyz.alexcrea.cuanvil.command.EditConfigExecutor import xyz.alexcrea.cuanvil.command.ReloadExecutor import xyz.alexcrea.cuanvil.config.ConfigHolder +import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment import xyz.alexcrea.cuanvil.gui.config.MainConfigGui import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant import xyz.alexcrea.cuanvil.listener.ChatEventListener @@ -93,6 +94,9 @@ class CustomAnvil : JavaPlugin() { logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus") } + // Register enchantments + WrappedEnchantment.registerEnchantments(); + // Load ProtocolLib dependency if exist packetManager = if(pluginManager.isPluginEnabled("ProtocolLib")) { ProtocoLibWrapper(); }