Skip to content

Commit

Permalink
Merge pull request #42 from webhead1104/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxikle authored May 9, 2024
2 parents 5ac0505 + 908c69f commit b73b630
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/cytonic/cytosis/config/CytosisSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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 = "";
Expand All @@ -46,6 +45,7 @@ public static void inportConfig(Map<String, Object> 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;
Expand Down Expand Up @@ -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");
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/net/cytonic/cytosis/events/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

/**
Expand Down Expand Up @@ -79,14 +78,12 @@ public boolean registerListener(EventListener<? extends Event> listener) {

public <T extends Event> void handleEvent(T event) {
List<EventListener<? extends Event>> matchingListeners = new ArrayList<>();

for (EventListener<? extends Event> 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));

Expand All @@ -96,7 +93,6 @@ public <T extends Event> void handleEvent(T event) {
}
}


private void setupInternalListeners() {
//book events
GLOBAL_HANDLER.addListener(EditBookEvent.class, (this::handleEvent));
Expand All @@ -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));
Expand All @@ -127,22 +121,19 @@ 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));
GLOBAL_HANDLER.addListener(InventoryItemChangeEvent.class, (this::handleEvent));
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));
Expand Down Expand Up @@ -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));
Expand All @@ -199,4 +189,4 @@ private void setupInternalListeners() {
GLOBAL_HANDLER.addListener(RankSetupEvent.class, (this::handleEvent));
GLOBAL_HANDLER.addListener(RankChangeEvent.class, (this::handleEvent));
}
}
}
4 changes: 1 addition & 3 deletions src/main/java/net/cytonic/cytosis/events/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.Getter;
import net.minestom.server.event.Event;

import java.util.function.Consumer;

@Getter
Expand Down Expand Up @@ -31,7 +30,6 @@ public EventListener(String namespace, boolean async, int priority, Class<T> eve
this.namespace = namespace;
}


/**
* Constructs a new instance of {@link EventListener} with the specified namespace, priority, and consumer. It will be executed synchronously.
*
Expand Down Expand Up @@ -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));
}
}
}
8 changes: 1 addition & 7 deletions src/main/java/net/cytonic/cytosis/files/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public CompletableFuture<File> createConfigFile() {
future.complete(file);
} catch (IllegalStateException | IOException e) {
Logger.error("An error occurred whilst parsing the config.toml file!", e);

future.completeExceptionally(e);
}
});
Expand All @@ -75,7 +74,6 @@ public CompletableFuture<File> createConfigFile() {
future.complete(CONFIG_PATH.toFile());
}
});

return future;
}

Expand Down Expand Up @@ -122,23 +120,19 @@ private Map<String, Object> recursiveParse(Map<String, Object> map, String paren
if (!parentKey.equalsIgnoreCase("")) {
parentKey = STR."\{parentKey}.";
}

Map<String, Object> resultMap = new HashMap<>();

for (Map.Entry<String, Object> 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));
}
// 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -28,4 +28,4 @@ public CompletableFuture<Void> initialize() {
});
return future;
}
}
}
10 changes: 2 additions & 8 deletions src/main/java/net/cytonic/cytosis/messaging/RabbitMQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -76,4 +70,4 @@ public void sendServerDeclarationMessage() {
}
Logger.info(STR."Server Declaration message sent! '\{message}'.");
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/config.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b73b630

Please sign in to comment.