Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public static ConcurrentSkipListMap<String, ProxiedPlayer> getPlayersByUUID() {

@Override
public Logger getLoggerLogger() {
return getLogger();
return getProxy().getLogger();
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ ext {
"org.pf4j:pf4j:3.10.0",
"commons-codec:commons-codec:1.5",
"ch.qos.logback:logback-classic:1.5.6",
'redis.clients:jedis:6.0.0',
]
SHADOW = [
"com.github.ben-manes.caffeine:caffeine:3.1.8",
"com.github.Server-Utilities:TheBase:master-SNAPSHOT",
"org.pf4j:pf4j:3.10.0",
"commons-codec:commons-codec:1.5",
"ch.qos.logback:logback-classic:1.5.6",
'redis.clients:jedis:6.0.0',
]
ANNO = [
"com.github.ben-manes.caffeine:caffeine:3.1.8",
"com.github.Server-Utilities:TheBase:master-SNAPSHOT",
"org.pf4j:pf4j:3.10.0"
"org.pf4j:pf4j:3.10.0",
'redis.clients:jedis:6.0.0',
]
COMP_ONLY = [
]
Expand Down
8 changes: 1 addition & 7 deletions singularity-api/src/main/java/singularity/Singularity.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,7 @@ public Singularity(String identifier, S platform, U userManager, M messenger, IC
public void setupLogger() {
if (getPlatform().hasLoggerLogger()) {
java.util.logging.Logger rootLogger = getPlatform().getLoggerLogger();
while (rootLogger.getParent() != null) {
rootLogger = rootLogger.getParent();
}
// Remove existing handlers to avoid duplicates (optional)
// for (java.util.logging.Handler handler : rootLogger.getHandlers()) {
// rootLogger.removeHandler(handler);
// }

// Add the custom handler
CosmicLogHandler handler = new CosmicLogHandler();
rootLogger.addHandler(handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import singularity.database.ConnectorSet;
import singularity.database.CoreDBOperator;
import singularity.database.servers.SavedServer;
import singularity.redis.RedisClient;
import singularity.utils.UserUtils;

import java.io.File;
Expand All @@ -23,19 +24,25 @@ public class GivenConfigs {
private static DatabaseConfigHandler databaseConfig;
@Getter @Setter
private static ServerConfigHandler serverConfig;
@Getter @Setter
private static RedisConfigHandler redisConfig;

@Getter @Setter
private static File punishmentFolder;

@Getter @Setter
private static CoreDBOperator mainDatabase;

@Getter @Setter
private static RedisClient redisClient;

public static void init() {
setMainConfig(new MainConfigHandler());
setMainMessages(new MainMessagesHandler());
setWhitelistConfig(new WhitelistConfig());
setDatabaseConfig(new DatabaseConfigHandler());
setServerConfig(new ServerConfigHandler());
setRedisConfig(new RedisConfigHandler());

try {
ConnectorSet connectorSet = getDatabaseConfig().getConnectorSet();
Expand All @@ -46,6 +53,12 @@ public static void init() {
} catch (Exception e) {
e.printStackTrace();
}

try {
setRedisClient(new RedisClient());
} catch (Exception e) {
e.printStackTrace();
}
}

public static void ensureFolders() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package singularity.configs.given;

import gg.drak.thebase.storage.resources.flat.simple.SimpleConfiguration;
import lombok.Getter;
import lombok.Setter;
import singularity.Singularity;

@Getter @Setter
public class RedisConfigHandler extends SimpleConfiguration {
public RedisConfigHandler() {
super("redis-config.yml", Singularity.getInstance(), true);
}

@Override
public void init() {
getHost();
getPort();
getUsername();
getPassword();
}

// DATABASE

public String getHost() {
reloadResource();

return getResource().getOrSetDefault("host", "localhost");
}

public int getPort() {
reloadResource();

return getResource().getOrSetDefault("port", 6379);
}

public String getUsername() {
reloadResource();

return getResource().getOrSetDefault("username", "default");
}

public String getPassword() {
reloadResource();

return getResource().getOrSetDefault("password", "password");
}

public boolean isEnabled() {
reloadResource();

return getResource().getOrSetDefault("enabled", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

public class CosmicListener implements BaseEventListener {
public CosmicListener() {
BaseEventHandler.bake(this, Singularity.getBaseModule());
BaseEventHandler.bake(this, Singularity.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

import singularity.events.server.ServerLogTextEvent;

import java.io.ByteArrayOutputStream;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;
import java.util.logging.Level;

public class CosmicLogHandler extends StreamHandler {
private final ByteArrayOutputStream baos;

public CosmicLogHandler() {
// Set output to System.out by default
super.setOutputStream(System.out);

this.baos = new ByteArrayOutputStream();
}

@Override
Expand Down Expand Up @@ -44,18 +39,5 @@ public synchronized void publish(LogRecord record) {
if (event.isCancelled()) {
return;
}

// Capture the log message
String formattedMessage = getFormatter().format(record);
baos.write(formattedMessage.getBytes(), 0, formattedMessage.length());

// Pass to parent handler for actual logging
super.publish(record);
super.flush(); // Ensure immediate output
}

// Method to get captured logs
public String getCapturedLogs() {
return baos.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import ch.qos.logback.core.AppenderBase;
import singularity.events.server.ServerLogTextEvent;

import java.io.ByteArrayOutputStream;

public class CosmicLogbackAppender extends AppenderBase<ILoggingEvent> {
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();

@Override
protected void append(ILoggingEvent eventObject) {
Level level = eventObject.getLevel();
Expand Down Expand Up @@ -37,16 +33,5 @@ protected void append(ILoggingEvent eventObject) {
if (event.isCancelled()) {
return;
}

// Capture the log message
String message = eventObject.getFormattedMessage() + System.lineSeparator();
baos.write(message.getBytes(), 0, message.length());

// Optionally forward to console
System.out.print(message);
}

public String getCapturedLogs() {
return baos.toString();
}
}
30 changes: 18 additions & 12 deletions singularity-api/src/main/java/singularity/logging/LogCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,41 @@
import singularity.logging.timers.LogPopTimer;
import singularity.utils.MessageUtils;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicInteger;

public class LogCollector extends CosmicListener {
@Getter @Setter
private static ConcurrentLinkedQueue<ServerLogTextEvent> logQueue = new ConcurrentLinkedQueue<>();
private static ConcurrentSkipListMap<Integer, ServerLogTextEvent> logQueue = new ConcurrentSkipListMap<>();

@Getter @Setter
private static AtomicInteger idCounter = new AtomicInteger(0);

public static int getNextId() {
return idCounter.getAndIncrement();
}

public static void addLog(ServerLogTextEvent event) {
logQueue.add(event);
getLogQueue().put(getNextId(), event);
}

/**
* Pops all logs from the queue and returns them as a map.
* @return A map of log entries where the key is the log ID and the value is the log message.
*/
public static ConcurrentSkipListMap<Integer, ServerLogTextEvent> getLogs() {
ConcurrentSkipListMap<Integer, ServerLogTextEvent> logs = new ConcurrentSkipListMap<>();
int id = 0;

while (! logQueue.isEmpty()) {
ServerLogTextEvent event = logQueue.poll();
if (event != null) {
logs.put(id++, event);
}
}
ConcurrentSkipListMap<Integer, ServerLogTextEvent> logs = new ConcurrentSkipListMap<>(getLogQueue());

resetQueue();

return logs;
}

public static void resetQueue() {
clearLogs();
setIdCounter(new AtomicInteger(0));
}

public static void clearLogs() {
logQueue.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
public class LogPopTimer extends AsyncTask {
public LogPopTimer() {
super(LogPopTimer::runTask, 0, 20 * 5); // Runs every 5 seconds
queue();
start();
}

public static void runTask(AsyncTask task) {
Expand Down
85 changes: 85 additions & 0 deletions singularity-api/src/main/java/singularity/redis/RedisClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package singularity.redis;

import lombok.Getter;
import lombok.Setter;
import redis.clients.jedis.Jedis;
import singularity.configs.given.GivenConfigs;
import singularity.configs.given.RedisConfigHandler;
import singularity.utils.MessageUtils;

import java.util.Optional;
import java.util.function.Consumer;

public class RedisClient {
public static RedisConfigHandler getConfig() {
return GivenConfigs.getRedisConfig();
}

@Getter @Setter
private static Jedis jedisClient;

public static String getHost() {
return getConfig().getHost();
}

public static int getPort() {
return getConfig().getPort();
}

public static String getUsername() {
return getConfig().getUsername();
}

public static String getPassword() {
return getConfig().getPassword();
}

public static boolean isEnabled() {
return getConfig().isEnabled();
}

public static Jedis getJedis() {
if (! isEnabled()) return null;

if (getJedisClient() != null) {
return getJedisClient();
}

Jedis jedis = new Jedis(getHost(), getPort());
String auth = jedis.auth(getUsername(), getPassword());
if (auth != null) {
if (auth.equals("OK")) {
MessageUtils.logInfo("Redis authenticated successfully.");
} else {
MessageUtils.logInfo("Redis authentication failed: " + auth);
}
}

String pingResponse = jedis.ping();
MessageUtils.logInfo("Redis ping response: " + pingResponse);

setJedisClient(jedis);

return getJedisClient();
}

public static void withJedis(Consumer<Jedis> consumer) {
withJedis(consumer, true);
}

public static void withJedis(Consumer<Jedis> consumer, boolean silent) {
if (isEnabled()) Optional.ofNullable(getJedis()).ifPresentOrElse(consumer, () -> {
if (! silent) {
MessageUtils.logWarning("Redis client is not initialized. Please check your Redis configuration.");
}
});
}

public static RedisClient getInstance() {
return GivenConfigs.getRedisClient();
}

public static void sendMessage(RedisMessage message) {
withJedis(j -> j.publish(message.getChannel(), message.getMessage()));
}
}
Loading