Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions plugin/src/main/java/net/elytrium/limboapi/LimboAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.netty.MinecraftCompressDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftCompressorAndLengthEncoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -49,6 +50,8 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -137,6 +140,8 @@ public class LimboAPI implements LimboFactory {

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

private static final MethodHandle STATE_FIELD;

private final VelocityServer server;
private final Metrics.Factory metricsFactory;
private final File configFile;
Expand Down Expand Up @@ -443,12 +448,14 @@ public void inject3rdParty(Player player, MinecraftConnection connection, Channe
public void setState(MinecraftConnection connection, StateRegistry stateRegistry) {
connection.setState(stateRegistry);
this.setEncoderState(connection, stateRegistry);
this.fixDecoderState(connection, stateRegistry);
}

public void setActiveSessionHandler(MinecraftConnection connection, StateRegistry stateRegistry,
MinecraftSessionHandler sessionHandler) {
connection.setActiveSessionHandler(stateRegistry, sessionHandler);
this.setEncoderState(connection, stateRegistry);
this.fixDecoderState(connection, stateRegistry);
}

public void setEncoderState(MinecraftConnection connection, StateRegistry state) {
Expand All @@ -474,6 +481,20 @@ public void setEncoderState(MinecraftConnection connection, StateRegistry state)
}
}

public void fixDecoderState(MinecraftConnection connection, StateRegistry state) {
if (state.name() == null) { // custom state
MinecraftDecoder decoder = connection.getChannel().pipeline().get(MinecraftDecoder.class);
if (decoder != null) {
try {
// Let decoder know what we're in PLAY state, or it will kick the player.
STATE_FIELD.invokeExact(decoder, StateRegistry.PLAY);
} catch (Throwable throwable) {
LimboAPI.getLogger().error("Failed to fixup decoder", throwable);
}
}
}
}

public void deject3rdParty(ChannelPipeline pipeline) {
this.preparedPacketFactory.deject(pipeline);
}
Expand Down Expand Up @@ -693,4 +714,13 @@ private static void setSerializer(Serializer serializer) {
public static Serializer getSerializer() {
return SERIALIZER;
}

static {
try {
STATE_FIELD = MethodHandles.privateLookupIn(MinecraftDecoder.class, MethodHandles.lookup())
.findSetter(MinecraftDecoder.class, "state", StateRegistry.class);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

public class LoginConfirmHandler implements MinecraftSessionHandler {

private static final boolean BACKPRESSURE_LOG =
Boolean.getBoolean("velocity.log-server-backpressure");

private static final MethodHandle TEARDOWN_METHOD;

private final LimboAPI plugin;
Expand Down Expand Up @@ -108,6 +111,17 @@ public void handleUnknown(ByteBuf buf) {
this.connection.close(true);
}

@Override
public void writabilityChanged() {
if (BACKPRESSURE_LOG) {
if (this.connection.getChannel().isWritable()) {
LimboAPI.getLogger().info("{} is writable, will auto-read", this.player);
} else {
LimboAPI.getLogger().info("{} is not writable, not auto-reading", this.player);
}
}
}

@Override
public void disconnected() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@

public class LimboSessionHandlerImpl implements MinecraftSessionHandler {

private static final boolean BACKPRESSURE_LOG =
Boolean.getBoolean("velocity.log-server-backpressure");

private static final MethodHandle TEARDOWN_METHOD;

private final LimboAPI plugin;
Expand Down Expand Up @@ -380,6 +383,17 @@ public void handleGeneric(MinecraftPacket packet) {
this.callback.onGeneric(packet);
}

@Override
public void writabilityChanged() {
if (BACKPRESSURE_LOG) {
if (this.player.getConnection().getChannel().isWritable()) {
LimboAPI.getLogger().info("{} is writable, will auto-read", this.player);
} else {
LimboAPI.getLogger().info("{} is not writable, not auto-reading", this.player);
}
}
}

private void kickTooBigPacket(String type, int length) {
this.player.getConnection().closeWith(this.plugin.getPackets().getTooBigPacket());

Expand Down
Loading