Skip to content

Commit 850ba5e

Browse files
authored
Velocity b582+ support (#230)
* Log about writability changes if enabled * Velocity b582+ support
1 parent 2a79564 commit 850ba5e

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

plugin/src/main/java/net/elytrium/limboapi/LimboAPI.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.velocitypowered.proxy.protocol.StateRegistry;
4141
import com.velocitypowered.proxy.protocol.netty.MinecraftCompressDecoder;
4242
import com.velocitypowered.proxy.protocol.netty.MinecraftCompressorAndLengthEncoder;
43+
import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder;
4344
import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder;
4445
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder;
4546
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -49,6 +50,8 @@
4950
import java.io.File;
5051
import java.io.IOException;
5152
import java.io.InputStream;
53+
import java.lang.invoke.MethodHandle;
54+
import java.lang.invoke.MethodHandles;
5255
import java.nio.file.Path;
5356
import java.util.HashMap;
5457
import java.util.HashSet;
@@ -137,6 +140,8 @@ public class LimboAPI implements LimboFactory {
137140

138141
public static final ConcurrentHashMap<Player, UUID> INITIAL_ID = new ConcurrentHashMap<>();
139142

143+
private static final MethodHandle STATE_FIELD;
144+
140145
private final VelocityServer server;
141146
private final Metrics.Factory metricsFactory;
142147
private final File configFile;
@@ -443,12 +448,14 @@ public void inject3rdParty(Player player, MinecraftConnection connection, Channe
443448
public void setState(MinecraftConnection connection, StateRegistry stateRegistry) {
444449
connection.setState(stateRegistry);
445450
this.setEncoderState(connection, stateRegistry);
451+
this.fixDecoderState(connection, stateRegistry);
446452
}
447453

448454
public void setActiveSessionHandler(MinecraftConnection connection, StateRegistry stateRegistry,
449455
MinecraftSessionHandler sessionHandler) {
450456
connection.setActiveSessionHandler(stateRegistry, sessionHandler);
451457
this.setEncoderState(connection, stateRegistry);
458+
this.fixDecoderState(connection, stateRegistry);
452459
}
453460

454461
public void setEncoderState(MinecraftConnection connection, StateRegistry state) {
@@ -474,6 +481,20 @@ public void setEncoderState(MinecraftConnection connection, StateRegistry state)
474481
}
475482
}
476483

484+
public void fixDecoderState(MinecraftConnection connection, StateRegistry state) {
485+
if (state.name() == null) { // custom state
486+
MinecraftDecoder decoder = connection.getChannel().pipeline().get(MinecraftDecoder.class);
487+
if (decoder != null) {
488+
try {
489+
// Let decoder know what we're in PLAY state, or it will kick the player.
490+
STATE_FIELD.invokeExact(decoder, StateRegistry.PLAY);
491+
} catch (Throwable throwable) {
492+
LimboAPI.getLogger().error("Failed to fixup decoder", throwable);
493+
}
494+
}
495+
}
496+
}
497+
477498
public void deject3rdParty(ChannelPipeline pipeline) {
478499
this.preparedPacketFactory.deject(pipeline);
479500
}
@@ -693,4 +714,13 @@ private static void setSerializer(Serializer serializer) {
693714
public static Serializer getSerializer() {
694715
return SERIALIZER;
695716
}
717+
718+
static {
719+
try {
720+
STATE_FIELD = MethodHandles.privateLookupIn(MinecraftDecoder.class, MethodHandles.lookup())
721+
.findSetter(MinecraftDecoder.class, "state", StateRegistry.class);
722+
} catch (NoSuchFieldException | IllegalAccessException e) {
723+
throw new RuntimeException(e);
724+
}
725+
}
696726
}

plugin/src/main/java/net/elytrium/limboapi/injection/login/confirmation/LoginConfirmHandler.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737

3838
public class LoginConfirmHandler implements MinecraftSessionHandler {
3939

40+
private static final boolean BACKPRESSURE_LOG =
41+
Boolean.getBoolean("velocity.log-server-backpressure");
42+
4043
private static final MethodHandle TEARDOWN_METHOD;
4144

4245
private final LimboAPI plugin;
@@ -108,6 +111,17 @@ public void handleUnknown(ByteBuf buf) {
108111
this.connection.close(true);
109112
}
110113

114+
@Override
115+
public void writabilityChanged() {
116+
if (BACKPRESSURE_LOG) {
117+
if (this.connection.getChannel().isWritable()) {
118+
LimboAPI.getLogger().info("{} is writable, will auto-read", this.player);
119+
} else {
120+
LimboAPI.getLogger().info("{} is not writable, not auto-reading", this.player);
121+
}
122+
}
123+
}
124+
111125
@Override
112126
public void disconnected() {
113127
try {

plugin/src/main/java/net/elytrium/limboapi/server/LimboSessionHandlerImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868

6969
public class LimboSessionHandlerImpl implements MinecraftSessionHandler {
7070

71+
private static final boolean BACKPRESSURE_LOG =
72+
Boolean.getBoolean("velocity.log-server-backpressure");
73+
7174
private static final MethodHandle TEARDOWN_METHOD;
7275

7376
private final LimboAPI plugin;
@@ -380,6 +383,17 @@ public void handleGeneric(MinecraftPacket packet) {
380383
this.callback.onGeneric(packet);
381384
}
382385

386+
@Override
387+
public void writabilityChanged() {
388+
if (BACKPRESSURE_LOG) {
389+
if (this.player.getConnection().getChannel().isWritable()) {
390+
LimboAPI.getLogger().info("{} is writable, will auto-read", this.player);
391+
} else {
392+
LimboAPI.getLogger().info("{} is not writable, not auto-reading", this.player);
393+
}
394+
}
395+
}
396+
383397
private void kickTooBigPacket(String type, int length) {
384398
this.player.getConnection().closeWith(this.plugin.getPackets().getTooBigPacket());
385399

0 commit comments

Comments
 (0)