From 59dd57d992503ba534be683076b97089ce6a0ea6 Mon Sep 17 00:00:00 2001 From: Lorenzo <69311874+Lorenzo0111@users.noreply.github.com> Date: Sat, 18 Sep 2021 15:54:42 +0200 Subject: [PATCH] Fixed legacy support --- .../multilang/handlers/ConfigManager.java | 5 +--- .../me/lorenzo0111/multilang/hooks/Hook.java | 9 +++++-- .../hooks/LegacyRocketPlaceholdersHook.java | 27 +++++++++++++++++-- .../hooks/RocketPlaceholdersHook.java | 10 ++++++- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java b/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java index b6bd2df..97c6998 100644 --- a/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java +++ b/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java @@ -30,7 +30,6 @@ import me.lorenzo0111.multilang.exceptions.ConfigException; import me.lorenzo0111.multilang.exceptions.ReloadException; import me.lorenzo0111.multilang.requirements.LangRequirement; -import me.lorenzo0111.rocketplaceholders.creator.Placeholder; import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode; import org.bukkit.configuration.ConfigurationSection; import org.jetbrains.annotations.Nullable; @@ -112,9 +111,7 @@ public void save(String identifier,String defaultText,Map locales localesMap.forEach((locale,string) -> conditions.add(new ConditionNode(new LangRequirement(plugin,locale),string))); - Placeholder placeholder = new Placeholder(identifier,plugin,defaultText,conditions, null); - - plugin.getHook().addPlaceholder(placeholder); + plugin.getHook().addPlaceholder(identifier,plugin,defaultText,conditions); } public void unregisterAll() { diff --git a/src/main/java/me/lorenzo0111/multilang/hooks/Hook.java b/src/main/java/me/lorenzo0111/multilang/hooks/Hook.java index 162ab79..564ba19 100644 --- a/src/main/java/me/lorenzo0111/multilang/hooks/Hook.java +++ b/src/main/java/me/lorenzo0111/multilang/hooks/Hook.java @@ -1,11 +1,16 @@ package me.lorenzo0111.multilang.hooks; import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI; -import me.lorenzo0111.rocketplaceholders.creator.Placeholder; +import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; public interface Hook { void register(IRocketPlaceholdersAPI api); void unregister(IRocketPlaceholdersAPI api); - void addPlaceholder(Placeholder placeholder); + void addPlaceholder(@NotNull String identifier, JavaPlugin owner, @NotNull String text, @Nullable List nodes); } diff --git a/src/main/java/me/lorenzo0111/multilang/hooks/LegacyRocketPlaceholdersHook.java b/src/main/java/me/lorenzo0111/multilang/hooks/LegacyRocketPlaceholdersHook.java index 6db6f43..972f0b1 100644 --- a/src/main/java/me/lorenzo0111/multilang/hooks/LegacyRocketPlaceholdersHook.java +++ b/src/main/java/me/lorenzo0111/multilang/hooks/LegacyRocketPlaceholdersHook.java @@ -1,12 +1,20 @@ package me.lorenzo0111.multilang.hooks; +import me.lorenzo0111.multilang.MultiLangPlugin; import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI; import me.lorenzo0111.rocketplaceholders.creator.Placeholder; +import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.List; public class LegacyRocketPlaceholdersHook implements Hook { private final List placeholders; + private Constructor constructor; public LegacyRocketPlaceholdersHook(List placeholders) { this.placeholders = placeholders; @@ -29,7 +37,22 @@ public void unregister(IRocketPlaceholdersAPI api) { } @Override - public void addPlaceholder(Placeholder placeholder) { - placeholders.add(placeholder); + public void addPlaceholder(@NotNull String identifier, JavaPlugin owner, @NotNull String text, @Nullable List nodes) { + if (constructor == null) { + try { + this.constructor = Placeholder.class.getConstructor(String.class, String.class, JavaPlugin.class, String.class, List.class); + } catch (NoSuchMethodException e) { + MultiLangPlugin.getInstance().getLogger().severe("An error has occurred while registering a placeholder with reflection legacy support."); + } + } + + try { + Placeholder placeholder = constructor.newInstance(null,identifier,owner,text,nodes); + + placeholders.add(placeholder); + } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { + MultiLangPlugin.getInstance().getLogger().severe("An error has occurred while registering a placeholder with reflection legacy support: " + e.getMessage()); + } + } } diff --git a/src/main/java/me/lorenzo0111/multilang/hooks/RocketPlaceholdersHook.java b/src/main/java/me/lorenzo0111/multilang/hooks/RocketPlaceholdersHook.java index 37be0eb..5131e29 100644 --- a/src/main/java/me/lorenzo0111/multilang/hooks/RocketPlaceholdersHook.java +++ b/src/main/java/me/lorenzo0111/multilang/hooks/RocketPlaceholdersHook.java @@ -4,10 +4,16 @@ import me.lorenzo0111.rocketplaceholders.RocketPlaceholders; import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI; import me.lorenzo0111.rocketplaceholders.creator.Placeholder; +import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode; import me.lorenzo0111.rocketplaceholders.exceptions.InvalidConditionException; import me.lorenzo0111.rocketplaceholders.providers.Provider; import me.lorenzo0111.rocketplaceholders.storage.StorageManager; import org.bukkit.OfflinePlayer; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; public class RocketPlaceholdersHook extends Provider implements Hook { @@ -27,7 +33,9 @@ public void unregister(IRocketPlaceholdersAPI api) { } @Override - public void addPlaceholder(Placeholder placeholder) { + public void addPlaceholder(@NotNull String identifier, JavaPlugin owner, @NotNull String text, @Nullable List nodes) { + Placeholder placeholder = new Placeholder(identifier,owner,text,nodes, null); + manager.getInternalPlaceholders().add(placeholder); }