2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2026-04-08 16:10:26 +00:00

reimpl kick api using serialized messages instead, Move language to own module, depenedcies fixes

This commit is contained in:
2025-02-22 17:57:06 +04:00
parent 1d3bd7e101
commit 338297192c
22 changed files with 179 additions and 114 deletions

View File

@@ -20,8 +20,6 @@ import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEv
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisPipelineTask;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer;
import org.json.JSONObject;
import redis.clients.jedis.ClusterPipeline;
import redis.clients.jedis.Pipeline;
@@ -45,7 +43,6 @@ public abstract class PlayerDataManager<P> {
private final LoadingCache<UUID, InetAddress> ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getIpAddressFromRedis);
private final LoadingCache<UUID, Long> lastOnlineCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastOnlineFromRedis);
private final LoadingCache<Object, Multimap<String, UUID>> serverToPlayersCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build(this::serversToPlayersBuilder);
private final JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json();
public PlayerDataManager(RedisBungeePlugin<P> plugin) {
this.plugin = plugin;
@@ -99,50 +96,33 @@ public abstract class PlayerDataManager<P> {
this.serverToPlayersCache.invalidate(SERVERS_TO_PLAYERS_KEY);
}
protected void handlePubSubMessageEvent(IPubSubMessageEvent event) {
// kick api
if (event.getChannel().equals("redisbungee-kick")) {
JSONObject data = new JSONObject(event.getMessage());
String proxy = data.getString("proxy");
if (proxy.equals(this.proxyId)) {
return;
switch (event.getChannel()) {
case "redisbungee-serverchange" -> {
JSONObject data = new JSONObject(event.getMessage());
UUID uuid = UUID.fromString(data.getString("uuid"));
String from = null;
if (data.has("from")) from = data.getString("from");
String to = data.getString("to");
plugin.fireEvent(plugin.createPlayerChangedServerNetworkEvent(uuid, from, to));
}
UUID uuid = UUID.fromString(data.getString("uuid"));
String message = data.getString("message");
plugin.handlePlatformKick(uuid, COMPONENT_SERIALIZER.deserialize(message));
return;
}
if (event.getChannel().equals("redisbungee-serverchange")) {
JSONObject data = new JSONObject(event.getMessage());
String proxy = data.getString("proxy");
if (proxy.equals(this.proxyId)) {
return;
case "redisbungee-player-join" -> {
JSONObject data = new JSONObject(event.getMessage());
UUID uuid = UUID.fromString(data.getString("uuid"));
plugin.fireEvent(plugin.createPlayerJoinedNetworkEvent(uuid));
}
UUID uuid = UUID.fromString(data.getString("uuid"));
String from = null;
if (data.has("from")) from = data.getString("from");
String to = data.getString("to");
plugin.fireEvent(plugin.createPlayerChangedServerNetworkEvent(uuid, from, to));
return;
}
if (event.getChannel().equals("redisbungee-player-join")) {
JSONObject data = new JSONObject(event.getMessage());
String proxy = data.getString("proxy");
if (proxy.equals(this.proxyId)) {
return;
case "redisbungee-player-leave" -> {
JSONObject data = new JSONObject(event.getMessage());
UUID uuid = UUID.fromString(data.getString("uuid"));
plugin.fireEvent(plugin.createPlayerLeftNetworkEvent(uuid));
}
UUID uuid = UUID.fromString(data.getString("uuid"));
plugin.fireEvent(plugin.createPlayerJoinedNetworkEvent(uuid));
return;
}
if (event.getChannel().equals("redisbungee-player-leave")) {
JSONObject data = new JSONObject(event.getMessage());
String proxy = data.getString("proxy");
if (proxy.equals(this.proxyId)) {
return;
case "redisbungee-player-kick" -> {
JSONObject data = new JSONObject(event.getMessage());
UUID uuid = UUID.fromString(data.getString("uuid"));
String message = data.getString("serialized-message");
handleSerializedKick(uuid, message);
}
UUID uuid = UUID.fromString(data.getString("uuid"));
plugin.fireEvent(plugin.createPlayerLeftNetworkEvent(uuid));
}
}
@@ -158,14 +138,16 @@ public abstract class PlayerDataManager<P> {
handleServerChangeRedis(uuid, to);
}
public void kickPlayer(UUID uuid, Component message) {
if (!plugin.handlePlatformKick(uuid, message)) { // handle locally before SENDING a message
JSONObject data = new JSONObject();
data.put("proxy", this.proxyId);
data.put("uuid", uuid);
data.put("message", COMPONENT_SERIALIZER.serialize(message));
plugin.proxyDataManager().sendChannelMessage("redisbungee-kick", data.toString());
}
// must check if player is on the local proxy
protected abstract boolean handleSerializedKick(UUID player, String serializedMessage);
public void serializedPlayerKick(UUID player, String serializedMessage) {
JSONObject data = new JSONObject();
data.put("proxy", this.proxyId);
data.put("uuid", player);
data.put("serialized-message", serializedMessage);
if (!handleSerializedKick(player, serializedMessage))
plugin.proxyDataManager().sendChannelMessage("redisbungee-player-kick", data.toString());
}
private void handleServerChangeRedis(UUID uuid, String server) {

View File

@@ -103,7 +103,6 @@ public abstract class ProxyDataManager implements Runnable {
public synchronized void sendChannelMessage(String channel, String message) {
if (isClosed()) return;
this.plugin.fireEvent(this.plugin.createPubSubEvent(channel, message));
publishPayload(new PubSubPayload(this.proxyId, channel, message));
}

View File

@@ -11,12 +11,10 @@
package com.imaginarycode.minecraft.redisbungee.api;
import com.imaginarycode.minecraft.redisbungee.AbstractRedisBungeeAPI;
import com.imaginarycode.minecraft.redisbungee.api.config.LangConfiguration;
import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration;
import com.imaginarycode.minecraft.redisbungee.api.events.EventsPlatform;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
import net.kyori.adventure.text.Component;
import java.net.InetAddress;
import java.util.UUID;
@@ -55,8 +53,6 @@ public interface RedisBungeePlugin<P> extends EventsPlatform {
RedisBungeeConfiguration configuration();
LangConfiguration langConfiguration();
Summoner<?> getSummoner();
RedisBungeeMode getRedisBungeeMode();
@@ -77,11 +73,8 @@ public interface RedisBungeePlugin<P> extends EventsPlatform {
UUID getPlayerUUID(String player);
String getPlayerName(UUID player);
boolean handlePlatformKick(UUID uuid, Component message);
String getPlayerServerName(P player);
boolean isPlayerOnAServer(P player);

View File

@@ -1,155 +0,0 @@
/*
* Copyright (c) 2013-present RedisBungee contributors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.imaginarycode.minecraft.redisbungee.api.config;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* This language support implementation is temporarily
* until I come up with better system but for now we will use Maps instead :/
* Todo: possible usage of adventure api
*/
public class LangConfiguration {
private interface RegistrableMessages {
void register(String id, Locale locale, String miniMessage);
void test(Locale locale);
default void throwError(Locale locale, String where) {
throw new IllegalStateException("Language system in `" + where + "` found missing entries for " + locale.toString());
}
}
public static class Messages implements RegistrableMessages{
private final Map<Locale, Component> LOGGED_IN_FROM_OTHER_LOCATION;
private final Map<Locale, Component> ALREADY_LOGGED_IN;
private final Map<Locale, String> SERVER_CONNECTING;
private final Map<Locale, String> SERVER_NOT_FOUND;
private final Locale defaultLocale;
public Messages(Locale defaultLocale) {
LOGGED_IN_FROM_OTHER_LOCATION = new HashMap<>();
ALREADY_LOGGED_IN = new HashMap<>();
SERVER_CONNECTING = new HashMap<>();
SERVER_NOT_FOUND = new HashMap<>();
this.defaultLocale = defaultLocale;
}
public void register(String id, Locale locale, String miniMessage) {
switch (id) {
case "server-not-found" -> SERVER_NOT_FOUND.put(locale, miniMessage);
case "server-connecting" -> SERVER_CONNECTING.put(locale, miniMessage);
case "logged-in-other-location" -> LOGGED_IN_FROM_OTHER_LOCATION.put(locale, MiniMessage.miniMessage().deserialize(miniMessage));
case "already-logged-in" -> ALREADY_LOGGED_IN.put(locale, MiniMessage.miniMessage().deserialize(miniMessage));
}
}
public Component alreadyLoggedIn(Locale locale) {
if (ALREADY_LOGGED_IN.containsKey(locale)) return ALREADY_LOGGED_IN.get(locale);
return ALREADY_LOGGED_IN.get(defaultLocale);
}
// there is no way to know whats client locale during login so just default to use default locale MESSAGES.
public Component alreadyLoggedIn() {
return this.alreadyLoggedIn(this.defaultLocale);
}
public Component loggedInFromOtherLocation(Locale locale) {
if (LOGGED_IN_FROM_OTHER_LOCATION.containsKey(locale)) return LOGGED_IN_FROM_OTHER_LOCATION.get(locale);
return LOGGED_IN_FROM_OTHER_LOCATION.get(defaultLocale);
}
// there is no way to know what's client locale during login so just default to use default locale MESSAGES.
public Component loggedInFromOtherLocation() {
return this.loggedInFromOtherLocation(this.defaultLocale);
}
public Component serverConnecting(Locale locale, String server) {
String miniMessage;
if (SERVER_CONNECTING.containsKey(locale)) {
miniMessage = SERVER_CONNECTING.get(locale);
} else {
miniMessage = SERVER_CONNECTING.get(defaultLocale);
}
return MiniMessage.miniMessage().deserialize(miniMessage, Placeholder.parsed("server", server));
}
public Component serverConnecting(String server) {
return this.serverConnecting(this.defaultLocale, server);
}
public Component serverNotFound(Locale locale, String server) {
String miniMessage;
if (SERVER_NOT_FOUND.containsKey(locale)) {
miniMessage = SERVER_NOT_FOUND.get(locale);
} else {
miniMessage = SERVER_NOT_FOUND.get(defaultLocale);
}
return MiniMessage.miniMessage().deserialize(miniMessage, Placeholder.parsed("server", server));
}
public Component serverNotFound(String server) {
return this.serverNotFound(this.defaultLocale, server);
}
// tests locale if set CORRECTLY or just throw if not
public void test(Locale locale) {
if (!(LOGGED_IN_FROM_OTHER_LOCATION.containsKey(locale) && ALREADY_LOGGED_IN.containsKey(locale) && SERVER_CONNECTING.containsKey(locale) && SERVER_NOT_FOUND.containsKey(locale))) {
throwError(locale, "messages");
}
}
}
private final Component redisBungeePrefix;
private final Locale defaultLanguage;
private final boolean useClientLanguage;
private final Messages messages;
public LangConfiguration(Component redisBungeePrefix, Locale defaultLanguage, boolean useClientLanguage, Messages messages) {
this.redisBungeePrefix = redisBungeePrefix;
this.defaultLanguage = defaultLanguage;
this.useClientLanguage = useClientLanguage;
this.messages = messages;
}
public Component redisBungeePrefix() {
return redisBungeePrefix;
}
public Locale defaultLanguage() {
return defaultLanguage;
}
public boolean useClientLanguage() {
return useClientLanguage;
}
public Messages messages() {
return messages;
}
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright (c) 2013-present RedisBungee contributors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.imaginarycode.minecraft.redisbungee.api.config.loaders;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.config.LangConfiguration;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Locale;
public interface LangConfigLoader extends GenericConfigLoader {
int CONFIG_VERSION = 1;
default void loadLangConfig(RedisBungeePlugin<?> plugin, Path dataFolder) throws IOException {
Path configFile = createConfigFile(dataFolder, "lang.yml", "lang.yml");
final YAMLConfigurationLoader yamlConfigurationFileLoader = YAMLConfigurationLoader.builder().setPath(configFile).build();
ConfigurationNode node = yamlConfigurationFileLoader.load();
if (node.getNode("config-version").getInt(0) != CONFIG_VERSION) {
handleOldConfig(dataFolder, "lang.yml", "lang.yml");
node = yamlConfigurationFileLoader.load();
}
// MINI message serializer
MiniMessage miniMessage = MiniMessage.miniMessage();
Component prefix = miniMessage.deserialize(node.getNode("prefix").getString("<color:red>[<color:yellow>Redis<color:red>Bungee]"));
Locale defaultLocale = Locale.forLanguageTag(node.getNode("default-locale").getString("en-us"));
boolean useClientLocale = node.getNode("use-client-locale").getBoolean(true);
LangConfiguration.Messages messages = new LangConfiguration.Messages(defaultLocale);
node.getNode("messages").getChildrenMap().forEach((key, childNode) -> childNode.getChildrenMap().forEach((childKey, childChildNode) -> {
messages.register(key.toString(), Locale.forLanguageTag(childKey.toString()), childChildNode.getString());
}));
messages.test(defaultLocale);
onLangConfigLoad(new LangConfiguration(prefix, defaultLocale, useClientLocale, messages));
}
void onLangConfigLoad(LangConfiguration langConfiguration);
}

View File

@@ -1,55 +0,0 @@
# this config file is for messages / Languages
# use MiniMessage format https://docs.advntr.dev/minimessage/format.html
# for colors etc... Legacy chat color is not supported.
# Language codes used in minecraft from the minecraft wiki
# example: en-us for american english and ar-sa for arabic
# all codes can be obtained from link below
# from the colum Locale Code -> In-game
# NOTE: minecraft wiki shows languages like this `en_us` in config it should be `en-us`
# https://minecraft.wiki/w/Language
# example:
# lets assume we want to add arabic language.
# messages:
# logged-in-other-location:
# en-us: "<color:red>You logged in from another location!"
# ar-sa: "<color:red>لقد اتصلت من مكان اخر"
# RedisBungee Prefix if ever used.
prefix: "<color:red>[<color:yellow>Redis<color:red>Bungee]"
# en-us is american English, Which is the default language used when a language for a message isn't defined.
# Warning: IF THE set default locale wasn't defined in the config for all messages, plugin will not load.
# set the Default locale
default-locale: en-us
# send language based on client sent settings
# if you don't have languages configured For client Language
# it will default to language that has been set above
# NOTE: due minecraft protocol not sending player settings during login,
# some of the messages like logged-in-other-location will
# skip translation and use default locale that has been set in default-locale.
use-client-locale: true
# messages that are used during login, and connecting to Last server
messages:
logged-in-other-location:
en-us: "<color:red>You logged in from another location!"
pt-br: "<color:red>Você está logado em outra localização!"
already-logged-in:
en-us: "<color:red>You are already logged in!"
pt-br: "<color:red>Você já está logado!"
server-not-found:
# placeholder <server> displays server name in the message.
en-us: "<color:red>unable to connect you to the last server, because server <server> was not found."
pt-br: "<color:red>falha ao conectar você ao último servidor, porque o servidor <server> não foi encontrado."
server-connecting:
# placeholder <server> displays server name in the message.
en-us: "<color:green>Connecting you to <server>..."
pt-br: "<color:green>Conectando você a <server>..."
# DO NOT CHANGE!!!!!
config-version: 1