2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-05 04:48:02 +00:00

welcome adventure api

MiniMessage is now used for Messages.yml

fix bug when from is null when server change
This commit is contained in:
mohammed jasem alaajel 2023-09-10 18:15:01 +04:00
parent 9f90d67ff3
commit 8e606fe4d2
No known key found for this signature in database
12 changed files with 84 additions and 23 deletions

View File

@ -23,6 +23,13 @@ dependencies {
api("com.squareup.okhttp:okhttp:2.7.5") api("com.squareup.okhttp:okhttp:2.7.5")
api("org.spongepowered:configurate-yaml:$configurateVersion") api("org.spongepowered:configurate-yaml:$configurateVersion")
api("com.github.ben-manes.caffeine:caffeine:3.1.8") api("com.github.ben-manes.caffeine:caffeine:3.1.8")
api("net.kyori:adventure-api:4.14.0")
api("net.kyori:adventure-text-serializer-gson:4.14.0")
api("net.kyori:adventure-text-serializer-legacy:4.14.0")
api("net.kyori:adventure-text-serializer-plain:4.14.0")
api("net.kyori:adventure-text-minimessage:4.14.0")
// tests // tests
testImplementation("junit:junit:4.13.2") testImplementation("junit:junit:4.13.2")
} }

View File

@ -17,6 +17,7 @@ import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
@ -347,8 +348,9 @@ public abstract class AbstractRedisBungeeAPI {
* calls {@link #getUuidFromName(String)} to get uuid * calls {@link #getUuidFromName(String)} to get uuid
* *
* @param playerName player name * @param playerName player name
* @param message kick message that player will see on kick * @param message kick message that player will see on kick
* @since 0.8.0 * @since 0.8.0
* @deprecated
*/ */
public void kickPlayer(String playerName, String message) { public void kickPlayer(String playerName, String message) {
@ -361,11 +363,38 @@ public abstract class AbstractRedisBungeeAPI {
* @param playerUUID player name * @param playerUUID player name
* @param message kick message that player will see on kick * @param message kick message that player will see on kick
* @since 0.8.0 * @since 0.8.0
* @deprecated
*/ */
@Deprecated
public void kickPlayer(UUID playerUUID, String message) { public void kickPlayer(UUID playerUUID, String message) {
kickPlayer(playerUUID, Component.text(message));
}
/**
* Kicks a player from the network
* calls {@link #getUuidFromName(String)} to get uuid
*
* @param playerName player name
* @param message kick message that player will see on kick
* @since 0.12.0
*/
public void kickPlayer(String playerName, Component message) {
kickPlayer(getUuidFromName(playerName), message);
}
/**
* Kicks a player from the network
*
* @param playerUUID player name
* @param message kick message that player will see on kick
* @since 0.12.0
*/
public void kickPlayer(UUID playerUUID, Component message) {
this.plugin.playerDataManager().kickPlayer(playerUUID, message); this.plugin.playerDataManager().kickPlayer(playerUUID, message);
} }
/** /**
* This gives you instance of Jedis * This gives you instance of Jedis
* *

View File

@ -19,6 +19,8 @@ import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNe
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisPipelineTask; 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 org.json.JSONObject;
import redis.clients.jedis.ClusterPipeline; import redis.clients.jedis.ClusterPipeline;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
@ -85,7 +87,7 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
} }
UUID uuid = UUID.fromString(data.getString("uuid")); UUID uuid = UUID.fromString(data.getString("uuid"));
String message = data.getString("message"); String message = data.getString("message");
plugin.handlePlatformKick(uuid, message); plugin.handlePlatformKick(uuid, COMPONENT_SERIALIZER.deserialize(message));
return; return;
} }
if (event.getChannel().equals("redisbungee-serverchange")) { if (event.getChannel().equals("redisbungee-serverchange")) {
@ -95,7 +97,8 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
return; return;
} }
UUID uuid = UUID.fromString(data.getString("uuid")); UUID uuid = UUID.fromString(data.getString("uuid"));
String from = data.getString("from"); String from = null;
if (data.has("from")) from = data.getString("from");
String to = data.getString("to"); String to = data.getString("to");
plugin.fireEvent(plugin.createPlayerChangedServerNetworkEvent(uuid, from, to)); plugin.fireEvent(plugin.createPlayerChangedServerNetworkEvent(uuid, from, to));
return; return;
@ -133,12 +136,14 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
handleServerChangeRedis(uuid, to); handleServerChangeRedis(uuid, to);
} }
public void kickPlayer(UUID uuid, String message) { private final JSONComponentSerializer COMPONENT_SERIALIZER =JSONComponentSerializer.json();
public void kickPlayer(UUID uuid, Component message) {
if (!plugin.handlePlatformKick(uuid, message)) { // handle locally before SENDING a message if (!plugin.handlePlatformKick(uuid, message)) { // handle locally before SENDING a message
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("proxy", plugin.configuration().getProxyId()); data.put("proxy", plugin.configuration().getProxyId());
data.put("uuid", uuid); data.put("uuid", uuid);
data.put("message", message); data.put("message", COMPONENT_SERIALIZER.serialize(message));
plugin.proxyDataManager().sendChannelMessage("redisbungee-kick", data.toString()); plugin.proxyDataManager().sendChannelMessage("redisbungee-kick", data.toString());
} }
} }

View File

@ -15,6 +15,7 @@ import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfigurati
import com.imaginarycode.minecraft.redisbungee.api.events.EventsPlatform; import com.imaginarycode.minecraft.redisbungee.api.events.EventsPlatform;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
import net.kyori.adventure.text.Component;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.UUID; import java.util.UUID;
@ -76,7 +77,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform {
public String getPlayerName(UUID player); public String getPlayerName(UUID player);
boolean handlePlatformKick(UUID uuid, String message); boolean handlePlatformKick(UUID uuid, Component message);
public String getPlayerServerName(P player); public String getPlayerServerName(P player);

View File

@ -67,8 +67,8 @@ public class RedisBungeeConfiguration {
return overrideBungeeCommands; return overrideBungeeCommands;
} }
public ImmutableMap<MessageType, String> getMessages() { public String getMessage(MessageType messageType) {
return messages; return this.messages.get(messageType);
} }
public boolean kickWhenOnline() { public boolean kickWhenOnline() {

View File

@ -1,5 +1,5 @@
# this config file is for messages for players and command messages # this config file is for messages for players and command messages
# Note this uses legacy formating which § for color codes # Note this uses MiniMessage format https://docs.advntr.dev/minimessage/format.html
# this might get replaced soon with mini message for both, velocity and bungeecord
logged-in-other-location: "§cYou logged in from another location!" logged-in-other-location: "<color:red>You logged in from another location!"
already-logged-in: "§cYou are already logged in!" already-logged-in: "<color:red>You are already logged in!"

View File

@ -16,7 +16,10 @@ dependencies {
compileOnly("net.md-5:bungeecord-api:$bungeecordApiVersion") { compileOnly("net.md-5:bungeecord-api:$bungeecordApiVersion") {
exclude("com.google.guava", "guava") exclude("com.google.guava", "guava")
exclude("com.google.code.gson", "gson") exclude("com.google.code.gson", "gson")
exclude("net.kyori","adventure-api")
} }
implementation("net.kyori:adventure-platform-bungeecord:4.3.0")
implementation("net.kyori:adventure-text-serializer-gson:4.14.0")
} }
description = "RedisBungee Bungeecord implementation" description = "RedisBungee Bungeecord implementation"
@ -35,6 +38,7 @@ tasks {
options.isDocFilesSubDirs = true options.isDocFilesSubDirs = true
options.links( options.links(
"https://ci.md-5.net/job/BungeeCord/ws/api/target/apidocs/", // bungeecord api "https://ci.md-5.net/job/BungeeCord/ws/api/target/apidocs/", // bungeecord api
"https://jd.advntr.dev/api/4.14.0"
) )
val apiDocs = File(rootProject.projectDir, "RedisBungee-API/build/docs/javadoc") val apiDocs = File(rootProject.projectDir, "RedisBungee-API/build/docs/javadoc")
options.linksOffline("https://ci.limework.net/RedisBungee/RedisBungee-API/build/docs/javadoc", apiDocs.path) options.linksOffline("https://ci.limework.net/RedisBungee/RedisBungee-API/build/docs/javadoc", apiDocs.path)

View File

@ -16,6 +16,8 @@ import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfigurati
import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -32,6 +34,9 @@ import java.util.concurrent.TimeUnit;
public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, PostLoginEvent, PlayerDisconnectEvent, PubSubMessageEvent, PlayerChangedServerNetworkEvent, PlayerLeftNetworkEvent, ServerConnectedEvent> implements Listener { public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, PostLoginEvent, PlayerDisconnectEvent, PubSubMessageEvent, PlayerChangedServerNetworkEvent, PlayerLeftNetworkEvent, ServerConnectedEvent> implements Listener {
private final BungeeComponentSerializer BUNGEECORD_SERIALIZER = BungeeComponentSerializer.get();
public BungeePlayerDataManager(RedisBungeePlugin<ProxiedPlayer> plugin) { public BungeePlayerDataManager(RedisBungeePlugin<ProxiedPlayer> plugin) {
super(plugin); super(plugin);
} }
@ -68,12 +73,12 @@ public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, Po
// check if online // check if online
if (getLastOnline(event.getConnection().getUniqueId()) == 0) { if (getLastOnline(event.getConnection().getUniqueId()) == 0) {
if (plugin.configuration().kickWhenOnline()) { if (plugin.configuration().kickWhenOnline()) {
kickPlayer(event.getConnection().getUniqueId(), plugin.configuration().getMessages().get(RedisBungeeConfiguration.MessageType.LOGGED_IN_OTHER_LOCATION)); kickPlayer(event.getConnection().getUniqueId(),MiniMessage.miniMessage().deserialize( plugin.configuration().getMessage(RedisBungeeConfiguration.MessageType.LOGGED_IN_OTHER_LOCATION)));
// wait 3 seconds before releasing the event // wait 3 seconds before releasing the event
plugin.executeAsyncAfter(() -> event.completeIntent((Plugin) plugin), TimeUnit.SECONDS, 3); plugin.executeAsyncAfter(() -> event.completeIntent((Plugin) plugin), TimeUnit.SECONDS, 3);
} else { } else {
event.setCancelled(true); event.setCancelled(true);
event.setCancelReason(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(plugin.configuration().getMessages().get(RedisBungeeConfiguration.MessageType.ALREADY_LOGGED_IN))))); event.setCancelReason(BungeeComponentSerializer.get().serialize(MiniMessage.miniMessage().deserialize( plugin.configuration().getMessage(RedisBungeeConfiguration.MessageType.ALREADY_LOGGED_IN))));
event.completeIntent((Plugin) plugin); event.completeIntent((Plugin) plugin);
} }
} else { } else {

View File

@ -32,6 +32,8 @@ import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.squareup.okhttp.Dispatcher; import com.squareup.okhttp.Dispatcher;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -156,11 +158,11 @@ public class RedisBungee extends Plugin implements RedisBungeePlugin<ProxiedPlay
} }
@Override @Override
public boolean handlePlatformKick(UUID uuid, String message) { public boolean handlePlatformKick(UUID uuid, Component message) {
ProxiedPlayer player = getPlayer(uuid); ProxiedPlayer player = getPlayer(uuid);
if (player == null) return false; if (player == null) return false;
if (!player.isConnected()) return false; if (!player.isConnected()) return false;
player.disconnect(TextComponent.fromLegacyText(message)); player.disconnect(BungeeComponentSerializer.get().serialize(message));
return true; return true;
} }

View File

@ -17,6 +17,13 @@ dependencies {
exclude("com.google.guava", "guava") exclude("com.google.guava", "guava")
exclude("com.google.code.gson", "gson") exclude("com.google.code.gson", "gson")
exclude("org.spongepowered", "configurate-yaml") exclude("org.spongepowered", "configurate-yaml")
// exclude also adventure api
exclude("net.kyori", "adventure-api")
exclude("net.kyori", "adventure-text-serializer-gson")
exclude("net.kyori", "adventure-text-serializer-legacy")
exclude("net.kyori", "adventure-text-serializer-plain")
exclude("net.kyori", "adventure-text-minimessage")
} }
compileOnly("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT") compileOnly("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")
annotationProcessor("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT") annotationProcessor("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")
@ -37,6 +44,7 @@ tasks {
options.isDocFilesSubDirs = true options.isDocFilesSubDirs = true
options.links( options.links(
"https://jd.papermc.io/velocity/3.0.0/", // velocity api "https://jd.papermc.io/velocity/3.0.0/", // velocity api
"https://jd.advntr.dev/api/4.14.0"
) )
val apiDocs = File(rootProject.projectDir, "RedisBungee-API/build/docs/javadoc") val apiDocs = File(rootProject.projectDir, "RedisBungee-API/build/docs/javadoc")
options.linksOffline("https://ci.limework.net/RedisBungee/RedisBungee-API/build/docs/javadoc", apiDocs.path) options.linksOffline("https://ci.limework.net/RedisBungee/RedisBungee-API/build/docs/javadoc", apiDocs.path)

View File

@ -45,6 +45,7 @@ import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier; import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.scheduler.ScheduledTask; import com.velocitypowered.api.scheduler.ScheduledTask;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.slf4j.Logger; import org.slf4j.Logger;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
@ -225,13 +226,12 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player>, Con
return this.getProxy().getPlayer(player).map(Player::getUsername).orElse(null); return this.getProxy().getPlayer(player).map(Player::getUsername).orElse(null);
} }
private final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacySection();
@Override @Override
public boolean handlePlatformKick(UUID uuid, String message) { public boolean handlePlatformKick(UUID uuid, Component message) {
Player player = getPlayer(uuid); Player player = getPlayer(uuid);
if (player == null) return false; if (player == null) return false;
player.disconnect(serializer.deserialize(message)); player.disconnect(message);
return true; return true;
} }

View File

@ -24,6 +24,7 @@ import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.connection.PostLoginEvent; import com.velocitypowered.api.event.connection.PostLoginEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.Objects; import java.util.Objects;
@ -65,18 +66,17 @@ public class VelocityPlayerDataManager extends PlayerDataManager<Player, PostLog
super.playerChangedServer(event.getPlayer().getUniqueId(), oldServer, currentServer); super.playerChangedServer(event.getPlayer().getUniqueId(), oldServer, currentServer);
} }
private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER = LegacyComponentSerializer.builder().build();
@Subscribe @Subscribe
public void onLoginEvent(LoginEvent event, Continuation continuation) { public void onLoginEvent(LoginEvent event, Continuation continuation) {
// check if online // check if online
if (getLastOnline(event.getPlayer().getUniqueId()) == 0) { if (getLastOnline(event.getPlayer().getUniqueId()) == 0) {
if (plugin.configuration().kickWhenOnline()) { if (plugin.configuration().kickWhenOnline()) {
kickPlayer(event.getPlayer().getUniqueId(), plugin.configuration().getMessages().get(RedisBungeeConfiguration.MessageType.LOGGED_IN_OTHER_LOCATION));
kickPlayer(event.getPlayer().getUniqueId(), MiniMessage.miniMessage().deserialize(plugin.configuration().getMessage(RedisBungeeConfiguration.MessageType.LOGGED_IN_OTHER_LOCATION)));
// wait 3 seconds before releasing the event // wait 3 seconds before releasing the event
plugin.executeAsyncAfter(continuation::resume, TimeUnit.SECONDS, 3); plugin.executeAsyncAfter(continuation::resume, TimeUnit.SECONDS, 3);
} else { } else {
event.setResult(ResultedEvent.ComponentResult.denied(LEGACY_COMPONENT_SERIALIZER.deserialize(Objects.requireNonNull(plugin.configuration().getMessages().get(RedisBungeeConfiguration.MessageType.ALREADY_LOGGED_IN))))); event.setResult(ResultedEvent.ComponentResult.denied( MiniMessage.miniMessage().deserialize(plugin.configuration().getMessage(RedisBungeeConfiguration.MessageType.ALREADY_LOGGED_IN))));
continuation.resume(); continuation.resume();
} }
} else { } else {