diff --git a/build.gradle b/build.gradle index 044f9df..93e1692 100644 --- a/build.gradle +++ b/build.gradle @@ -38,7 +38,7 @@ ext { apply from: "https://cdn.rocketplugins.space/spigot" group = "me.lorenzo0111" -version = "1.6" +version = "1.6.1" java { withJavadocJar() @@ -64,7 +64,7 @@ dependencies { implementation("dev.triumphteam:triumph-gui:3.0.3") compileOnly("com.comphenix.protocol:ProtocolLib:4.7.0") compileOnly("com.github.MilkBowl:VaultAPI:1.7.1") - compileOnly("me.lorenzo0111:RocketPlaceholders:1.9.2") + compileOnly("me.lorenzo0111:RocketPlaceholders:2.0") slim("com.zaxxer:HikariCP:4.0.3") slim("com.github.cryptomorin:XSeries:8.4.0") slim("net.kyori:adventure-api:4.9.1") diff --git a/src/main/java/me/lorenzo0111/multilang/MultiLangPlugin.java b/src/main/java/me/lorenzo0111/multilang/MultiLangPlugin.java index a177a6b..6849800 100644 --- a/src/main/java/me/lorenzo0111/multilang/MultiLangPlugin.java +++ b/src/main/java/me/lorenzo0111/multilang/MultiLangPlugin.java @@ -33,6 +33,7 @@ import me.lorenzo0111.multilang.database.DatabaseManager; import me.lorenzo0111.multilang.exceptions.ReloadException; import me.lorenzo0111.multilang.handlers.ConfigManager; +import me.lorenzo0111.multilang.hooks.Hook; import me.lorenzo0111.multilang.listeners.JoinListener; import me.lorenzo0111.multilang.protocol.PacketHandler; import me.lorenzo0111.multilang.storage.StorageManager; @@ -62,6 +63,7 @@ public final class MultiLangPlugin extends JavaPlugin { private IMultiLangAPI api; private IRocketPlaceholdersAPI rocketPlaceholdersAPI; + private Hook hook; private ConfigManager configManager; private PluginLoader loader; private static MultiLangPlugin instance = null; @@ -167,14 +169,26 @@ public void onDisable() { } } - public IRocketPlaceholdersAPI getRocketPlaceholdersAPI() { - return rocketPlaceholdersAPI; + public void register() { + this.getHook().register(rocketPlaceholdersAPI); + } + + public void unregister() { + this.getHook().unregister(rocketPlaceholdersAPI); + } + + public Hook getHook() { + return hook; } public void setRocketPlaceholdersAPI(IRocketPlaceholdersAPI rocketPlaceholdersAPI) { this.rocketPlaceholdersAPI = rocketPlaceholdersAPI; } + public void setHook(Hook hook) { + this.hook = hook; + } + public void setConfigManager(ConfigManager configManager) { this.configManager = configManager; } diff --git a/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java b/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java index 2ef5377..b6bd2df 100644 --- a/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java +++ b/src/main/java/me/lorenzo0111/multilang/handlers/ConfigManager.java @@ -41,7 +41,6 @@ public class ConfigManager { private Locale defaultLocale; private final Map keysMap = new HashMap<>(); private final MultiLangPlugin plugin; - private final List addedPlaceholders = new ArrayList<>(); public ConfigManager(MultiLangPlugin plugin) { this.plugin = plugin; } @@ -88,8 +87,8 @@ public void register() { } + this.plugin.register(); final long time = System.currentTimeMillis() - before; - plugin.getLogger().info("Loaded all placeholders in " + time + "ms"); } @@ -113,15 +112,13 @@ 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(null,identifier,plugin,defaultText,conditions); - - plugin.getRocketPlaceholdersAPI().addPlaceholder(placeholder); + Placeholder placeholder = new Placeholder(identifier,plugin,defaultText,conditions, null); - addedPlaceholders.add(placeholder); + plugin.getHook().addPlaceholder(placeholder); } public void unregisterAll() { - addedPlaceholders.forEach(item -> plugin.getRocketPlaceholdersAPI().removePlaceholder(item.getIdentifier())); + plugin.unregister(); this.plugin.getStorage().getInternal().clear(); } diff --git a/src/main/java/me/lorenzo0111/multilang/hooks/Hook.java b/src/main/java/me/lorenzo0111/multilang/hooks/Hook.java new file mode 100644 index 0000000..162ab79 --- /dev/null +++ b/src/main/java/me/lorenzo0111/multilang/hooks/Hook.java @@ -0,0 +1,11 @@ +package me.lorenzo0111.multilang.hooks; + +import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI; +import me.lorenzo0111.rocketplaceholders.creator.Placeholder; + +public interface Hook { + void register(IRocketPlaceholdersAPI api); + void unregister(IRocketPlaceholdersAPI api); + + void addPlaceholder(Placeholder placeholder); +} diff --git a/src/main/java/me/lorenzo0111/multilang/hooks/LegacyRocketPlaceholdersHook.java b/src/main/java/me/lorenzo0111/multilang/hooks/LegacyRocketPlaceholdersHook.java new file mode 100644 index 0000000..6db6f43 --- /dev/null +++ b/src/main/java/me/lorenzo0111/multilang/hooks/LegacyRocketPlaceholdersHook.java @@ -0,0 +1,35 @@ +package me.lorenzo0111.multilang.hooks; + +import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI; +import me.lorenzo0111.rocketplaceholders.creator.Placeholder; + +import java.util.List; + +public class LegacyRocketPlaceholdersHook implements Hook { + private final List placeholders; + + public LegacyRocketPlaceholdersHook(List placeholders) { + this.placeholders = placeholders; + } + + @Override + public void register(IRocketPlaceholdersAPI api) { + this.unregister(api); + + for (Placeholder placeholder : placeholders) { + api.addPlaceholder(placeholder); + } + } + + @Override + public void unregister(IRocketPlaceholdersAPI api) { + for (Placeholder placeholder : placeholders) { + api.removePlaceholder(placeholder.getIdentifier()); + } + } + + @Override + public void addPlaceholder(Placeholder placeholder) { + placeholders.add(placeholder); + } +} diff --git a/src/main/java/me/lorenzo0111/multilang/hooks/RocketPlaceholdersHook.java b/src/main/java/me/lorenzo0111/multilang/hooks/RocketPlaceholdersHook.java new file mode 100644 index 0000000..37be0eb --- /dev/null +++ b/src/main/java/me/lorenzo0111/multilang/hooks/RocketPlaceholdersHook.java @@ -0,0 +1,38 @@ +package me.lorenzo0111.multilang.hooks; + +import me.clip.placeholderapi.PlaceholderAPI; +import me.lorenzo0111.rocketplaceholders.RocketPlaceholders; +import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI; +import me.lorenzo0111.rocketplaceholders.creator.Placeholder; +import me.lorenzo0111.rocketplaceholders.exceptions.InvalidConditionException; +import me.lorenzo0111.rocketplaceholders.providers.Provider; +import me.lorenzo0111.rocketplaceholders.storage.StorageManager; +import org.bukkit.OfflinePlayer; + +public class RocketPlaceholdersHook extends Provider implements Hook { + + public RocketPlaceholdersHook(StorageManager manager) { + super(RocketPlaceholders.getInstance(), manager); + } + + @Override + public void register(IRocketPlaceholdersAPI api) { + api.registerProvider(this,"multilang"); + } + + @Override + public void unregister(IRocketPlaceholdersAPI api) { + this.manager.getInternalPlaceholders().clear(); + this.manager.getExternalPlaceholders().clear(); + } + + @Override + public void addPlaceholder(Placeholder placeholder) { + manager.getInternalPlaceholders().add(placeholder); + } + + @Override + public String parse(Placeholder placeholder, OfflinePlayer player, String text) throws InvalidConditionException { + return placeholder.parseJS(PlaceholderAPI.setPlaceholders(player,text)); + } +} diff --git a/src/main/java/me/lorenzo0111/multilang/requirements/LangRequirement.java b/src/main/java/me/lorenzo0111/multilang/requirements/LangRequirement.java index 6addcd4..bf3c79c 100644 --- a/src/main/java/me/lorenzo0111/multilang/requirements/LangRequirement.java +++ b/src/main/java/me/lorenzo0111/multilang/requirements/LangRequirement.java @@ -36,7 +36,7 @@ public class LangRequirement extends Requirement { private final MultiLangPlugin plugin; public LangRequirement(MultiLangPlugin plugin, Locale locale) { - super(null); + super(); this.locale = locale; this.plugin = plugin; } diff --git a/src/main/java/me/lorenzo0111/multilang/utils/PluginLoader.java b/src/main/java/me/lorenzo0111/multilang/utils/PluginLoader.java index 8a34960..a80501c 100644 --- a/src/main/java/me/lorenzo0111/multilang/utils/PluginLoader.java +++ b/src/main/java/me/lorenzo0111/multilang/utils/PluginLoader.java @@ -31,9 +31,12 @@ import me.lorenzo0111.multilang.commands.MultiLangCommand; import me.lorenzo0111.multilang.handlers.ConfigManager; import me.lorenzo0111.multilang.handlers.MessagesManager; +import me.lorenzo0111.multilang.hooks.LegacyRocketPlaceholdersHook; +import me.lorenzo0111.multilang.hooks.RocketPlaceholdersHook; import me.lorenzo0111.pluginslib.command.Customization; import me.lorenzo0111.pluginslib.config.ConfigExtractor; import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI; +import me.lorenzo0111.rocketplaceholders.storage.StorageManager; import org.bstats.bukkit.Metrics; import org.bstats.charts.SimplePie; import org.bukkit.Bukkit; @@ -44,6 +47,7 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Objects; public final class PluginLoader { @@ -65,6 +69,16 @@ public boolean init() { if (api != null) { plugin.getLogger().info("RocketPlaceholders hooked!"); plugin.setRocketPlaceholdersAPI(api); + + try { + Class.forName("me.lorenzo0111.rocketplaceholders.hooks.PapiHook"); + + plugin.getLogger().info("Found RocketPlaceholders 2.0, registering custom provider"); + plugin.setHook(new RocketPlaceholdersHook(new StorageManager())); + } catch (ClassNotFoundException ignored) { + plugin.setHook(new LegacyRocketPlaceholdersHook(new ArrayList<>())); + } + } else { plugin.getLogger().severe("Unable to find RocketPlaceholdersAPI, disabling.."); plugin.getServer().getPluginManager().disablePlugin(plugin);