Skip to content

Commit

Permalink
refactor(bungee): get protocol version from pending connection (#343)
Browse files Browse the repository at this point in the history
Relates to #342
Relates to #289

Port of 7fb13e4 to v4
  • Loading branch information
diogotcorreia committed Nov 3, 2023
1 parent ac5af6d commit 3f8f41e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.protocol.MinecraftEncoder;
import org.bstats.bungeecord.Metrics;
import org.bstats.charts.SingleLineChart;

Expand Down Expand Up @@ -67,7 +66,7 @@ public void onEnable() {

for (ProxiedPlayer p : getBungeeCord().getPlayers()) {
BungeeLanguagePlayer lp = getPlayerManager().get(p.getUniqueId());
injectPipeline(lp, p);
injectPipeline(lp, p, p.getPendingConnection().getVersion());
}

val commandHandler = new BungeeCommandHandler();
Expand Down Expand Up @@ -122,20 +121,19 @@ public String getVersion() {
return getLoader().getDescription().getVersion();
}

public void injectPipeline(BungeeLanguagePlayer lp, Connection p) {
public void injectPipeline(BungeeLanguagePlayer lp, Connection p, int protocolVersion) {
Triton.get().getLogger().logTrace("Injecting pipeline for player %1", lp);
try {
Object ch = ReflectionUtils.getDeclaredField(p, "ch");
Method method = ch.getClass().getDeclaredMethod("getHandle");
Channel channel = (Channel) method.invoke(ch, new Object[0]);
int protocolVersion = (int) ReflectionUtils.getDeclaredField(channel.pipeline().get(MinecraftEncoder.class), "protocolVersion");

channel.pipeline().addAfter(PipelineUtils.PACKET_DECODER, "triton-custom-decoder", new BungeeDecoder(lp));
channel.pipeline()
.addAfter(PipelineUtils.PACKET_ENCODER, "triton-custom-encoder", new BungeeListener(lp, protocolVersion));
channel.pipeline().remove("triton-pre-login-encoder");
} catch (Exception e) {
getLogger().logError("[PacketInjector] Failed to inject client connection for %1", lp.getUUID());
e.printStackTrace();
getLogger().logError(e, "[PacketInjector] Failed to inject client connection for %1", lp.getUUID());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onLogin(LoginEvent event) {
BungeeTriton.asBungee().getBungeeCord().getScheduler().runAsync(plugin, () -> {
val lp = new BungeeLanguagePlayer(event.getConnection().getUniqueId(), event.getConnection());
BungeeTriton.asBungee().getPlayerManager().registerPlayer(lp);
BungeeTriton.asBungee().injectPipeline(lp, event.getConnection());
BungeeTriton.asBungee().injectPipeline(lp, event.getConnection(), event.getConnection().getVersion());
event.completeIntent(plugin);
});
}
Expand Down

0 comments on commit 3f8f41e

Please sign in to comment.