From e30f70eab52de599bf350b1540e3dd124b5569a9 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 12 Dec 2020 12:44:28 +0000 Subject: [PATCH] Updated requiredToExtend --- .../willfp/ecoenchants/EcoEnchantsPlugin.java | 2 - .../ecoenchants/enchantments/EcoEnchant.java | 43 ++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java b/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java index f03beb84..6ec2ac18 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java @@ -1,7 +1,5 @@ package com.willfp.ecoenchants; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; import com.willfp.ecoenchants.extensions.loader.EcoExtensionLoader; import com.willfp.ecoenchants.extensions.loader.ExtensionLoader; import com.willfp.ecoenchants.util.internal.Loader; diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java index ea20dd3f..c9f8fd0e 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java @@ -2,6 +2,8 @@ package com.willfp.ecoenchants.enchantments; import com.willfp.ecoenchants.config.ConfigManager; import com.willfp.ecoenchants.config.configs.EnchantmentConfig; +import com.willfp.ecoenchants.enchantments.itemtypes.Artifact; +import com.willfp.ecoenchants.enchantments.itemtypes.Spell; import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; import com.willfp.ecoenchants.enchantments.util.Watcher; @@ -76,6 +78,11 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist Bukkit.getPluginManager().addPermission(permission); } + if(type.getRequiredToExtend() != null && type.getRequiredToExtend().isInstance(this)) { + Logger.error("Enchantment " + key + " has type " + this.getType().getName() + " but doesn't extend " + type.getRequiredToExtend().getName()); + return; + } + if (!Prerequisite.areMet(prerequisites)) return; @@ -405,14 +412,15 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist public static final EnchantmentType NORMAL = new EnchantmentType("normal", false, () -> ConfigManager.getLang().getString("not-curse-color")); public static final EnchantmentType CURSE = new EnchantmentType("curse", false, () -> ConfigManager.getLang().getString("curse-color")); public static final EnchantmentType SPECIAL = new EnchantmentType("special", () -> !ConfigManager.getConfig().getBool("types.special.allow-multiple"), () -> ConfigManager.getLang().getString("special-color")); - public static final EnchantmentType ARTIFACT = new EnchantmentType("artifact", () -> !ConfigManager.getConfig().getBool("types.artifact.allow-multiple"), () -> ConfigManager.getLang().getString("artifact-color")); - public static final EnchantmentType SPELL = new EnchantmentType("spell", true, () -> ConfigManager.getLang().getString("spell-color")); + public static final EnchantmentType ARTIFACT = new EnchantmentType("artifact", () -> !ConfigManager.getConfig().getBool("types.artifact.allow-multiple"), () -> ConfigManager.getLang().getString("artifact-color"), Artifact.class); + public static final EnchantmentType SPELL = new EnchantmentType("spell", true, () -> ConfigManager.getLang().getString("spell-color"), Spell.class); private boolean singular; private String color; private final String name; private final ObjectCallable colorCallable; private final ObjectCallable singularCallable; + private final Class requiredToExtend; /** * Create simple EnchantmentType @@ -440,6 +448,20 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist this(name, () -> singular, colorCallable); } + /** + * Create EnchantmentType with updatable color that must extend a specified class + *

+ * Singularity will not be updated using this constructor + * + * @param name The name of the type + * @param singular Whether an item can have several enchantments of this type + * @param colorCallable Lambda to fetch the color of enchantments with this type to have. Updates on /ecoreload + * @param requiredToExtend Class that all enchantments of this type must extend - or null if not required + */ + public EnchantmentType(String name, boolean singular, ObjectCallable colorCallable, Class requiredToExtend) { + this(name, () -> singular, colorCallable, requiredToExtend); + } + /** * Create EnchantmentType with updatable color and singularity * @@ -448,9 +470,22 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist * @param colorCallable Lambda to fetch the color of enchantments with this type to have. Updates on /ecoreload */ public EnchantmentType(String name, ObjectCallable singularCallable, ObjectCallable colorCallable) { + this(name, singularCallable, colorCallable, null); + } + + /** + * Create EnchantmentType with updatable color and singularity that must extend a specified class + * + * @param name The name of the type + * @param singularCallable Lambda to fetch whether an item can have several enchantments of this type. Updates on /ecoreload + * @param colorCallable Lambda to fetch the color of enchantments with this type to have. Updates on /ecoreload + * @param requiredToExtend Class that all enchantments of this type must extend - or null if not required + */ + public EnchantmentType(String name, ObjectCallable singularCallable, ObjectCallable colorCallable, Class requiredToExtend) { this.name = name; this.singularCallable = singularCallable; this.colorCallable = colorCallable; + this.requiredToExtend = requiredToExtend; color = colorCallable.call(); singular = singularCallable.call(); values.add(this); @@ -473,6 +508,10 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist return name; } + public Class getRequiredToExtend() { + return requiredToExtend; + } + public static void update() { values.forEach(EnchantmentType::refresh); }