Skip to content

Commit

Permalink
Added support for RocketPlaceholders 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo0111 committed Sep 14, 2021
1 parent 1efd5a4 commit e02efdf
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 12 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ext {
apply from: "https://cdn.rocketplugins.space/spigot"

group = "me.lorenzo0111"
version = "1.6"
version = "1.6.1"

java {
withJavadocJar()
Expand All @@ -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")
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/me/lorenzo0111/multilang/MultiLangPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class ConfigManager {
private Locale defaultLocale;
private final Map<String,String> keysMap = new HashMap<>();
private final MultiLangPlugin plugin;
private final List<Placeholder> addedPlaceholders = new ArrayList<>();
public ConfigManager(MultiLangPlugin plugin) {
this.plugin = plugin;
}
Expand Down Expand Up @@ -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");
}

Expand All @@ -113,15 +112,13 @@ public void save(String identifier,String defaultText,Map<Locale,String> 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();
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/me/lorenzo0111/multilang/hooks/Hook.java
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<Placeholder> placeholders;

public LegacyRocketPlaceholdersHook(List<Placeholder> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/me/lorenzo0111/multilang/utils/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +47,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;

public final class PluginLoader {
Expand All @@ -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);
Expand Down

0 comments on commit e02efdf

Please sign in to comment.