mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Implemented multiples methods for enchantment wrapper.
This commit is contained in:
parent
f261e3c795
commit
9f74c2cfff
3 changed files with 129 additions and 20 deletions
|
|
@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
import xyz.alexcrea.cuanvil.enchant.wrapped.VanillaEnchant;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class WrappedEnchantment {
|
||||
|
||||
|
|
@ -21,9 +22,9 @@ public abstract class WrappedEnchantment {
|
|||
|
||||
/**
|
||||
* Constructor of Wrapped Enchantment.
|
||||
* @param key the enchantment's key.
|
||||
* @param name the enchantment's name.
|
||||
* @param defaultRarity default rarity the enchantment should be.
|
||||
* @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,
|
||||
|
|
@ -38,7 +39,7 @@ public abstract class WrappedEnchantment {
|
|||
|
||||
/**
|
||||
* 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(){
|
||||
return defaultRarity;
|
||||
|
|
@ -46,7 +47,7 @@ public abstract class WrappedEnchantment {
|
|||
|
||||
/**
|
||||
* Get the enchantment key.
|
||||
* @return the enchantment key.
|
||||
* @return The enchantment key.
|
||||
*/
|
||||
@NotNull
|
||||
public final NamespacedKey getKey(){
|
||||
|
|
@ -55,7 +56,7 @@ public abstract class WrappedEnchantment {
|
|||
|
||||
/**
|
||||
* Get the enchantment name.
|
||||
* @return the enchantment name.
|
||||
* @return The enchantment name.
|
||||
*/
|
||||
@NotNull
|
||||
public String getName() {
|
||||
|
|
@ -64,17 +65,80 @@ public abstract class WrappedEnchantment {
|
|||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
// 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
|
||||
|
||||
/**
|
||||
* 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_NAME; //TODO decide if I should implement it.
|
||||
|
||||
|
|
@ -96,8 +160,8 @@ public abstract class WrappedEnchantment {
|
|||
* 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.
|
||||
* (By late I mean after custom anvil startup.)
|
||||
* @param enchantment The enchantment to be registered.
|
||||
*/
|
||||
public static void register(@NotNull WrappedEnchantment enchantment){
|
||||
BY_KEY.put(enchantment.getKey(), enchantment);
|
||||
|
|
@ -110,8 +174,8 @@ public abstract class WrappedEnchantment {
|
|||
* 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.
|
||||
* (By late I mean after custom anvil startup.)
|
||||
* @param enchantment The enchantment to be unregistered.
|
||||
*/
|
||||
public static void unregister(@NotNull WrappedEnchantment enchantment){
|
||||
BY_KEY.remove(enchantment.getKey());
|
||||
|
|
@ -120,8 +184,8 @@ public abstract class WrappedEnchantment {
|
|||
|
||||
/**
|
||||
* Gets the enchantment by the provided key.
|
||||
* @param key key to fetch.
|
||||
* @return registered enchantment. null if absent.
|
||||
* @param key Key to fetch.
|
||||
* @return Registered enchantment. null if absent.
|
||||
*/
|
||||
public static @Nullable WrappedEnchantment getByKey(@NotNull NamespacedKey key){
|
||||
return BY_KEY.get(key);
|
||||
|
|
@ -129,11 +193,11 @@ public abstract class WrappedEnchantment {
|
|||
|
||||
/**
|
||||
* Gets an array of all the registered enchantments.
|
||||
* @return array of enchantment.
|
||||
* @return Array of enchantment.
|
||||
*/
|
||||
@NotNull
|
||||
public static WrappedEnchantment[] values() {
|
||||
return BY_KEY.values().toArray(new WrappedEnchantment[BY_KEY.size()]);
|
||||
return BY_KEY.values().toArray(new WrappedEnchantment[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
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.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||
|
|
@ -24,7 +26,50 @@ public class VanillaEnchant extends WrappedEnchantment {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int enchantmentLevel(ItemStack item, ItemMeta meta) {
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
public int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta) {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue