Start the implementation of a custom wrapper for enchantment.

This will aim to allow to implement support for custom implementation of enchantment
This commit is contained in:
alexcrea 2024-06-16 02:16:38 +02:00
parent d13085f4e1
commit f261e3c795
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
3 changed files with 173 additions and 0 deletions

View file

@ -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<NamespacedKey, WrappedEnchantment> BY_KEY;
//private static HashMap<NamespacedKey, WrappedEnchantment> 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.
* <p>
* 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.
* <p>
* 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()]);
}
}

View file

@ -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");
}
}

View file

@ -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(); }