Skip to content

Commit

Permalink
chore: update cloudnet dependency and did some improvements to some c…
Browse files Browse the repository at this point in the history
…ommands + changed the way how the global playercount works
  • Loading branch information
byPixelTV committed Oct 19, 2024
1 parent ad02e82 commit f332d34
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

14 changes: 7 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ dependencies {
// CommandAPI and Jakarta Inject
implementation("dev.jorel:commandapi-velocity-shade:9.6.0-SNAPSHOT")

implementation("org.json:json:20240303")

// Lombok dependencies
annotationProcessor("org.projectlombok:lombok:1.18.34")
compileOnly("org.projectlombok:lombok:1.18.34")

// CloudNet
val cloudNetVersion = "4.0.0-RC10"
compileOnly(platform("eu.cloudnetservice.cloudnet:bom:$cloudNetVersion"))
compileOnly("eu.cloudnetservice.cloudnet", "bridge")
compileOnly("eu.cloudnetservice.cloudnet", "wrapper-jvm")
compileOnly("eu.cloudnetservice.cloudnet", "driver")
compileOnly("eu.cloudnetservice.cloudnet", "syncproxy")
val cloudNetVersion = "4.0.0-RC11"
compileOnly("eu.cloudnetservice.cloudnet:driver:$cloudNetVersion")
compileOnly("eu.cloudnetservice.cloudnet:bridge:$cloudNetVersion")
compileOnly("eu.cloudnetservice.cloudnet:wrapper-jvm:$cloudNetVersion")

val simpleCloudVersion = "2.8.1"
compileOnly("eu.thesimplecloud.simplecloud", "simplecloud-api", simpleCloudVersion)
Expand All @@ -64,7 +64,7 @@ sourceSets {
tasks {
compileJava {
options.encoding = "UTF-8"
options.release.set(21)
options.release.set(23)
}

build {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/de/bypixeltv/redivelocity/RediVelocity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.bypixeltv.redivelocity.listeners.ProxyPingListener;
import de.bypixeltv.redivelocity.listeners.ServerSwitchListener;
import de.bypixeltv.redivelocity.managers.RedisController;
import de.bypixeltv.redivelocity.managers.RedisManager;
import de.bypixeltv.redivelocity.managers.UpdateManager;
import de.bypixeltv.redivelocity.utils.CloudUtils;
import de.bypixeltv.redivelocity.utils.ProxyIdGenerator;
Expand All @@ -22,6 +23,7 @@
import jakarta.inject.Provider;
import jakarta.inject.Singleton;
import lombok.Getter;
import de.bypixeltv.redivelocity.pubsub.MessageListener;
import lombok.Setter;
import net.kyori.adventure.text.minimessage.MiniMessage;

Expand Down Expand Up @@ -85,7 +87,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
(CloudUtils.getServiceName(config.getCloud().getCloudSystem()) != null ? CloudUtils.getServiceName(config.getCloud().getCloudSystem()) : proxyIdGenerator.generate()) :
proxyIdGenerator.generate();

redisController.addToList("rv-proxies", new String[]{proxyId});
redisController.setHashField("rv-proxies", proxyId, proxyId);
redisController.setHashField("rv-proxy-players", proxyId, "0");
if (redisController.getString("rv-global-playercount") == null) {
redisController.setString("rv-global-playercount", "0");
Expand Down Expand Up @@ -114,24 +116,27 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
proxy.getEventManager().register(this, new ProxyPingListener(this));
}

RedisManager redisManager = new RedisManager(redisController.getJedisPool());

new MessageListener(redisManager, this.proxy);

rediVelocityCommandProvider.get().register();
sendLogs("Proxy initialization completed");
}

@Subscribe
public void onProxyShutdown(ProxyShutdownEvent event) {
sendLogs("Proxy shutdown started");
redisController.removeFromListByValue("rv-proxies", proxyId);
redisController.deleteHashField("rv-proxies", proxyId);
redisController.deleteHashField("rv-proxy-players", proxyId);
redisController.deleteHash("rv-players-name");
redisController.deleteHash("rv-" + proxyId + "-servers-servers");
redisController.deleteHash("rv-" + proxyId + "-servers-players");
redisController.deleteHash("rv-" + proxyId + "-servers-playercount");
redisController.deleteHash("rv-" + proxyId + "-servers-address");

if (redisController.getList("rv-proxies").isEmpty()) {
redisController.deleteHash("rv-proxy-players");
redisController.deleteString("rv-global-playercount");
if (!redisController.exists("rv-proxies")) {
redisController.deleteHash("rv-global-playercount");
}
redisController.shutdown();
sendLogs("Proxy shutdown completed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ public void register() {
.executes((sender, args) -> {
String proxyId = (String) args.getOptional(0).orElse(null);
List<String> players = redisController.getAllHashValues("rv-players-name");
List<String> playersPrettyNames = players.stream()

// Use parallel stream for better performance
List<String> playersPrettyNames = players.parallelStream()
.map(player -> {
String playerProxy = redisController.getHashField("rv-players-proxy", mojangUtilsProvider.get().getUUID(player).toString());
String playerProxy = redisController.getHashField("rv-players-proxy", redisController.getHashValueByField("rv-players-name", player));
if (proxyId == null) {
return prefix + " <aqua>" + player + "</aqua> <dark_gray>(<aqua>" + playerProxy + "</aqua>)</dark_gray>";
} else if (playerProxy.equals(proxyId)) {
Expand All @@ -143,6 +145,7 @@ public void register() {
})
.filter(Objects::nonNull)
.collect(Collectors.toList());

String playersPrettyString = String.join("<br>", playersPrettyNames);
if (playersPrettyNames.isEmpty()) {
if (proxyId == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import java.util.Set;

@Singleton
public class DisconnectListener {

Expand Down Expand Up @@ -52,17 +54,8 @@ public void onDisconnectEvent(DisconnectEvent event) {
redisController.setHashField("rv-proxy-players", proxyId, "0");
}

var gplayers = redisController.getString("rv-global-playercount");
if (gplayers != null) {
int globalPlayerCount = Integer.parseInt(gplayers);
if (globalPlayerCount <= 0) {
redisController.setString("rv-global-playercount", "0");
} else {
redisController.setString("rv-global-playercount", String.valueOf(globalPlayerCount - 1));
}
} else {
redisController.setString("rv-global-playercount", "0");
}
Set<String> proxyPlayersKeys = redisController.getHashValuesAsPair("rv-proxy-players").keySet();
redisController.setString("rv-global-playercount", String.valueOf(proxyPlayersKeys.stream().mapToInt(Integer::parseInt).sum()));

redisController.deleteHashField("rv-players-proxy", player.getUniqueId().toString());
redisController.deleteHashField("rv-players-name", player.getUniqueId().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import jakarta.inject.Singleton;
import net.kyori.adventure.text.minimessage.MiniMessage;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Singleton
public class PostLoginListener {

Expand Down Expand Up @@ -64,17 +68,8 @@ public void onPostLogin(PostLoginEvent event) {
redisController.setHashField("rv-proxy-players", proxyId, "0");
}

var gplayers = redisController.getString("rv-global-playercount");
if (gplayers != null) {
int globalPlayerCount = Integer.parseInt(gplayers);
if (globalPlayerCount <= 0) {
redisController.setString("rv-global-playercount", "1");
} else {
redisController.setString("rv-global-playercount", String.valueOf(globalPlayerCount + 1));
}
} else {
redisController.setString("rv-global-playercount", "0");
}
Set<String> proxyPlayersKeys = redisController.getHashValuesAsPair("rv-proxy-players").keySet();
redisController.setString("rv-global-playercount", String.valueOf(proxyPlayersKeys.stream().mapToInt(Integer::parseInt).sum()));

redisController.setHashField("rv-players-name", player.getUniqueId().toString(), player.getUsername());
redisController.setHashField("rv-players-ip", player.getUniqueId().toString(), player.getRemoteAddress().toString().split(":")[0].substring(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ public List<String> getList(String listName) {
}
}

public String getHashValueByField(String hashName, String fieldName) {
try (var jedis = jedisPool.getResource()) {
return jedis.hget(hashName, fieldName);
}
}

public Boolean exists(String key) {
try (var jedis = jedisPool.getResource()) {
return jedis.exists(key);
}
}

public List<String> getHashFieldNamesByValue(String hashName, String value) {
List<String> fieldNames = new ArrayList<>();
try (var jedis = jedisPool.getResource()) {
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/de/bypixeltv/redivelocity/managers/RedisManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.bypixeltv.redivelocity.managers;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;

import java.util.List;

public class RedisManager {

private final JedisPool jedisPool;

public RedisManager(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}

public void subscribe(List<String> channels, RedisMessageListener onMessage) {
JedisPubSub jedisPubSub = new JedisPubSub() {
@Override
public void onPMessage(String pattern, String channel, String message) {
onMessage.onMessage(pattern, channel, message);
}
};

new Thread(() -> {
try (Jedis jedis = jedisPool.getResource()) {
jedis.psubscribe(jedisPubSub, channels.toArray(new String[0]));
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}

@FunctionalInterface
public interface RedisMessageListener {
void onMessage(String pattern, String channel, String message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.bypixeltv.redivelocity.pubsub;

import com.velocitypowered.api.proxy.ProxyServer;
import de.bypixeltv.redivelocity.managers.RedisManager;
import net.kyori.adventure.text.minimessage.MiniMessage;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.json.JSONObject;

import java.util.List;
import java.util.UUID;

@Singleton
public class MessageListener {

private static final List<String> channels = List.of("redivelocity-kick");

private final RedisManager redisManager;
private final ProxyServer proxyServer;

@Inject
public MessageListener(RedisManager redisManager, ProxyServer proxyServer) {
this.redisManager = redisManager;
this.proxyServer = proxyServer;
init();
}

private void init() {
redisManager.subscribe(channels, (pattern, channel, msg) -> {
if ("redivelocity-kick".equals(channel)) {
JSONObject message = new JSONObject(msg);
String messagesString = message.getString("messages");
JSONObject data = new JSONObject(messagesString);
if (data.has("uuid") && data.has("reason")) {
UUID uuid = UUID.fromString(data.getString("uuid"));
String reason = data.getString("reason");

proxyServer.getPlayer(uuid).ifPresent(player -> player.disconnect(MiniMessage.miniMessage().deserialize(reason)));
}
}
});
}
}

0 comments on commit f332d34

Please sign in to comment.