Skip to content

Commit

Permalink
Fix sync on Velocity servers (1.19.1) (#69)
Browse files Browse the repository at this point in the history
* Fix sync on Velocity servers

Partial backport of 3d83cd3

Signed-off-by: unilock <[email protected]>

* Move proxy mod checks to main module

Signed-off-by: unilock <[email protected]>

---------

Signed-off-by: unilock <[email protected]>
  • Loading branch information
Katherine authored May 20, 2023
1 parent a151e50 commit 72d6efe
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import net.minecraft.network.packet.c2s.play.ResourcePackStatusC2SPacket;
import net.minecraft.network.packet.s2c.play.*;
import net.minecraft.text.Text;
import net.minecraft.world.GameMode;

import java.util.Optional;

public class ResourcePackNetworkHandler extends EarlyPlayNetworkHandler {
private final boolean required;
Expand All @@ -34,8 +31,7 @@ public ResourcePackNetworkHandler(Context context) {
if (PolymerRPUtils.hasPack(player)) {
this.continueJoining();
} else {
var server = this.getServer();
this.sendPacket(new GameJoinS2CPacket(player.getId(), false, GameMode.SPECTATOR, null, server.getWorldRegistryKeys(), server.getRegistryManager(), server.getOverworld().getDimensionKey(), server.getOverworld().getRegistryKey(), 0, server.getPlayerManager().getMaxPlayerCount(), 2, 2, false, false, false, true, Optional.empty()));
this.sendInitialGameJoin();

//this.sendPacket(new ChunkDataS2CPacket(FAKE_CHUNK, PolymerUtils.getFakeWorld().getLightingProvider(), null, null, true));
this.sendPacket(FAKE_ENTITY.createSpawnPacket());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.listener.ServerPlayPacketListener;
import net.minecraft.network.packet.c2s.play.*;
import net.minecraft.network.packet.s2c.play.DisconnectS2CPacket;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.network.packet.s2c.play.KeepAliveS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerRespawnS2CPacket;
import net.minecraft.network.packet.s2c.play.*;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.world.GameMode;
import org.jetbrains.annotations.ApiStatus;

import java.util.Optional;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -87,6 +86,10 @@ protected void forceRespawnPacket() {
((TempPlayerLoginAttachments) this.getPlayer()).polymer_setForceRespawnPacket();
}

protected boolean isForcingRespawnPacket() {
return ((TempPlayerLoginAttachments) this.getPlayer()).polymer_getForceRespawnPacket();
}

@Override
public final void onKeepAlive(KeepAliveC2SPacket packet) {
this.lastRespose = -20;
Expand All @@ -103,7 +106,7 @@ public final void sendPacket(Packet<?> packet) {
this.context.connection().send(packet);

if (packet instanceof GameJoinS2CPacket packet1) {
if (((TempPlayerLoginAttachments) this.getPlayer()).polymer_getForceRespawnPacket()) {
if (this.isForcingRespawnPacket()) {
this.context.connection().send(new PlayerRespawnS2CPacket(packet1.dimensionType(), packet1.dimensionId(), packet1.sha256Seed(), packet1.gameMode(), packet1.previousGameMode(), packet1.debugWorld(), packet1.flatWorld(), false, packet1.lastDeathLocation()));
}

Expand All @@ -120,6 +123,10 @@ public final void sendKeepAlive() {
this.sendKeepAlive(System.currentTimeMillis());
}

public final void sendPing(int id) {
this.sendPacket(new PlayPingS2CPacket(id));
}

@ApiStatus.Internal
public final void tickInternal() {
if (this.lastRespose++ == 1200) {
Expand All @@ -138,6 +145,14 @@ public final void onCustomPayload(CustomPayloadC2SPacket packet) {
}
}

public final void sendInitialGameJoin() {
if (!this.isForcingRespawnPacket()) {
var player = this.getPlayer();
var server = this.getServer();
this.sendPacket(new GameJoinS2CPacket(player.getId(), false, GameMode.SPECTATOR, null, server.getWorldRegistryKeys(), server.getRegistryManager(), server.getOverworld().getDimensionKey(), server.getOverworld().getRegistryKey(), 0, server.getPlayerManager().getMaxPlayerCount(), 2, 2, false, false, false, true, Optional.empty()));
}
}

@Override
public final void onDisconnected(Text reason) {
for (var packets : this.context.storedPackets()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public final class CompatStatus {
public static final boolean JEI = LOADER.isModLoaded("jei");
public static final boolean EMI = LOADER.isModLoaded("emi");

public static final boolean FABRIC_PROXY_LITE = LOADER.isModLoaded("fabricproxy-lite");
public static final boolean FABRIC_PROXY_LEGACY = LOADER.isModLoaded("fabricproxy-legacy");
public static final boolean FABRIC_PROXY = LOADER.isModLoaded("fabricproxy");
public static final boolean QFORWARD = LOADER.isModLoaded("qforward");

public static final boolean PROXY_MODS = FABRIC_PROXY || FABRIC_PROXY_LEGACY || FABRIC_PROXY_LITE || QFORWARD;

public static final boolean IRIS = LOADER.isModLoaded("iris");
public static final boolean CANVAS = LOADER.isModLoaded("canvas");
public static final boolean OPTIBAD = LOADER.isModLoaded("optifabric");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package eu.pb4.polymer.impl.networking;

public class NetConfig {
public String _c1 = "Sends GameJoin packet, only enable if sync does work (most likely for servers under proxy)";
public boolean sendGameJoinBeforeSync = false;
}
14 changes: 14 additions & 0 deletions polymer/src/main/java/eu/pb4/polymer/impl/networking/NetImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eu.pb4.polymer.impl.networking;

import eu.pb4.polymer.impl.PolymerImpl;
import eu.pb4.polymer.impl.compat.CompatStatus;

public class NetImpl {
public static final boolean SEND_GAME_JOIN_PACKET;

static {
var config = PolymerImpl.loadConfig("networking", NetConfig.class);

SEND_GAME_JOIN_PACKET = config.sendGameJoinBeforeSync || CompatStatus.PROXY_MODS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayPongC2SPacket;
import net.minecraft.network.packet.c2s.play.ResourcePackStatusC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;

public class PolymerHandshakeHandlerImplLogin extends EarlyPlayNetworkHandler implements PolymerHandshakeHandler {
public static long MAGIC_VALUE = 0xbb706c6d72627374L;
public static long MAGIC_INIT_VALUE = 0xbb706c6d72627374L;
public static int CONTINUE_LOGIN_ID = 1;

private String polymerVersion = "";
private Object2IntMap<String> protocolVersions = null;
Expand All @@ -27,7 +29,10 @@ public class PolymerHandshakeHandlerImplLogin extends EarlyPlayNetworkHandler im
public PolymerHandshakeHandlerImplLogin(EarlyPlayNetworkHandler.Context context) {
super(PolymerImplUtils.id("early_handshake"), context);
((TempPlayerLoginAttachments) this.getPlayer()).polymer_setHandshakeHandler(this);
this.sendKeepAlive(MAGIC_VALUE);
if (NetImpl.SEND_GAME_JOIN_PACKET) {
this.sendInitialGameJoin();
}
this.sendKeepAlive(MAGIC_INIT_VALUE);
this.blockMapper = BlockMapper.getDefault(this.getPlayer());

PolymerSyncUtils.PREPARE_HANDSHAKE.invoke((c) -> c.accept(this));
Expand Down Expand Up @@ -124,7 +129,14 @@ public boolean handleCustomPayload(CustomPayloadC2SPacket packet) {

@Override
public void handleKeepAlive(long value) {
if (value == MAGIC_VALUE) {
if (value == MAGIC_INIT_VALUE) {
this.sendPing(CONTINUE_LOGIN_ID);
}
}

@Override
public void onPong(PlayPongC2SPacket packet) {
if (packet.getParameter() == CONTINUE_LOGIN_ID) {
this.continueJoining();
}
}
Expand All @@ -139,4 +151,4 @@ public void onResourcePackStatus(ResourcePackStatusC2SPacket packet) {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void polymer_catchPackets(CustomPayloadS2CPacket packet, CallbackInfo ci
private void polymer_handleHackfest(KeepAliveS2CPacket packet, CallbackInfo ci) {
// Yes, it's a hack but it works quite well!
// I should replace it with some api later
if (packet.getId() == PolymerHandshakeHandlerImplLogin.MAGIC_VALUE) {
if (packet.getId() == PolymerHandshakeHandlerImplLogin.MAGIC_INIT_VALUE) {
PolymerClientProtocol.sendHandshake((ClientPlayNetworkHandler) (Object) this);
}
}
Expand Down

0 comments on commit 72d6efe

Please sign in to comment.