Implemented multiples methods for enchantment wrapper.

This commit is contained in:
alexcrea 2024-06-16 03:42:33 +02:00
parent f261e3c795
commit 9f74c2cfff
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
3 changed files with 129 additions and 20 deletions

View file

@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.enchant.wrapped.VanillaEnchant; import xyz.alexcrea.cuanvil.enchant.wrapped.VanillaEnchant;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
public abstract class WrappedEnchantment { public abstract class WrappedEnchantment {
@ -21,9 +22,9 @@ public abstract class WrappedEnchantment {
/** /**
* Constructor of Wrapped Enchantment. * Constructor of Wrapped Enchantment.
* @param key the enchantment's key. * @param key The enchantment's key.
* @param name the enchantment's name. * @param name The enchantment's name.
* @param defaultRarity default rarity the enchantment should be. * @param defaultRarity Default rarity the enchantment should be.
*/ */
public WrappedEnchantment( public WrappedEnchantment(
@NotNull NamespacedKey key, @NotNull NamespacedKey key,
@ -38,7 +39,7 @@ public abstract class WrappedEnchantment {
/** /**
* Get the default rarity of this enchant. * Get the default rarity of this enchant.
* @return the default rarity of this enchant. * @return The default rarity of this enchant.
*/ */
public final EnchantmentRarity defaultRarity(){ public final EnchantmentRarity defaultRarity(){
return defaultRarity; return defaultRarity;
@ -46,7 +47,7 @@ public abstract class WrappedEnchantment {
/** /**
* Get the enchantment key. * Get the enchantment key.
* @return the enchantment key. * @return The enchantment key.
*/ */
@NotNull @NotNull
public final NamespacedKey getKey(){ public final NamespacedKey getKey(){
@ -55,7 +56,7 @@ public abstract class WrappedEnchantment {
/** /**
* Get the enchantment name. * Get the enchantment name.
* @return the enchantment name. * @return The enchantment name.
*/ */
@NotNull @NotNull
public String getName() { public String getName() {
@ -64,17 +65,80 @@ public abstract class WrappedEnchantment {
/** /**
* Get the default maximum level of this enchantment. * Get the default maximum level of this enchantment.
* @return the default maximum level of this enchantment. * @return The default maximum level of this enchantment.
*/ */
public abstract int defaultMaxLevel(); public abstract int defaultMaxLevel();
// TODO maybe methods that do not require itemmeta ?
public abstract int enchantmentLevel(ItemStack item, ItemMeta meta); /**
* Get current level of the enchantment.
* @param item Item to search the level for.
* @param meta Meta of the provided item. It will not be changed and not be set on the item.
* @return Current leve of this enchantment on item. or 0 if absent.
*/
public abstract int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta);
/**
* Check if this enchantment is present on the provided level.
* @param item The item to set the enchantment level.
* @param meta Meta of the provided item. It will not be changed and not be set on the item.
* @return If the enchantment have been found.
*/
public abstract boolean isEnchantmentPresent(@NotNull ItemStack item, @NotNull ItemMeta meta);
/**
* Force add an enchantment at the provided level.
* @param item The item to set the enchantment level.
* @param meta Meta of the provided item. It can be changed, but will not be set on the item.
* @param level The level to set the enchantment to.
*/
public abstract void addEnchantmentUnsafe(@NotNull ItemStack item, @NotNull ItemMeta meta, int level);
/**
* Remove this enchantment from the provided ItemStack.
* @param item The item to remove the enchantment.
* @param meta Meta of the provided item. It can be changed, but will not be set on the item.
*/
public abstract void removeFrom(@NotNull ItemStack item, @NotNull ItemMeta meta);
// Static functions // Static functions
/**
* Clear every enchantment from this item.
* @param item Item to be cleared from enchantments.
*/
public static void clearEnchants(@NotNull ItemStack item){ //TODO faster method to clear vanilla enchantment
ItemMeta meta = item.getItemMeta();
if(meta == null) return;
for (WrappedEnchantment enchant : getEnchants(item).keySet()) {
enchant.removeFrom(item, meta);
}
item.setItemMeta(meta);
}
/**
* Get enchantments of an item.
* @param item item to get enchantment from.
* @return a map of the set enchantments and there's respective levels.
*/
public static Map<WrappedEnchantment, Integer> getEnchants(@NotNull ItemStack item){ //TODO faster method to find vanilla enchantment
Map<WrappedEnchantment, Integer> enchantments = new HashMap<>();
ItemMeta meta = item.getItemMeta();
if(meta == null) return enchantments;
for (WrappedEnchantment enchantment : WrappedEnchantment.values()) {
if(enchantment.isEnchantmentPresent(item, meta)){
enchantments.put(enchantment, enchantment.getLevel(item, meta));
}
}
return enchantments;
}
// Register enchantment functions
private static HashMap<NamespacedKey, WrappedEnchantment> BY_KEY; private static HashMap<NamespacedKey, WrappedEnchantment> BY_KEY;
//private static HashMap<NamespacedKey, WrappedEnchantment> BY_NAME; //TODO decide if I should implement it. //private static HashMap<NamespacedKey, WrappedEnchantment> BY_NAME; //TODO decide if I should implement it.
@ -96,8 +160,8 @@ public abstract class WrappedEnchantment {
* Can be used to register new enchantment. * Can be used to register new enchantment.
* <p> * <p>
* No guarantee that the enchantment will be present on the config gui if registered late. * No guarantee that the enchantment will be present on the config gui if registered late.
* (by late I mean after custom anvil startup.) * (By late I mean after custom anvil startup.)
* @param enchantment the enchantment to register. * @param enchantment The enchantment to be registered.
*/ */
public static void register(@NotNull WrappedEnchantment enchantment){ public static void register(@NotNull WrappedEnchantment enchantment){
BY_KEY.put(enchantment.getKey(), enchantment); BY_KEY.put(enchantment.getKey(), enchantment);
@ -110,8 +174,8 @@ public abstract class WrappedEnchantment {
* It should probably rarely be used. * It should probably rarely be used.
* <p> * <p>
* No guarantee that the enchantment will absent if the config guis if unregistered late. * No guarantee that the enchantment will absent if the config guis if unregistered late.
* (by late I mean after custom anvil startup.) * (By late I mean after custom anvil startup.)
* @param enchantment the enchantment to unregister. * @param enchantment The enchantment to be unregistered.
*/ */
public static void unregister(@NotNull WrappedEnchantment enchantment){ public static void unregister(@NotNull WrappedEnchantment enchantment){
BY_KEY.remove(enchantment.getKey()); BY_KEY.remove(enchantment.getKey());
@ -120,8 +184,8 @@ public abstract class WrappedEnchantment {
/** /**
* Gets the enchantment by the provided key. * Gets the enchantment by the provided key.
* @param key key to fetch. * @param key Key to fetch.
* @return registered enchantment. null if absent. * @return Registered enchantment. null if absent.
*/ */
public static @Nullable WrappedEnchantment getByKey(@NotNull NamespacedKey key){ public static @Nullable WrappedEnchantment getByKey(@NotNull NamespacedKey key){
return BY_KEY.get(key); return BY_KEY.get(key);
@ -129,11 +193,11 @@ public abstract class WrappedEnchantment {
/** /**
* Gets an array of all the registered enchantments. * Gets an array of all the registered enchantments.
* @return array of enchantment. * @return Array of enchantment.
*/ */
@NotNull @NotNull
public static WrappedEnchantment[] values() { public static WrappedEnchantment[] values() {
return BY_KEY.values().toArray(new WrappedEnchantment[BY_KEY.size()]); return BY_KEY.values().toArray(new WrappedEnchantment[0]);
} }
} }

View file

@ -1,8 +1,10 @@
package xyz.alexcrea.cuanvil.enchant.wrapped; package xyz.alexcrea.cuanvil.enchant.wrapped;
import org.bukkit.NamespacedKey; import io.delilaheve.util.ItemUtil;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
@ -24,7 +26,50 @@ public class VanillaEnchant extends WrappedEnchantment {
} }
@Override @Override
public int enchantmentLevel(ItemStack item, ItemMeta meta) { public int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta) {
throw new UnsupportedOperationException("Not yet implemented"); if (isEnchantedBook(item)) {
return ((EnchantmentStorageMeta)meta).getStoredEnchantLevel(this.enchantment);
} else {
return meta.getEnchantLevel(this.enchantment);
}
} }
@Override
public boolean isEnchantmentPresent(@NotNull ItemStack item, @NotNull ItemMeta meta) {
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)meta);
return bookMeta.getStoredEnchants().containsKey(this.enchantment);
}else{
return item.containsEnchantment(this.enchantment);
}
}
@Override
public void addEnchantmentUnsafe(@NotNull ItemStack item, @NotNull ItemMeta meta, int level) {
if (isEnchantedBook(item)) {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)meta);
bookMeta.addStoredEnchant(this.enchantment, level, true);
} else {
item.addUnsafeEnchantment(this.enchantment, level);
}
}
@Override
public void removeFrom(@NotNull ItemStack item, @NotNull ItemMeta meta) {
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)meta);
bookMeta.removeStoredEnchant(this.enchantment);
}
item.removeEnchantment(this.enchantment);
}
public static boolean isEnchantedBook(@NotNull ItemStack item){
return Material.ENCHANTED_BOOK.equals(item.getType());
}
} }

View file

@ -95,7 +95,7 @@ class CustomAnvil : JavaPlugin() {
} }
// Register enchantments // Register enchantments
WrappedEnchantment.registerEnchantments(); WrappedEnchantment.registerEnchantments()
// Load ProtocolLib dependency if exist // Load ProtocolLib dependency if exist
packetManager = if(pluginManager.isPluginEnabled("ProtocolLib")) packetManager = if(pluginManager.isPluginEnabled("ProtocolLib"))