Skip to content

Commit

Permalink
Skip handshake/networking for brokenrock/geyser players. Fix some bugs™
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jun 20, 2023
1 parent 1689879 commit 329713d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.83.0+1.20.1

maven_group = eu.pb4

mod_version = 0.5.1
mod_version = 0.5.2

minecraft_version_supported = ">=1.20-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void init(MinecraftServer server) {
CommonImpl.saveConfig("auto-host", config);


EarlyPlayNetworkHandler.register(ResourcePackNetworkHandler::new);
EarlyPlayNetworkHandler.register(ResourcePackNetworkHandler::create);
provider.serverStarted(server);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public ResourcePackNetworkHandler(Context context) {
}
}

public static ResourcePackNetworkHandler create(Context context) {
if (PolymerCommonUtils.isBedrockPlayer(context.player())) {
return null;
}
return new ResourcePackNetworkHandler(context);
}

@Override
public void onTick() {
if (this.delayed && AutoHost.provider.isReady()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public abstract class IdListMixin<T> implements PolymerIdList<T> {
}
}
}
this.polymer$vanillaBitCount = MathHelper.ceilLog2(this.list.size() - this.polymer$states.size());
}
}

Expand Down Expand Up @@ -157,7 +158,7 @@ public abstract class IdListMixin<T> implements PolymerIdList<T> {
this.polymer$locked = false;
this.polymer$lazyList.forEach(this::add);
this.polymer$lazyList.clear();
this.polymer$vanillaBitCount = MathHelper.ceilLog2(this.size() - this.polymer$states.size());
this.polymer$vanillaBitCount = MathHelper.ceilLog2(this.list.size() - this.polymer$states.size());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void register(Function<Context, EarlyPlayNetworkHandler> construct
private final EarlyConnectionMagic.ContextImpl context;
private final Identifier identifier;

private volatile long lastRespose = 0;
private volatile long lastResponse = 0;

private volatile int keepAliveSent = 0;
private volatile int keepAliveReceived = 0;
Expand Down Expand Up @@ -76,9 +76,9 @@ public boolean handleCustomPayload(CustomPayloadC2SPacket packet) {

@Override
public final void tick() {
if (this.lastRespose++ == 1200) {
if (this.lastResponse++ == 1200) {
this.disconnect(Text.translatable("multiplayer.disconnect.slow_login"));
} else if (this.lastRespose == 20) {
} else if (this.lastResponse == 20) {
this.sendKeepAlive();
}

Expand All @@ -98,7 +98,7 @@ protected boolean isForcingRespawnPacket() {

@Override
public final void onKeepAlive(KeepAliveC2SPacket packet) {
this.lastRespose = -20;
this.lastResponse = -20;
this.keepAliveReceived++;
if (this.canContinue) {
this.handleKeepAlive(packet.getId());
Expand Down Expand Up @@ -443,7 +443,10 @@ public boolean isConnectionOpen() {
}

@ApiStatus.NonExtendable
public interface Context {}
public interface Context {
MinecraftServer server();
ServerPlayerEntity player();
}

@Override
public final @Nullable ServerPlayerEntity getPlayerForPacketTweaker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -19,28 +20,28 @@ public class EarlyConnectionMagic {
public static void handle(ServerPlayerEntity player, MinecraftServer server, ClientConnection connection, Consumer<ContextImpl> finish) {
var iterator = new ArrayList<>(CONSTRUCTORS).iterator();

if (iterator.hasNext()) {
var context = new ContextImpl(server, player, connection, new ArrayList<>(), (c) -> {

if (iterator.hasNext()) {
iterator.next().apply(c);
} else {
finish.accept(c);
var context = new ContextImpl(server, player, connection, new ArrayList<>(), (c) -> {
while (iterator.hasNext()) {
var handler = iterator.next().apply(c);
if (handler != null) {
return;
}
});
}

finish.accept(c);
});

((TempPlayerLoginAttachments) player).polymerNet$setLatePackets(context.storedPackets);
((TempPlayerLoginAttachments) player).polymerNet$setLatePackets(context.storedPackets);

iterator.next().apply(context);
}
context.continueRunning.accept(context);
}

public static void register(Function<EarlyPlayNetworkHandler.Context, EarlyPlayNetworkHandler> constructor) {
public static void register(Function<EarlyPlayNetworkHandler.Context, @Nullable EarlyPlayNetworkHandler> constructor) {
CONSTRUCTORS.add(constructor);
}

static {
EarlyConnectionMagic.register(PolymerHandshakeHandlerImplLogin::new);
EarlyConnectionMagic.register(PolymerHandshakeHandlerImplLogin::create);
}

public record ContextImpl(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.pb4.polymer.networking.impl;

import com.ibm.icu.impl.ICUResourceBundle;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.common.impl.CommonResourcePackInfoHolder;
import eu.pb4.polymer.networking.api.EarlyPlayNetworkHandler;
import eu.pb4.polymer.networking.api.PolymerHandshakeHandler;
Expand All @@ -10,18 +10,16 @@
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayPongC2SPacket;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.util.Identifier;
import net.minecraft.world.GameMode;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@ApiStatus.Internal
public class PolymerHandshakeHandlerImplLogin extends EarlyPlayNetworkHandler implements PolymerHandshakeHandler {
public final class PolymerHandshakeHandlerImplLogin extends EarlyPlayNetworkHandler implements PolymerHandshakeHandler {
public static long MAGIC_INIT_VALUE = 0xbb706c6d72627374L;
public static int CONTINUE_LOGIN_ID = 1;

Expand All @@ -30,7 +28,7 @@ public class PolymerHandshakeHandlerImplLogin extends EarlyPlayNetworkHandler im
private final Object2LongMap<Identifier> lastUpdate = new Object2LongOpenHashMap<>();
private final Map<Identifier, NbtElement> metadata = new HashMap<>();

public PolymerHandshakeHandlerImplLogin(Context context) {
private PolymerHandshakeHandlerImplLogin(Context context) {
super(new Identifier("polymer", "early_handshake"), context);
((TempPlayerLoginAttachments) this.getPlayer()).polymerNet$setHandshakeHandler(this);
if (NetImpl.SEND_GAME_JOIN_PACKET) {
Expand All @@ -41,6 +39,14 @@ public PolymerHandshakeHandlerImplLogin(Context context) {
//PolymerSyncUtils.PREPARE_HANDSHAKE.invoke((c) -> c.accept(this));
}

@Nullable
public static EarlyPlayNetworkHandler create(Context context) {
if (PolymerCommonUtils.isBedrockPlayer(context.player())) {
return null;
}
return new PolymerHandshakeHandlerImplLogin(context);
}

public void set(String polymerVersion, Object2IntMap<Identifier> protocolVersions) {
this.polymerVersion = polymerVersion;
this.protocolVersions = protocolVersions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ public static Packet<ClientPlayPacketListener> createSimpleMovePacket(int id, Ve
}

public static EntityPassengersSetS2CPacket createRidePacket(int id, IntList list) {
return createRidePacket(id, list.toIntArray());
}

public static EntityPassengersSetS2CPacket createRidePacket(int id, int[] list) {
var packet = VirtualEntityImplUtils.createUnsafe(EntityPassengersSetS2CPacket.class);
((EntityPassengersSetS2CPacketAccessor) packet).setId(id);
((EntityPassengersSetS2CPacketAccessor) packet).setPassengerIds(list.toIntArray());
((EntityPassengersSetS2CPacketAccessor) packet).setPassengerIds(list);
return packet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public final int getEntityId() {
public void setOffset(Vec3d offset) {
super.setOffset(offset);
if (this.getHolder() != null) {
this.getHolder().sendPacket(VirtualEntityUtils.createMovePacket(this.id, this.getHolder().getPos().add(this.getOffset()), this.getHolder().getPos().add(offset), false, 0f, 0f));
var x = VirtualEntityUtils.createMovePacket(this.id, this.getHolder().getPos().add(this.getOffset()), this.getHolder().getPos().add(offset), false, 0f, 0f);
if (x != null) {
this.getHolder().sendPacket(x);
}
}
}

Expand Down Expand Up @@ -113,7 +116,10 @@ protected void sendChangedTrackerEntries(ServerPlayerEntity player, Consumer<Pac

@Override
public void notifyMove(Vec3d oldPos, Vec3d newPos, Vec3d delta) {
this.getHolder().sendPacket(VirtualEntityUtils.createMovePacket(this.id, oldPos.add(this.getOffset()), newPos.add(this.getOffset()), this.isRotationDirty, this.yaw, this.pitch));
var x = VirtualEntityUtils.createMovePacket(this.id, oldPos.add(this.getOffset()), newPos.add(this.getOffset()), this.isRotationDirty, this.yaw, this.pitch);
if (x != null) {
this.getHolder().sendPacket(x);
}
this.isRotationDirty = false;
}

Expand Down

0 comments on commit 329713d

Please sign in to comment.