Skip to content

Commit

Permalink
Implement player data synchronization on redis, add translatable bund…
Browse files Browse the repository at this point in the history
…le and override logging systems to json
  • Loading branch information
Xharos committed Jun 25, 2024
1 parent 3b34826 commit a820897
Show file tree
Hide file tree
Showing 28 changed files with 1,051 additions and 120 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ dependencies {
implementation("org.mongodb:mongodb-driver-reactivestreams:5.0.0")
implementation("io.lettuce:lettuce-core:6.3.2.RELEASE")
implementation("com.rabbitmq:amqp-client:5.21.0")
implementation("org.apache.logging.log4j:log4j-core:3.0.0-beta2")
implementation("fr.islandswars:commons:0.3.1")
compileOnly("com.velocitypowered:velocity-api:3.3.0-SNAPSHOT")
annotationProcessor("com.velocitypowered:velocity-api:3.3.0-SNAPSHOT")
implementation("fr.islandswars:commons:0.3.1")
}

tasks.withType<Jar> {
Expand Down
25 changes: 22 additions & 3 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ services:
depends_on:
mongo:
condition: service_healthy
redis:
condition: service_healthy

mc:
image: itzg/minecraft-server:java21-jdk
Expand All @@ -37,9 +39,6 @@ services:
PAPER_DOWNLOAD_URL: "https://api.papermc.io/v2/projects/paper/versions/1.21/builds/37/downloads/paper-1.21-37.jar"
volumes:
- ./data:/data
depends_on:
mongo:
condition: service_healthy
networks:
- islands_network

Expand Down Expand Up @@ -71,6 +70,26 @@ services:
- ./mongodb/docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d/
command: [ --auth ]

redis:
build: redis/.
ports:
- '6379:6379'
environment:
REDIS_PASSWORD_FILE: /run/secrets/redis_password
REDIS_USERNAME_FILE: /run/secrets/redis_username
REDIS_DISABLE_DEFAULT_USER: true
secrets:
- redis_password
- redis_username
networks:
- islands_network
healthcheck:
test: [ "CMD-SHELL", "redis-cli -u redis://$(cat /run/secrets/redis_username):$(cat /run/secrets/redis_password)@redis:6379 PING | grep -q PONG" ]
interval: 2s
timeout: 12s
retries: 5
start_period: 10s

networks:
islands_network:
driver: bridge
Expand Down
7 changes: 7 additions & 0 deletions redis/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM redis:latest

# Copy the custom entrypoint script
COPY custom-entrypoint.sh /usr/local/bin/custom-entrypoint.sh
RUN chmod +x /usr/local/bin/custom-entrypoint.sh

ENTRYPOINT ["/usr/local/bin/custom-entrypoint.sh"]
28 changes: 28 additions & 0 deletions redis/custom-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

# Paths to the secrets files
REDIS_USERNAME_FILE=/run/secrets/redis_username
REDIS_PASSWORD_FILE=/run/secrets/redis_password

# Read the contents of the secrets files
REDIS_USERNAME=$(cat $REDIS_USERNAME_FILE)
REDIS_PASSWORD=$(cat $REDIS_PASSWORD_FILE)

# Set up Redis configuration directory
mkdir -p /usr/local/etc/redis

# Dynamically generate Redis configuration and ACL files
echo "aclfile /usr/local/etc/redis/custom_aclfile.acl" > /usr/local/etc/redis/redis.conf

# Generate ACL file using secrets contents
if [ -n "$REDIS_USERNAME" ] && [ -n "$REDIS_PASSWORD" ]; then
echo "user $REDIS_USERNAME on allkeys allchannels allcommands >$REDIS_PASSWORD" > /usr/local/etc/redis/custom_aclfile.acl
fi

# Disable default user
if [ "$REDIS_DISABLE_DEFAULT_USER" = "true" ]; then
echo "user default off nopass nocommands" >> /usr/local/etc/redis/custom_aclfile.acl
fi

# Call the original Docker entrypoint script with redis-server and the path to the custom Redis configuration
exec docker-entrypoint.sh redis-server /usr/local/etc/redis/redis.conf
69 changes: 49 additions & 20 deletions src/main/java/fr/islandswars/ineundo/Ineundo.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package fr.islandswars.ineundo;

import com.google.inject.Inject;
import com.mongodb.MongoClientSettings;
import com.mongodb.reactivestreams.client.MongoClients;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import fr.islandswars.commons.service.mongodb.MongoDBConnection;
import fr.islandswars.commons.service.redis.RedisConnection;
import fr.islandswars.commons.utils.LogUtils;
import fr.islandswars.ineundo.listener.PlayerDataListener;
import fr.islandswars.ineundo.locale.TranslationLoader;
import fr.islandswars.ineundo.log.InternalLogger;
import fr.islandswars.ineundo.player.IslandsPlayer;
import net.kyori.adventure.text.Component;
import org.bson.UuidRepresentation;
import org.apache.logging.log4j.Level;

import java.nio.file.Path;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* File <b>Ineundo</b> located on fr.islandswars.ineundo
Expand Down Expand Up @@ -53,47 +55,74 @@
)
public class Ineundo {

private final CopyOnWriteArrayList<IslandsPlayer> players;
private final MongoDBConnection mongoConnection;
private final Logger logger;
private final ProxyServer server;
private static Ineundo INSTANCE;
private final CopyOnWriteArrayList<IslandsPlayer> players;
private final MongoDBConnection mongoConnection;
private final RedisConnection redisConnection;
private final ProxyServer server;
private final InternalLogger infraLogger;
private final AtomicBoolean STAFF_ONLY;

@Inject
public Ineundo(Logger logger, ProxyServer server, @DataDirectory Path dataDirectory) {
public Ineundo(ProxyServer server, @DataDirectory Path dataDirectory) {
if (INSTANCE == null)
INSTANCE = this;
this.mongoConnection = new MongoDBConnection();
this.redisConnection = new RedisConnection();
this.infraLogger = new InternalLogger();
this.players = new CopyOnWriteArrayList<>();
this.logger = logger;
this.STAFF_ONLY = new AtomicBoolean(false);
this.server = server;
LogUtils.setErrorConsummer(e -> {
e.printStackTrace();//TODO change
});
LogUtils.setErrorConsummer(infraLogger::logError);
}

public static Ineundo getInstance() {
return INSTANCE;
}

@Subscribe
public void onInitialization(ProxyInitializeEvent event) {
new TranslationLoader().load("locale.ineundo");
//databases
try {
mongoConnection.load();
mongoConnection.connect();
logger.info(mongoConnection.getConnection().getName());
redisConnection.load();
redisConnection.connect();
} catch (Exception e) {
server.shutdown(Component.text("Database issue"));
e.printStackTrace();
infraLogger.logError(e);
server.shutdown(Component.translatable("proxy.startup.database.error"));
}

new PlayerDataListener(this, mongoConnection);
//listeners
new PlayerDataListener(this, mongoConnection, redisConnection);
}

@Subscribe
public void onQuit(ProxyShutdownEvent event) {
try {
mongoConnection.close();
redisConnection.close();
} catch (Exception e) {
infraLogger.logError(e);
}
}

public ProxyServer getServer() {
return server;
}

public Logger getLogger() {
return logger;
public InternalLogger getInfraLogger() {
return infraLogger;
}

public AtomicBoolean getSTAFF_ONLY() {
return STAFF_ONLY;
}

public void addPlayer(IslandsPlayer player) {
if (getPlayer(player.getUUID()).isPresent())
logger.severe("Player " + player.getUUID() + " is already registered....");
infraLogger.log(Level.WARN, "Player " + player.getUUID() + " is already registered....");
else
players.add(player);
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/fr/islandswars/ineundo/lang/IneundoError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fr.islandswars.ineundo.lang;

/**
* File <b>IneundoError</b> located on fr.islandswars.ineundo.lang
* IneundoError is a part of ineundo.
* <p>
* Copyright (c) 2017 - 2024 Islands Wars.
* <p>
* ineundo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>.
* <p>
*
* @author Jangliu, {@literal <[email protected]>}
* Created the 25/06/2024 at 21:15
* @since 0.1
*/
public class IneundoError extends RuntimeException {

public IneundoError(String message) {
super(message);
}

public IneundoError(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public ProxyServer getServer() {
}

public void log(String msg) {
ineundo.getLogger().info(msg);
ineundo.getInfraLogger().logInfo(msg);
}

public void error(Exception e) {
ineundo.getInfraLogger().logError(e);
}

public Optional<IslandsPlayer> getPlayer(UUID uuid) {
Expand Down
Loading

0 comments on commit a820897

Please sign in to comment.