From 41702be00d3f68d75d77c51d799d678ec395ef41 Mon Sep 17 00:00:00 2001 From: Jules Date: Thu, 25 Jul 2024 20:27:52 -0700 Subject: [PATCH] Fixed compatibility with recent MythicEnchants builds --- MMOItems-API/pom.xml | 4 +- .../java/net/Indyuce/mmoitems/MMOItems.java | 56 +++++++++---------- .../mmoitems/comp/enchants/EnchantPlugin.java | 24 +++++--- .../comp/enchants/MythicEnchantsSupport.java | 20 ++++--- .../net/Indyuce/mmoitems/stat/Enchants.java | 14 +++-- MMOItems-Dist/src/main/resources/plugin.yml | 2 +- 6 files changed, 67 insertions(+), 53 deletions(-) diff --git a/MMOItems-API/pom.xml b/MMOItems-API/pom.xml index 08e44d0b..3f902a7f 100644 --- a/MMOItems-API/pom.xml +++ b/MMOItems-API/pom.xml @@ -133,8 +133,8 @@ io.lumine - MythicEnchants - 0.0.1-REUPLOAD + MythicEnchants-Dist + 0.1.0-SNAPSHOT provided true diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/MMOItems.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/MMOItems.java index a708402f..c9ad0fc5 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/MMOItems.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/MMOItems.java @@ -73,7 +73,7 @@ public class MMOItems extends MMOPlugin { private final TypeManager typeManager = new TypeManager(); private final ItemManager itemManager = new ItemManager(); private final PlayerInventoryHandler inventory = new PlayerInventoryHandler(); - private final List> enchantPlugins = new ArrayList<>(); + private final List> enchantPlugins = new ArrayList<>(); private final StatManager statManager = new StatManager(); private PlayerDataManager playerDataManager; @@ -124,11 +124,11 @@ public class MMOItems extends MMOPlugin { PluginUtils.isDependencyPresent("MMOCore", u -> new MMOCoreMMOLoader()); PluginUtils.isDependencyPresent("mcMMO", u -> statManager.register(McMMOHook.disableMcMMORepair)); - PluginUtils.isDependencyPresent("AdvancedEnchantments", u -> { + PluginUtils.hookDependencyIfPresent("AdvancedEnchantments", u -> { statManager.register(AdvancedEnchantmentsHook.ADVANCED_ENCHANTMENTS); statManager.register(AdvancedEnchantmentsHook.DISABLE_ADVANCED_ENCHANTMENTS); }); - PluginUtils.isDependencyPresent("MythicEnchants", u -> enchantPlugins.add(new MythicEnchantsSupport())); + PluginUtils.hookDependencyIfPresent("MythicEnchants", u -> enchantPlugins.add(new MythicEnchantsSupport())); PluginUtils.isDependencyPresent("Heroes", u -> statManager.register(HeroesHook.MAX_STAMINA)); } @@ -410,10 +410,10 @@ public class MMOItems extends MMOPlugin { * * @param value The player inventory subclass * @deprecated Rather than setting this to the only inventory MMOItems will - * search equipment within, you must add your inventory to the - * handler with getInventory().register(). This method - * will clear all other PlayerInventories for now, as to keep - * backwards compatibility. + * search equipment within, you must add your inventory to the + * handler with getInventory().register(). This method + * will clear all other PlayerInventories for now, as to keep + * backwards compatibility. */ @Deprecated public void setPlayerInventory(PlayerInventory value) { @@ -507,7 +507,7 @@ public class MMOItems extends MMOPlugin { return vaultSupport != null && vaultSupport.getPermissions() != null; } - public List> getEnchantPlugins() { + public List> getEnchantPlugins() { return enchantPlugins; } @@ -523,9 +523,9 @@ public class MMOItems extends MMOPlugin { /** * @return Generates an item given an item template. The item level will - * scale according to the player RPG level if the template has the - * 'level-item' option. The item will pick a random tier if the - * template has the 'tiered' option + * scale according to the player RPG level if the template has the + * 'level-item' option. The item will pick a random tier if the + * template has the 'tiered' option */ @Nullable public MMOItem getMMOItem(@Nullable Type type, @Nullable String id, @Nullable PlayerData player) { @@ -543,9 +543,9 @@ public class MMOItems extends MMOPlugin { /** * @return Generates an item given an item template. The item level will - * scale according to the player RPG level if the template has the - * 'level-item' option. The item will pick a random tier if the - * template has the 'tiered' option + * scale according to the player RPG level if the template has the + * 'level-item' option. The item will pick a random tier if the + * template has the 'tiered' option */ @Nullable public ItemStack getItem(@Nullable Type type, @Nullable String id, @NotNull PlayerData player) { @@ -562,7 +562,7 @@ public class MMOItems extends MMOPlugin { * @param itemLevel The desired item level * @param itemTier The desired item tier, can be null * @return Generates an item given an item template with a - * specific item level and item tier + * specific item level and item tier */ @Nullable public MMOItem getMMOItem(@Nullable Type type, @Nullable String id, int itemLevel, @Nullable ItemTier itemTier) { @@ -582,7 +582,7 @@ public class MMOItems extends MMOPlugin { * @param itemLevel The desired item level * @param itemTier The desired item tier, can be null * @return Generates an item given an item template with a - * specific item level and item tier + * specific item level and item tier */ @Nullable public ItemStack getItem(@Nullable Type type, @Nullable String id, int itemLevel, @Nullable ItemTier itemTier) { @@ -597,10 +597,10 @@ public class MMOItems extends MMOPlugin { /** * @return Generates an item given an item template. The item level will be - * 0 and the item will have no item tier unless one is specified in - * the base item data. - *

- * Will return null if such MMOItem does not exist. + * 0 and the item will have no item tier unless one is specified in + * the base item data. + *

+ * Will return null if such MMOItem does not exist. */ @Nullable public MMOItem getMMOItem(@Nullable Type type, @Nullable String id) { @@ -609,10 +609,10 @@ public class MMOItems extends MMOPlugin { /** * @return Generates an item given an item template. The item level will be - * 0 and the item will have no item tier unless one is specified in - * the base item data. - *

- * Will return null if such MMOItem does not exist. + * 0 and the item will have no item tier unless one is specified in + * the base item data. + *

+ * Will return null if such MMOItem does not exist. */ @Nullable @@ -625,10 +625,10 @@ public class MMOItems extends MMOPlugin { /** * @return Generates an item given an item template. The item level will be - * 0 and the item will have no item tier unless one is specified in - * the base item data. - *

- * Will return null if such MMOItem does not exist. + * 0 and the item will have no item tier unless one is specified in + * the base item data. + *

+ * Will return null if such MMOItem does not exist. */ @Nullable public ItemStack getItem(@Nullable Type type, @Nullable String id) { diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/EnchantPlugin.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/EnchantPlugin.java index 068824e7..b8c9db5a 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/EnchantPlugin.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/EnchantPlugin.java @@ -3,6 +3,8 @@ package net.Indyuce.mmoitems.comp.enchants; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; import org.bukkit.NamespacedKey; import org.bukkit.enchantments.Enchantment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * There are three types of enchant plugins. @@ -13,16 +15,19 @@ import org.bukkit.enchantments.Enchantment; * Interface used to support plugins which use the Bukkit Enchantment * interface to register their enchantments. This makes enchant storage * so much easier for MMOItems. - * - * @param The plugin class implementing Enchantment */ -public interface EnchantPlugin { +public interface EnchantPlugin { /** - * @param enchant Enchant being checked - * @return If this enchant plugin handles a given enchant + * Used to determine if an enchantment comes from that enchant plugin + * + * @return The namespace of keys used for registering Bukkit enchantments */ - boolean isCustomEnchant(Enchantment enchant); + @NotNull + String getNamespace(); + + @Nullable + T transfer(@NotNull Enchantment enchant); /** * Called when an item is built. This should be used to add the enchantment @@ -32,7 +37,10 @@ public interface EnchantPlugin { * @param enchant Enchantment being applied * @param level Enchant level */ - void handleEnchant(ItemStackBuilder builder, T enchant, int level); + void handleEnchant(@NotNull ItemStackBuilder builder, @NotNull T enchant, int level); - NamespacedKey getNamespacedKey(String key); + @NotNull + default NamespacedKey getNamespacedKey(@NotNull String key) { + return new NamespacedKey(getNamespace(), key); + } } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/MythicEnchantsSupport.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/MythicEnchantsSupport.java index a74adbbf..cf5a9465 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/MythicEnchantsSupport.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/comp/enchants/MythicEnchantsSupport.java @@ -4,28 +4,30 @@ import io.lumine.mythicenchants.MythicEnchants; import io.lumine.mythicenchants.enchants.MythicEnchant; import io.lumine.mythicenchants.util.LoreParser; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; -import org.apache.commons.lang.Validate; -import org.bukkit.NamespacedKey; +import org.bukkit.Bukkit; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class MythicEnchantsSupport implements EnchantPlugin { + private final MythicEnchants manager = (MythicEnchants) Bukkit.getPluginManager().getPlugin("MythicEnchants"); + @NotNull @Override - public boolean isCustomEnchant(Enchantment enchant) { - return enchant instanceof MythicEnchant; + public String getNamespace() { + return "mythicenchants"; } + @Nullable @Override - public NamespacedKey getNamespacedKey(String key) { - return new NamespacedKey(MythicEnchants.inst(), key); + public MythicEnchant transfer(@NotNull Enchantment enchantment) { + return manager.getEnchantManager().toMythicEnchantment(enchantment).orElse(null); } - public void handleEnchant(ItemStackBuilder builder, MythicEnchant enchant, int level) { - Validate.isTrue(level > 0, "Level must be strictly positive"); + public void handleEnchant(@NotNull ItemStackBuilder builder, @NotNull MythicEnchant enchant, int level) { // Type cannot be changed. Must make sure that item is an enchanted book - if (!builder.getMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)) builder.getLore().insert(0, LoreParser.formatEnchantment(enchant, level)); } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/Enchants.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/Enchants.java index 394ac13c..19a4ae4b 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/Enchants.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/Enchants.java @@ -283,9 +283,13 @@ public class Enchants extends ItemStat i } // Handle custom enchant - for (EnchantPlugin enchantPlugin : MMOItems.plugin.getEnchantPlugins()) - if (enchantPlugin.isCustomEnchant(enchant)) - enchantPlugin.handleEnchant(item, enchant, lvl); + for (EnchantPlugin enchantPlugin : MMOItems.plugin.getEnchantPlugins()) { + final Object custom = enchantPlugin.transfer(enchant); + if (custom != null) { + enchantPlugin.handleEnchant(item, custom, lvl); + break; + } + } } // Apply tags @@ -509,8 +513,8 @@ public class Enchants extends ItemStat i // Check for custom enchants for (EnchantPlugin enchPlugin : MMOItems.plugin.getEnchantPlugins()) { - Enchantment checked = Enchantment.getByKey(enchPlugin.getNamespacedKey(key)); - if (checked != null) return checked; + enchant = Enchantment.getByKey(enchPlugin.getNamespacedKey(key)); + if (enchant != null) return enchant; } // Last try, vanilla enchant with name diff --git a/MMOItems-Dist/src/main/resources/plugin.yml b/MMOItems-Dist/src/main/resources/plugin.yml index 63feae30..ea26a6e8 100644 --- a/MMOItems-Dist/src/main/resources/plugin.yml +++ b/MMOItems-Dist/src/main/resources/plugin.yml @@ -4,7 +4,7 @@ main: net.Indyuce.mmoitems.MMOItems author: Indyuce description: ${project.description} depend: [MythicLib] -softdepend: [WorldGuard,Residence,BossShopPro,Fabled,SkillsPro,MMOCore,MMOProfiles,MythicMobs,MMOInventory,PlaceholderAPI,Vault,Denizen,AureliumSkills,AuraSkills] +softdepend: [WorldGuard,Residence,BossShopPro,Fabled,SkillsPro,MMOCore,MMOProfiles,MythicMobs,MMOInventory,PlaceholderAPI,Vault,Denizen,AureliumSkills,AuraSkills,MythicEnchants] api-version: 1.14 website: www.phoenixdevt.fr commands: