mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
Cleaning up and add name map.
This commit is contained in:
parent
c7fee98d44
commit
27e8fb5719
4 changed files with 75 additions and 23 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package xyz.alexcrea.cuanvil.enchant;
|
||||
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
|
@ -10,7 +11,12 @@ import xyz.alexcrea.cuanvil.enchant.wrapped.VanillaEnchant;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* Represent any enchantment.
|
||||
* One issue with the plugin is: it does not handle well duplicate key name (ignoring namespace) as the plugin was coded with vanilla enchantment in head
|
||||
*/
|
||||
public abstract class WrappedEnchantment {
|
||||
|
||||
@NotNull
|
||||
|
|
@ -19,19 +25,21 @@ public abstract class WrappedEnchantment {
|
|||
private final String name;
|
||||
@NotNull
|
||||
private final EnchantmentRarity defaultRarity;
|
||||
private final int defaultMaxLevel;
|
||||
|
||||
/**
|
||||
* 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 defaultMaxLevel Default max level the enchantment can be applied with.
|
||||
*/
|
||||
public WrappedEnchantment(
|
||||
@NotNull NamespacedKey key,
|
||||
@NotNull String name,
|
||||
@Nullable EnchantmentRarity defaultRarity){
|
||||
@Nullable EnchantmentRarity defaultRarity,
|
||||
int defaultMaxLevel){
|
||||
this.key = key;
|
||||
this.name = name;
|
||||
this.name = key.getKey();
|
||||
this.defaultMaxLevel = defaultMaxLevel;
|
||||
|
||||
if(defaultRarity == null) this.defaultRarity = EnchantmentRarity.COMMON;
|
||||
else this.defaultRarity = defaultRarity;
|
||||
|
|
@ -59,7 +67,7 @@ public abstract class WrappedEnchantment {
|
|||
* @return The enchantment name.
|
||||
*/
|
||||
@NotNull
|
||||
public String getName() {
|
||||
public final String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
|
|
@ -67,9 +75,17 @@ public abstract class WrappedEnchantment {
|
|||
* Get the default maximum level of this enchantment.
|
||||
* @return The default maximum level of this enchantment.
|
||||
*/
|
||||
public abstract int defaultMaxLevel();
|
||||
public final int defaultMaxLevel(){return defaultMaxLevel;}
|
||||
|
||||
// TODO maybe methods that do not require itemmeta ?
|
||||
/**
|
||||
* Get current level of the enchantment.
|
||||
* @param item Item to search the level for.
|
||||
*/
|
||||
public int getLevel(@NotNull ItemStack item){
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if(meta == null) return 0;
|
||||
return getLevel(item, meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current level of the enchantment.
|
||||
|
|
@ -79,6 +95,17 @@ public abstract class WrappedEnchantment {
|
|||
*/
|
||||
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.
|
||||
* @return If the enchantment have been found.
|
||||
*/
|
||||
public boolean isEnchantmentPresent(@NotNull ItemStack item){
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if(meta == null) return false;
|
||||
return isEnchantmentPresent(item, meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this enchantment is present on the provided level.
|
||||
* @param item The item to set the enchantment level.
|
||||
|
|
@ -134,17 +161,14 @@ public abstract class WrappedEnchantment {
|
|||
}
|
||||
|
||||
// Register enchantment functions
|
||||
private static HashMap<NamespacedKey, WrappedEnchantment> BY_KEY;
|
||||
//private static HashMap<NamespacedKey, WrappedEnchantment> BY_NAME; //TODO decide if I should implement it.
|
||||
private static final HashMap<NamespacedKey, WrappedEnchantment> BY_KEY = new HashMap<>();
|
||||
private static final HashMap<String, WrappedEnchantment> BY_NAME = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
|
@ -159,8 +183,21 @@ public abstract class WrappedEnchantment {
|
|||
* @param enchantment The enchantment to be registered.
|
||||
*/
|
||||
public static void register(@NotNull WrappedEnchantment enchantment){
|
||||
if(BY_KEY.containsKey(enchantment.getKey())){
|
||||
CustomAnvil.instance.getLogger().log(Level.WARNING,
|
||||
"Duplicate registered enchantment. This should NOT happen.",
|
||||
new IllegalStateException(enchantment.getKey()+" enchantment was already registered"));
|
||||
return;
|
||||
}
|
||||
if(BY_NAME.containsKey(enchantment.getName())){
|
||||
CustomAnvil.instance.getLogger().log(Level.WARNING,
|
||||
"Duplicate registered enchantment name. There will have issue. " +
|
||||
"\nI hope this do not happen to you on a production server. If it do, there is probably a plugin trying to register an enchantment with the same name than another one",
|
||||
new IllegalStateException(enchantment.getKey()+" enchantment name was already registered"));
|
||||
}
|
||||
|
||||
BY_KEY.put(enchantment.getKey(), enchantment);
|
||||
//BY_NAME.put(enchantment.getName(), enchantment);
|
||||
BY_NAME.put(enchantment.getName(), enchantment);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -174,7 +211,7 @@ public abstract class WrappedEnchantment {
|
|||
*/
|
||||
public static void unregister(@NotNull WrappedEnchantment enchantment){
|
||||
BY_KEY.remove(enchantment.getKey());
|
||||
//BY_NAME.remove(enchantment.getName());
|
||||
BY_NAME.remove(enchantment.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -186,6 +223,15 @@ public abstract class WrappedEnchantment {
|
|||
return BY_KEY.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the enchantment by the provided name.
|
||||
* @param name Name to fetch.
|
||||
* @return Registered enchantment. null if absent.
|
||||
*/
|
||||
public static @Nullable WrappedEnchantment getByName(@NotNull String name){
|
||||
return BY_NAME.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of all the registered enchantments.
|
||||
* @return Array of enchantment.
|
||||
|
|
|
|||
|
|
@ -7,22 +7,22 @@ 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.EnchantmentProperties;
|
||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
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
|
||||
getRarity(enchantment),
|
||||
enchantment.getMaxLevel());
|
||||
this.enchantment = enchantment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int defaultMaxLevel() {
|
||||
return this.enchantment.getMaxLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -77,4 +77,11 @@ public class VanillaEnchant extends WrappedEnchantment {
|
|||
return Material.ENCHANTED_BOOK.equals(item.getType());
|
||||
}
|
||||
|
||||
public static EnchantmentRarity getRarity(Enchantment enchantment){
|
||||
try {return EnchantmentProperties.valueOf(enchantment.getKey().getKey().toUpperCase(Locale.ENGLISH)).getRarity();}
|
||||
catch (IllegalArgumentException ignored) {}
|
||||
|
||||
return EnchantmentRarity.COMMON;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui<EnchantCostSe
|
|||
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key);
|
||||
|
||||
// try to find rarity. default to 0 if not found
|
||||
EnchantmentRarity rarity = EnchantmentRarity.NO_RARITY;
|
||||
EnchantmentRarity rarity = enchant.defaultRarity();
|
||||
try {
|
||||
rarity = EnchantmentProperties.valueOf(key.toUpperCase(Locale.ENGLISH)).getRarity();
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue