Added the ability to close and connect to a different server

This commit is contained in:
lieght
2025-09-14 01:43:13 +02:00
parent a13eee3ecd
commit f7e42926a4
9 changed files with 258 additions and 155 deletions

View File

@@ -1,13 +1,77 @@
package org.toop.eventbus;
import org.toop.server.Server;
import org.toop.server.ServerCommand;
import java.lang.reflect.Constructor;
import java.util.Arrays;
/**
* Events that are used in the GlobalEventBus class.
*/
public class Events implements IEvents {
/**
*
* WIP, DO NOT USE!
*
* @param eventName
* @param args
* @return
* @throws Exception
*/
public static Object get(String eventName, Object... args) throws Exception {
Class<?> clazz = Class.forName("org.toop.eventbus.Events$ServerEvents$" + eventName);
Class<?>[] paramTypes = Arrays.stream(args).map(Object::getClass).toArray(Class<?>[]::new);
Constructor<?> constructor = clazz.getConstructor(paramTypes);
return constructor.newInstance(args);
}
/**
*
* WIP, DO NOT USE!
*
* @param eventCategory
* @param eventName
* @param args
* @return
* @throws Exception
*/
public static Object get(String eventCategory, String eventName, Object... args) throws Exception {
Class<?> clazz = Class.forName("org.toop.eventbus.Events$" + eventCategory + "$" + eventName);
Class<?>[] paramTypes = Arrays.stream(args).map(Object::getClass).toArray(Class<?>[]::new);
Constructor<?> constructor = clazz.getConstructor(paramTypes);
return constructor.newInstance(args);
}
/**
*
* WIP, DO NOT USE!
*
* @param eventName
* @param args
* @return
* @throws Exception
*/
public static Object get2(String eventName, Object... args) throws Exception {
// Fully qualified class name
String className = "org.toop.server.backend.Events$ServerEvents$" + eventName;
// Load the class
Class<?> clazz = Class.forName(className);
// Build array of argument types
Class<?>[] paramTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
paramTypes[i] = args[i].getClass();
}
// Get the constructor
Constructor<?> constructor = clazz.getConstructor(paramTypes);
// Create a new instance
return constructor.newInstance(args);
}
public static class ServerEvents {
/**
@@ -20,33 +84,46 @@ public class Events implements IEvents {
*/
public record OnCommand(org.toop.server.ServerCommand command, String[] args, String result) {}
/**
* Triggers when the server client receives a message.
*/
public record ReceivedMessage(String message) {}
/**
* Triggers on changing the server backend.
*/
public record OnChangingServerBackend(Server.ServerBackend backend) {}
/**
* Triggers changing the server ip.
*/
public record changeServerIp(String ip) {}
/**
* Triggers on changing the server ip.
*/
public record OnChangingServerIp(String ip) {}
/**
* Triggers changing the server port.
*/
public record changeServerPort(String port) {}
/**
* Triggers on changing the server port.
*/
public record OnChangingServerPort(String port) {}
/**
* Triggers reconnecting to previous address.
*/
public record Reconnect() {}
/**
* Triggers changing connection to a new address.
*/
public record ChangeConnection(String ip, String port) { }
}
public static class EventBusEvents {
}
public static class WindowEvents {
}
public static class TttEvents {
}
public static class AiTttEvents {
}
}