diff --git a/src/main/java/net/cytonic/cytosis/config/CytosisSettings.java b/src/main/java/net/cytonic/cytosis/config/CytosisSettings.java index eb31923a..e2af524b 100644 --- a/src/main/java/net/cytonic/cytosis/config/CytosisSettings.java +++ b/src/main/java/net/cytonic/cytosis/config/CytosisSettings.java @@ -13,8 +13,8 @@ public class CytosisSettings { public static boolean LOG_PLAYER_QUITS = true; public static boolean LOG_PLAYER_COMMANDS = true; public static boolean LOG_PLAYER_CHAT = true; - // Database + public static boolean DATABASE_ENABLED = true; public static String DATABASE_USER = ""; public static String DATABASE_PASSWORD = ""; public static String DATABASE_HOST = ""; @@ -25,7 +25,6 @@ public class CytosisSettings { public static boolean SERVER_PROXY_MODE = false; public static String SERVER_SECRET = ""; public static int SERVER_PORT = 25565; - // RabbitMQ public static boolean RABBITMQ_ENABLED = false; public static String RABBITMQ_HOST = ""; @@ -46,6 +45,7 @@ public static void inportConfig(Map config) { case "logging.player_chat" -> LOG_PLAYER_CHAT = (boolean) value; // database + case "database.enabled" -> DATABASE_ENABLED = (boolean) value; case "database.user" -> DATABASE_USER = (String) value; case "database.password" -> DATABASE_PASSWORD = (String) value; case "database.host" -> DATABASE_HOST = (String) value; @@ -87,6 +87,7 @@ public static void loadEnvironmentVariables() { if (!(System.getenv("LOG_PLAYER_COMMANDS") == null)) CytosisSettings.LOG_PLAYER_COMMANDS = Boolean.parseBoolean(System.getenv("LOG_PLAYER_COMMANDS")); if (!(System.getenv("LOG_PLAYER_CHAT") == null)) CytosisSettings.LOG_PLAYER_CHAT = Boolean.parseBoolean(System.getenv("LOG_PLAYER_CHAT")); // database + if (!(System.getenv("DATABASE_ENABLED") == null)) CytosisSettings.DATABASE_ENABLED = Boolean.parseBoolean(System.getenv("DATABASE_ENABLED")); if (!(System.getenv("DATABASE_USER") == null)) CytosisSettings.DATABASE_USER = System.getenv("DATABASE_USER"); if (!(System.getenv("DATABASE_PASSWORD") == null)) CytosisSettings.DATABASE_PASSWORD = System.getenv("DATABASE_PASSWORD"); if (!(System.getenv("DATABASE_HOST") == null)) CytosisSettings.DATABASE_HOST = System.getenv("DATABASE_HOST"); diff --git a/src/main/java/net/cytonic/cytosis/events/EventHandler.java b/src/main/java/net/cytonic/cytosis/events/EventHandler.java index c278f5e8..021f3c54 100644 --- a/src/main/java/net/cytonic/cytosis/events/EventHandler.java +++ b/src/main/java/net/cytonic/cytosis/events/EventHandler.java @@ -17,7 +17,6 @@ import net.minestom.server.event.server.ServerListPingEvent; import net.minestom.server.event.server.ServerTickMonitorEvent; import net.minestom.server.event.trait.CancellableEvent; - import java.util.*; /** @@ -79,14 +78,12 @@ public boolean registerListener(EventListener listener) { public void handleEvent(T event) { List> matchingListeners = new ArrayList<>(); - for (EventListener listener : NAMESPACED_HANDLERS.values()) { if (listener.getEventClass() == event.getClass() && !(event instanceof CancellableEvent && ((CancellableEvent) event).isCancelled())) { matchingListeners.add(listener); } } - // Sort listeners by priority matchingListeners.sort(Comparator.comparingInt(EventListener::getPriority)); @@ -96,7 +93,6 @@ public void handleEvent(T event) { } } - private void setupInternalListeners() { //book events GLOBAL_HANDLER.addListener(EditBookEvent.class, (this::handleEvent)); @@ -113,12 +109,10 @@ private void setupInternalListeners() { GLOBAL_HANDLER.addListener(EntitySpawnEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(EntityTickEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(EntityVelocityEvent.class, (this::handleEvent)); - //projectile events GLOBAL_HANDLER.addListener(ProjectileCollideWithBlockEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(ProjectileCollideWithEntityEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(ProjectileUncollideEvent.class, (this::handleEvent)); - // Instance events GLOBAL_HANDLER.addListener(AddEntityToInstanceEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(InstanceChunkLoadEvent.class, (this::handleEvent)); @@ -127,7 +121,6 @@ private void setupInternalListeners() { GLOBAL_HANDLER.addListener(InstanceTickEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(InstanceUnregisterEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(RemoveEntityFromInstanceEvent.class, (this::handleEvent)); - // Inventory Events GLOBAL_HANDLER.addListener(InventoryClickEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(InventoryCloseEvent.class, (this::handleEvent)); @@ -135,14 +128,12 @@ private void setupInternalListeners() { GLOBAL_HANDLER.addListener(InventoryOpenEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(InventoryPreClickEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(PlayerInventoryItemChangeEvent.class, (this::handleEvent)); - // Item Events GLOBAL_HANDLER.addListener(EntityEquipEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(ItemDropEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(ItemUpdateStateEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(PickupExperienceEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(PickupItemEvent.class, (this::handleEvent)); - // player events GLOBAL_HANDLER.addListener(AdvancementTabEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(AsyncPlayerConfigurationEvent.class, (this::handleEvent)); @@ -189,7 +180,6 @@ private void setupInternalListeners() { GLOBAL_HANDLER.addListener(PlayerUseItemEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(PlayerUseItemOnBlockEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(UpdateTagListEvent.class, (this::handleEvent)); // deprecated - // Server GLOBAL_HANDLER.addListener(ClientPingServerEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(ServerListPingEvent.class, (this::handleEvent)); @@ -199,4 +189,4 @@ private void setupInternalListeners() { GLOBAL_HANDLER.addListener(RankSetupEvent.class, (this::handleEvent)); GLOBAL_HANDLER.addListener(RankChangeEvent.class, (this::handleEvent)); } -} +} \ No newline at end of file diff --git a/src/main/java/net/cytonic/cytosis/events/EventListener.java b/src/main/java/net/cytonic/cytosis/events/EventListener.java index 5487b649..74d12dec 100644 --- a/src/main/java/net/cytonic/cytosis/events/EventListener.java +++ b/src/main/java/net/cytonic/cytosis/events/EventListener.java @@ -2,7 +2,6 @@ import lombok.Getter; import net.minestom.server.event.Event; - import java.util.function.Consumer; @Getter @@ -31,7 +30,6 @@ public EventListener(String namespace, boolean async, int priority, Class eve this.namespace = namespace; } - /** * Constructs a new instance of {@link EventListener} with the specified namespace, priority, and consumer. It will be executed synchronously. * @@ -60,4 +58,4 @@ public void complete(Object event) { throw new IllegalArgumentException(STR."The specified event object isn't an instance of \{eventClass.getName()}"); consumer.accept(eventClass.cast(event)); } -} +} \ No newline at end of file diff --git a/src/main/java/net/cytonic/cytosis/files/FileManager.java b/src/main/java/net/cytonic/cytosis/files/FileManager.java index 8afd21a6..2514c57c 100644 --- a/src/main/java/net/cytonic/cytosis/files/FileManager.java +++ b/src/main/java/net/cytonic/cytosis/files/FileManager.java @@ -57,7 +57,6 @@ public CompletableFuture createConfigFile() { future.complete(file); } catch (IllegalStateException | IOException e) { Logger.error("An error occurred whilst parsing the config.toml file!", e); - future.completeExceptionally(e); } }); @@ -75,7 +74,6 @@ public CompletableFuture createConfigFile() { future.complete(CONFIG_PATH.toFile()); } }); - return future; } @@ -122,13 +120,10 @@ private Map recursiveParse(Map map, String paren if (!parentKey.equalsIgnoreCase("")) { parentKey = STR."\{parentKey}."; } - Map resultMap = new HashMap<>(); - for (Map.Entry entry : map.entrySet()) { String key = STR."\{parentKey}\{entry.getKey()}"; Object value = entry.getValue(); - // If the value is a nested table (another map), recurse if (value instanceof TomlTable toml) { resultMap.putAll(recursiveParse(toml.toMap(), key)); @@ -136,9 +131,8 @@ private Map recursiveParse(Map map, String paren // If it's a list, check for nested tables within the list else if (value instanceof Iterable iterable) { for (Object item : iterable) { - if (item instanceof TomlTable toml) { + if (item instanceof TomlTable toml) resultMap.putAll(recursiveParse(toml.toMap(), key)); - } } } else { resultMap.put(key, value); diff --git a/src/main/java/net/cytonic/cytosis/messaging/MessagingManager.java b/src/main/java/net/cytonic/cytosis/messaging/MessagingManager.java index 78e054bc..02d60258 100644 --- a/src/main/java/net/cytonic/cytosis/messaging/MessagingManager.java +++ b/src/main/java/net/cytonic/cytosis/messaging/MessagingManager.java @@ -1,12 +1,12 @@ package net.cytonic.cytosis.messaging; import net.cytonic.cytosis.config.CytosisSettings; - import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MessagingManager { + private final RabbitMQ rabbitMQ; private final ExecutorService worker; @@ -28,4 +28,4 @@ public CompletableFuture initialize() { }); return future; } -} +} \ No newline at end of file diff --git a/src/main/java/net/cytonic/cytosis/messaging/RabbitMQ.java b/src/main/java/net/cytonic/cytosis/messaging/RabbitMQ.java index bcd50959..3f950dcc 100644 --- a/src/main/java/net/cytonic/cytosis/messaging/RabbitMQ.java +++ b/src/main/java/net/cytonic/cytosis/messaging/RabbitMQ.java @@ -5,13 +5,13 @@ import com.rabbitmq.client.ConnectionFactory; import net.cytonic.cytosis.config.CytosisSettings; import net.cytonic.cytosis.logging.Logger; - import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.concurrent.TimeoutException; public class RabbitMQ { + public static final String SERVER_DECLARE_QUEUE = "server-declaration"; public static final String SHUTDOWN_QUEUE = "server-shutdown"; private Connection connection; @@ -23,15 +23,12 @@ public void initializeConnection() { factory.setPassword(CytosisSettings.RABBITMQ_PASSWORD); factory.setUsername(CytosisSettings.RABBITMQ_USERNAME); factory.setPort(CytosisSettings.RABBITMQ_PORT); - try { connection = factory.newConnection(); } catch (IOException | TimeoutException e) { Logger.error("An error occoured whilst connecting to RabbitMQ!", e); } - Logger.info("Connected to RabbitMQ!"); - try { channel = connection.createChannel(); connection = factory.newConnection(); @@ -58,16 +55,13 @@ public void sendServerDeclarationMessage() { //formatting: {server-name}|:|{server-ip}|:|{server-port} String serverName = System.getenv("HOSTNAME"); String serverIP; - try { serverIP = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { Logger.error("An error occoured whilst fetching this server's IP address! Bailing out!", e); return; } - int port = 25565; - String message = STR."\{serverName}|:|\{serverIP}|:|\{port}"; try { channel.basicPublish("", SERVER_DECLARE_QUEUE, null, message.getBytes()); @@ -76,4 +70,4 @@ public void sendServerDeclarationMessage() { } Logger.info(STR."Server Declaration message sent! '\{message}'."); } -} +} \ No newline at end of file diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml index 9928955b..9859398a 100644 --- a/src/main/resources/config.toml +++ b/src/main/resources/config.toml @@ -1,5 +1,6 @@ # Database Connection Details [database] +enabled = true host = "" # Database host or IP address port = 3306 # Default MySQL/MariaDB port name = "" # Name of the database