Skip to content

Commit

Permalink
feat(velocity): support new client information packet (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia authored Oct 19, 2023
1 parent 2d88e82 commit b1142a1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion triton-velocity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {

annotationProcessor 'com.velocitypowered:velocity-api:3.1.0'

compileOnly 'com.velocitypowered:velocity-proxy:3.1.2-SNAPSHOT'
compileOnly 'com.velocitypowered:velocity-proxy:3.2.0-SNAPSHOT'

compileOnly 'io.netty:netty-codec:4.1.80.Final'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.connection.PreLoginEvent;
import com.velocitypowered.api.event.player.PlayerSettingsChangedEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
import com.velocitypowered.api.event.proxy.ProxyPingEvent;
Expand Down Expand Up @@ -68,14 +67,6 @@ public void onPlayerLogin(LoginEvent e) {
lp.injectNettyPipeline();
}

@Subscribe
public void onPlayerSettingsUpdate(PlayerSettingsChangedEvent event) {
val lp = Triton.get().getPlayerManager().get(event.getPlayer().getUniqueId());
if (lp.isWaitingForClientLocale()) {
lp.setLang(Triton.get().getLanguageManager().getLanguageByLocaleOrDefault(event.getPlayerSettings().getLocale().toString()));
}
}

@Subscribe(order = PostOrder.LAST)
public void onProxyPing(ProxyPingEvent event) {
if (shouldNotTranslateMotd()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.rexcantor64.triton.velocity.packetinterceptor;

import com.rexcantor64.triton.velocity.player.VelocityLanguagePlayer;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.ClientSettings;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.ReferenceCounted;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
public class VelocityNettyDecoder extends MessageToMessageDecoder<MinecraftPacket> {

private final VelocityLanguagePlayer player;

@Override
protected void decode(ChannelHandlerContext ctx, MinecraftPacket packet, List<Object> out) {
if (packet instanceof ReferenceCounted) {
// We need to retain the packet since we're just passing them through, otherwise Netty will throw an error
((ReferenceCounted) packet).retain();
out.add(packet);
return;
}
// PlayerSettingsChangedEvent is not working on 1.20.2, so we are using packets instead
if (packet instanceof ClientSettings) {
ClientSettings cs = (ClientSettings) packet;
player.setClientLocale(cs.getLocale());
}
out.add(packet);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.rexcantor64.triton.player.LanguagePlayer;
import com.rexcantor64.triton.utils.SocketUtils;
import com.rexcantor64.triton.velocity.VelocityTriton;
import com.rexcantor64.triton.velocity.packetinterceptor.VelocityNettyDecoder;
import com.rexcantor64.triton.velocity.packetinterceptor.VelocityNettyEncoder;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.proxy.Player;
Expand All @@ -27,6 +28,7 @@
import java.util.concurrent.ConcurrentHashMap;

public class VelocityLanguagePlayer implements LanguagePlayer {
@Getter
private final Player parent;

private Language language;
Expand All @@ -40,6 +42,7 @@ public class VelocityLanguagePlayer implements LanguagePlayer {
private final Map<UUID, String> bossBars = new HashMap<>();
private final Map<UUID, Component> playerListItemCache = new ConcurrentHashMap<>();
private boolean waitingForClientLocale = false;
private String clientLocale;
private final RefreshFeatures refresher;

public VelocityLanguagePlayer(@NotNull Player parent) {
Expand Down Expand Up @@ -87,6 +90,13 @@ public void waitForClientLocale() {
this.waitingForClientLocale = true;
}

public void setClientLocale(String locale) {
if (this.isWaitingForClientLocale()) {
this.setLang(Triton.get().getLanguageManager().getLanguageByLocaleOrDefault(locale));
}
this.clientLocale = locale;
}

public Language getLang() {
if (language == null)
language = Triton.get().getLanguageManager().getMainLanguage();
Expand Down Expand Up @@ -120,6 +130,8 @@ public void refreshAll() {

public void injectNettyPipeline() {
ConnectedPlayer connectedPlayer = (ConnectedPlayer) this.parent;
connectedPlayer.getConnection().getChannel().pipeline()
.addAfter(Connections.MINECRAFT_DECODER, "triton-custom-decoder", new VelocityNettyDecoder(this));
connectedPlayer.getConnection().getChannel().pipeline()
.addAfter(Connections.MINECRAFT_ENCODER, "triton-custom-encoder", new VelocityNettyEncoder(this));
}
Expand All @@ -129,18 +141,24 @@ public UUID getUUID() {
return this.parent.getUniqueId();
}

public Player getParent() {
return parent;
}

public @NotNull ProtocolVersion getProtocolVersion() {
return this.getParent().getProtocolVersion();
}

private void load() {
this.language = Triton.get().getStorage().getLanguage(this);
Triton.get().getStorage()
.setLanguage(null, SocketUtils.getIpAddress(getParent().getRemoteAddress()), language);
if (this.clientLocale != null && this.isWaitingForClientLocale()) {
this.waitingForClientLocale = false;
this.language = Triton.get().getLanguageManager().getLanguageByLocaleOrDefault(this.clientLocale);
if (getParent() != null) {
getParent().sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Triton.get().getMessagesConfig()
.getMessage("success.detected-language", language.getDisplayName())));
}
}
if (getParent() != null) {
Triton.get().getStorage()
.setLanguage(null, SocketUtils.getIpAddress(getParent().getRemoteAddress()), language);
}
}

private void save() {
Expand Down

0 comments on commit b1142a1

Please sign in to comment.