Use Reflections for Plex
This commit is contained in:
parent
f2e1cd2c3b
commit
9a63b857d5
|
@ -13,17 +13,12 @@ repositories {
|
|||
maven {
|
||||
url = uri("https://papermc.io/repo/repository/maven-public/")
|
||||
}
|
||||
|
||||
maven {
|
||||
url = uri("https://nexus.telesphoreo.me/repository/plex/")
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.projectlombok:lombok:1.18.22")
|
||||
annotationProcessor("org.projectlombok:lombok:1.18.22")
|
||||
implementation("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT")
|
||||
implementation("dev.plex:Plex:1.0.2-SNAPSHOT")
|
||||
library("org.json:json:20220320")
|
||||
library("commons-io:commons-io:2.11.0")
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public class Emotes extends JavaPlugin
|
|||
if (!getServer().getPluginManager().isPluginEnabled("TotalFreedomMod")
|
||||
&& !getServer().getPluginManager().isPluginEnabled("Plex"))
|
||||
{
|
||||
getLogger().log(Level.WARNING, "Plex nor TotalFreedomMod was found. Defaulting to if the player is op for permissions");
|
||||
getLogger().log(Level.WARNING, "Plex nor TotalFreedomMod was found, using permissions.");
|
||||
}
|
||||
this.tfmBridge = new TFMBridge();
|
||||
tfmBridge.getTFM();
|
||||
|
@ -39,6 +39,16 @@ public class Emotes extends JavaPlugin
|
|||
plexBridge.getPlex();
|
||||
this.cooldownTask = new CooldownTask();
|
||||
this.cooldownTask.runTaskTimerAsynchronously(plugin, 0L, 20L);
|
||||
loadCommands();
|
||||
}
|
||||
|
||||
public void onDisable()
|
||||
{
|
||||
this.cooldownTask.cancel();
|
||||
}
|
||||
|
||||
public void loadCommands()
|
||||
{
|
||||
try
|
||||
{
|
||||
EmoteLoader.registerDefaultEmote("greet");
|
||||
|
@ -58,9 +68,4 @@ public class Emotes extends JavaPlugin
|
|||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void onDisable()
|
||||
{
|
||||
this.cooldownTask.cancel();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
package dev.plex.emotes.bridge;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.emotes.EmotesBase;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.emotes.util.ReflectionsHelper;
|
||||
import java.util.Locale;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PlexBridge implements EmotesBase
|
||||
|
@ -33,17 +32,29 @@ public class PlexBridge implements EmotesBase
|
|||
return plex;
|
||||
}
|
||||
|
||||
public boolean checkRank(CommandSender sender, Rank rank, String permission)
|
||||
public boolean checkRank(CommandSender sender, String rankName, String permission)
|
||||
{
|
||||
if (getPlex() == null)
|
||||
{
|
||||
Bukkit.getLogger().warning("Plex not detected either. Using standard permission check for permission: " + permission);
|
||||
return sender.hasPermission(permission);
|
||||
}
|
||||
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
|
||||
String system = ReflectionsHelper.invokeMethod(plex, "getSystem");
|
||||
if (system == null)
|
||||
{
|
||||
PlexPlayer plexPlayer = DataUtils.getPlayer(sender.getName());
|
||||
return plexPlayer.getRankFromString().isAtLeast(rank);
|
||||
Bukkit.getLogger().severe("Unable to find Plex's system. Reflections may be setup incorrectly. Using standard permission check for permission: " + permission);
|
||||
return sender.hasPermission(permission);
|
||||
}
|
||||
if (system.equalsIgnoreCase("ranks"))
|
||||
{
|
||||
if (sender instanceof ConsoleCommandSender)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
Object plexPlayer = ReflectionsHelper.invokeStaticMethod("dev.plex.cache.DataUtils", "getPlayer", sender.getName());
|
||||
Enum<?> rank = ReflectionsHelper.invokeMethod(plexPlayer, "getRankFromString");
|
||||
Object rankFromEnum = ReflectionsHelper.getEnumValue("dev.plex.rank.enums.Rank", rankName.toUpperCase(Locale.ROOT));
|
||||
return Boolean.TRUE.equals(ReflectionsHelper.invokeMethod(rank, "isAtLeast", rankFromEnum));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -4,7 +4,6 @@ import dev.plex.emotes.EmotesBase;
|
|||
import dev.plex.emotes.util.EmoteLoader;
|
||||
import dev.plex.emotes.util.EmoteMeta;
|
||||
import dev.plex.emotes.util.MessageUtils;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -76,17 +75,28 @@ public class Command_emotes implements CommandExecutor, EmotesBase
|
|||
{
|
||||
sender.sendMessage(ChatColor.GREEN + "Reloading Emotes!");
|
||||
plugin.reloadConfig();
|
||||
plugin.loadCommands();
|
||||
return true;
|
||||
}
|
||||
else if (Bukkit.getServer().getPluginManager().isPluginEnabled("Plex") && plugin.plexBridge.checkRank(sender, Rank.ADMIN, "plex.emotes.reload"))
|
||||
else if (Bukkit.getServer().getPluginManager().isPluginEnabled("Plex") && plugin.plexBridge.checkRank(sender, "ADMIN", "plex.emotes.reload"))
|
||||
{
|
||||
sender.sendMessage(ChatColor.GREEN + "Reloading Emotes!");
|
||||
plugin.reloadConfig();
|
||||
plugin.loadCommands();
|
||||
return true;
|
||||
}
|
||||
else if (sender.hasPermission("emotes.reload"))
|
||||
{
|
||||
sender.sendMessage(ChatColor.GREEN + "Reloading Emotes!");
|
||||
plugin.reloadConfig();
|
||||
plugin.loadCommands();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class EmoteLoader
|
|||
emoteMeta.setDescription(json.getString("description"));
|
||||
emoteMeta.setAuthor(json.getString("author"));
|
||||
emoteMeta.setUsage(json.getString("usage"));
|
||||
HashMap<List<EmoteVariable>, String[]> lines = (HashMap)new HashMap<>();
|
||||
HashMap<List<EmoteVariable>, String[]> lines = new HashMap<>();
|
||||
JSONObject jsonLines = json.getJSONObject("lines");
|
||||
for (String key : jsonLines.keySet())
|
||||
{
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package dev.plex.emotes.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ReflectionsHelper
|
||||
{
|
||||
|
@ -34,4 +36,48 @@ public class ReflectionsHelper
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T, I> I invokeMethod(T instance, String name, Object... parameters)
|
||||
{
|
||||
try
|
||||
{
|
||||
Method f = instance.getClass().getDeclaredMethod(name, Arrays.stream(parameters).map(Object::getClass).toList().toArray(Class[]::new));
|
||||
f.setAccessible(true);
|
||||
return (I)f.invoke(instance, parameters);
|
||||
}
|
||||
catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T, I> I invokeStaticMethod(String classPath, String name, Object... parameters)
|
||||
{
|
||||
try
|
||||
{
|
||||
Method f = Class.forName(classPath).getDeclaredMethod(name, Arrays.stream(parameters).map(Object::getClass).toList().toArray(Class[]::new));
|
||||
f.setAccessible(true);
|
||||
return (I)f.invoke(null, parameters);
|
||||
}
|
||||
catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Enum<?> getEnumValue(String enumClassPath, String enumVal)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<Enum> clazz = (Class<Enum>)Class.forName(enumClassPath);
|
||||
return Enum.valueOf(clazz, enumVal);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue