mirror of
https://github.com/plexusorg/Plex.git
synced 2026-06-03 21:16:55 +00:00
Refactor more
This commit is contained in:
@@ -45,6 +45,7 @@ public class Plex
|
||||
this.server = server;
|
||||
this.logger = logger;
|
||||
this.dataFolder = folder.toFile();
|
||||
PlexLog.configure(server, () -> config != null && config.settings().getServer().isDebug());
|
||||
if (!dataFolder.exists())
|
||||
{
|
||||
dataFolder.mkdir();
|
||||
@@ -58,7 +59,7 @@ public class Plex
|
||||
this.config = loadConfig("config.yml");
|
||||
this.messages = loadConfig("messages.yml");
|
||||
this.api = new DefaultPlexApi(this, MODULE_API_COMPATIBILITY_VERSION);
|
||||
new ListenerHandler();
|
||||
new ListenerHandler(this);
|
||||
}
|
||||
|
||||
private YamlConfig loadConfig(String name)
|
||||
|
||||
@@ -18,16 +18,6 @@ public abstract class ProxyCommand implements SimpleCommand
|
||||
private final CommandSpec commandSpec;
|
||||
private final RequiredCommandSource commandSource;
|
||||
|
||||
/**
|
||||
* Creates and registers a proxy command using the current proxy plugin.
|
||||
*
|
||||
* @param commandSpec explicit command metadata
|
||||
*/
|
||||
protected ProxyCommand(CommandSpec commandSpec)
|
||||
{
|
||||
this(Plex.get(), commandSpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and registers a proxy command.
|
||||
*
|
||||
|
||||
@@ -1,32 +1,28 @@
|
||||
package dev.plex.handlers;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.listener.ProxyListener;
|
||||
import dev.plex.listener.impl.ConnectionListener;
|
||||
import dev.plex.listener.impl.ServerListener;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.ReflectionsUtil;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ListenerHandler
|
||||
{
|
||||
public ListenerHandler()
|
||||
{
|
||||
Set<Class<? extends ProxyListener>> listenerSet = ReflectionsUtil.getClassesBySubType("dev.plex.listener.impl", ProxyListener.class);
|
||||
List<ProxyListener> listeners = Lists.newArrayList();
|
||||
private final Plex plugin;
|
||||
private final List<ProxyListener> listeners = new ArrayList<>();
|
||||
|
||||
listenerSet.forEach(clazz ->
|
||||
{
|
||||
try
|
||||
{
|
||||
listeners.add(clazz.getConstructor().newInstance());
|
||||
}
|
||||
catch (InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException ex)
|
||||
{
|
||||
PlexLog.error("Failed to register " + clazz.getSimpleName() + " as a listener!");
|
||||
}
|
||||
});
|
||||
PlexLog.log(String.format("Registered %s listeners from %s classes!", listeners.size(), listenerSet.size()));
|
||||
public ListenerHandler(Plex plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
registerBuiltInListeners();
|
||||
PlexLog.log("Registered " + listeners.size() + " proxy listeners.");
|
||||
}
|
||||
|
||||
private void registerBuiltInListeners()
|
||||
{
|
||||
listeners.add(new ConnectionListener(plugin));
|
||||
listeners.add(new ServerListener(plugin));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,11 @@ import dev.plex.Plex;
|
||||
|
||||
public class ProxyListener
|
||||
{
|
||||
protected final Plex plugin = Plex.get();
|
||||
protected final Plex plugin;
|
||||
|
||||
public ProxyListener()
|
||||
protected ProxyListener(Plex plugin)
|
||||
{
|
||||
Plex.get().getServer().getEventManager().register(Plex.get(), this);
|
||||
this.plugin = plugin;
|
||||
plugin.getServer().getEventManager().register(plugin, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,18 @@ import com.velocitypowered.api.event.PostOrder;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||
import com.velocitypowered.api.event.player.ServerConnectedEvent;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.listener.ProxyListener;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
public class ConnectionListener extends ProxyListener
|
||||
{
|
||||
public ConnectionListener(Plex plugin)
|
||||
{
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Subscribe(order = PostOrder.FIRST)
|
||||
public void onPlayerJoin(ServerConnectedEvent event)
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.velocitypowered.api.event.PostOrder;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyPingEvent;
|
||||
import com.velocitypowered.api.proxy.server.ServerPing;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.listener.ProxyListener;
|
||||
import dev.plex.settings.ServerSettings;
|
||||
import dev.plex.util.RandomUtil;
|
||||
@@ -18,6 +19,11 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
public class ServerListener extends ProxyListener
|
||||
{
|
||||
public ServerListener(Plex plugin)
|
||||
{
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Subscribe(order = PostOrder.FIRST)
|
||||
public void onPing(ProxyPingEvent event)
|
||||
{
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
public class PlexLog
|
||||
{
|
||||
private static ProxyServer server;
|
||||
private static BooleanSupplier debugEnabled = () -> false;
|
||||
|
||||
public static void configure(ProxyServer server, BooleanSupplier debugEnabled)
|
||||
{
|
||||
PlexLog.server = server;
|
||||
PlexLog.debugEnabled = debugEnabled == null ? () -> false : debugEnabled;
|
||||
}
|
||||
|
||||
public static void log(String message, Object... strings)
|
||||
{
|
||||
for (int i = 0; i < strings.length; i++)
|
||||
@@ -16,12 +26,12 @@ public class PlexLog
|
||||
message = message.replace("{" + i + "}", strings[i].toString());
|
||||
}
|
||||
}
|
||||
Plex.get().getServer().getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<yellow>[Plex] <gray>" + message));
|
||||
server.getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<yellow>[Plex] <gray>" + message));
|
||||
}
|
||||
|
||||
public static void log(Component component)
|
||||
{
|
||||
Plex.get().getServer().getConsoleCommandSource().sendMessage(Component.text("[Plex] ").color(NamedTextColor.YELLOW).append(component).colorIfAbsent(NamedTextColor.GRAY));
|
||||
server.getConsoleCommandSource().sendMessage(Component.text("[Plex] ").color(NamedTextColor.YELLOW).append(component).colorIfAbsent(NamedTextColor.GRAY));
|
||||
}
|
||||
|
||||
public static void error(String message, Object... strings)
|
||||
@@ -33,7 +43,7 @@ public class PlexLog
|
||||
message = message.replace("{" + i + "}", strings[i].toString());
|
||||
}
|
||||
}
|
||||
Plex.get().getServer().getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<red>[Plex Error] <gold>" + message));
|
||||
server.getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<red>[Plex Error] <gold>" + message));
|
||||
}
|
||||
|
||||
public static void warn(String message, Object... strings)
|
||||
@@ -45,7 +55,7 @@ public class PlexLog
|
||||
message = message.replace("{" + i + "}", strings[i].toString());
|
||||
}
|
||||
}
|
||||
Plex.get().getServer().getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<#eb7c0e>[Plex Warning] <gold>" + message));
|
||||
server.getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<#eb7c0e>[Plex Warning] <gold>" + message));
|
||||
}
|
||||
|
||||
public static void debug(String message, Object... strings)
|
||||
@@ -57,9 +67,9 @@ public class PlexLog
|
||||
message = message.replace("{" + i + "}", strings[i].toString());
|
||||
}
|
||||
}
|
||||
if (Plex.get().getConfig().settings().getServer().isDebug())
|
||||
if (debugEnabled.getAsBoolean())
|
||||
{
|
||||
Plex.get().getServer().getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<dark_purple>[Plex Debug] <gold>" + message));
|
||||
server.getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<dark_purple>[Plex Debug] <gold>" + message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.reflect.ClassPath;
|
||||
import dev.plex.Plex;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ReflectionsUtil
|
||||
{
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public static Set<Class<?>> getClassesFrom(String packageName)
|
||||
{
|
||||
Set<Class<?>> classes = new HashSet<>();
|
||||
try
|
||||
{
|
||||
ClassPath path = ClassPath.from(Plex.class.getClassLoader());
|
||||
ImmutableSet<ClassPath.ClassInfo> infoSet = path.getTopLevelClasses(packageName);
|
||||
infoSet.forEach(info ->
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<?> clazz = Class.forName(info.getName());
|
||||
classes.add(clazz);
|
||||
}
|
||||
catch (ClassNotFoundException ex)
|
||||
{
|
||||
PlexLog.error("Unable to find class " + info.getName() + " in " + packageName);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
PlexLog.error("Something went wrong while fetching classes from " + packageName);
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
return Collections.unmodifiableSet(classes);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Set<Class<? extends T>> getClassesBySubType(String packageName, Class<T> subType)
|
||||
{
|
||||
Set<Class<?>> loadedClasses = getClassesFrom(packageName);
|
||||
Set<Class<? extends T>> classes = new HashSet<>();
|
||||
loadedClasses.forEach(clazz ->
|
||||
{
|
||||
if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()).contains(subType))
|
||||
{
|
||||
classes.add((Class<? extends T>) clazz);
|
||||
}
|
||||
});
|
||||
return Collections.unmodifiableSet(classes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user