Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle GameState 13 in 1.20.2 -> 1.20.3 correctly. #643

Merged
merged 7 commits into from
Dec 8, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.viaversion.viabackwards.protocol.protocol1_20_2to1_20_3.rewriter.BlockItemPacketRewriter1_20_3;
import com.viaversion.viabackwards.protocol.protocol1_20_2to1_20_3.rewriter.EntityPacketRewriter1_20_3;
import com.viaversion.viabackwards.protocol.protocol1_20_2to1_20_3.storage.ResourcepackIDStorage;
import com.viaversion.viabackwards.protocol.protocol1_20_2to1_20_3.storage.SpawnPositionStorage;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_3;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
Expand All @@ -34,6 +36,7 @@
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.libs.fastutil.Pair;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.rewriter.CommandRewriter1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2;
Expand All @@ -47,6 +50,7 @@
import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;

import java.util.BitSet;
import java.util.UUID;

Expand Down Expand Up @@ -307,6 +311,27 @@ public void register() {
}
}
});
registerClientbound(ClientboundPackets1_20_3.SPAWN_POSITION, wrapper -> {
final Position position = wrapper.passthrough(Type.POSITION1_14);
final float angle = wrapper.passthrough(Type.FLOAT);

wrapper.user().get(SpawnPositionStorage.class).setSpawnPosition(Pair.of(position, angle));
});
registerClientbound(ClientboundPackets1_20_3.GAME_EVENT, wrapper -> {
final short reason = wrapper.passthrough(Type.UNSIGNED_BYTE);

if (reason == 13) { // Level chunks load start
wrapper.cancel();
final Pair<Position, Float> spawnPositionAndAngle = wrapper.user().get(SpawnPositionStorage.class).getSpawnPosition();

// To emulate the old behavior, we send a fake spawn pos packet containing the actual spawn pos which forces
// the 1.20.2 client to close the downloading terrain screen like the new game state does
final PacketWrapper spawnPosition = wrapper.create(ClientboundPackets1_20_2.SPAWN_POSITION);
spawnPosition.write(Type.POSITION1_14, spawnPositionAndAngle.first()); // position
spawnPosition.write(Type.FLOAT, spawnPositionAndAngle.second()); // angle
spawnPosition.send(Protocol1_20_2To1_20_3.class, true);
}
});

cancelClientbound(ClientboundPackets1_20_3.RESOURCE_PACK_POP);
registerServerbound(ServerboundPackets1_20_2.RESOURCE_PACK_STATUS, resourcePackStatusHandler());
Expand All @@ -326,7 +351,6 @@ private PacketHandler resourcePackStatusHandler() {
};
}


private PacketHandler resourcePackHandler() {
return wrapper -> {
final UUID uuid = wrapper.read(Type.UUID);
Expand All @@ -353,6 +377,7 @@ private void convertOptionalComponent(final PacketWrapper wrapper) throws Except

@Override
public void init(final UserConnection connection) {
connection.put(new SpawnPositionStorage());
addEntityTracker(connection, new EntityTrackerBase(connection, EntityTypes1_20_3.PLAYER));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import com.viaversion.viabackwards.api.rewriters.EntityRewriter;
import com.viaversion.viabackwards.protocol.protocol1_20_2to1_20_3.Protocol1_20_2To1_20_3;
import com.viaversion.viabackwards.protocol.protocol1_20_2to1_20_3.storage.SpawnPositionStorage;
import com.viaversion.viaversion.api.data.ParticleMappings;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_3;
import com.viaversion.viaversion.api.minecraft.metadata.MetaType;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_20_2;
Expand Down Expand Up @@ -68,20 +70,30 @@ public void register() {
map(Type.BOOLEAN); // Limited crafting
map(Type.STRING); // Dimension key
map(Type.STRING); // World

handler(spawnPositionHandler());
handler(worldDataTrackerHandlerByKey());
}
});

protocol.registerClientbound(ClientboundPackets1_20_3.RESPAWN, new PacketHandlers() {
@Override
public void register() {
map(Type.STRING); // Dimension
map(Type.STRING); // World

handler(spawnPositionHandler());
handler(worldDataTrackerHandlerByKey());
}
});
}

private PacketHandler spawnPositionHandler() {
return wrapper -> {
final String world = wrapper.get(Type.STRING, 1);
wrapper.user().get(SpawnPositionStorage.class).setDimension(world);
};
}

@Override
protected void registerRewrites() {
filter().handler((event, meta) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2023 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.protocol.protocol1_20_2to1_20_3.storage;

import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.libs.fastutil.Pair;

import java.util.Objects;

public class SpawnPositionStorage implements StorableObject {
public static final Pair<Position, Float> DEFAULT_SPAWN_POSITION = Pair.of(new Position(8, 64, 8), 0.0F); // Default values copied from the original client

private Pair<Position, Float> spawnPosition;
private String dimension;

public Pair<Position, Float> getSpawnPosition() {
return spawnPosition;
}

public void setSpawnPosition(final Pair<Position, Float> spawnPosition) {
this.spawnPosition = spawnPosition;
}

/**
* Sets the dimension and resets the spawn position to the default value if the dimension changed.
*
* @param dimension The new dimension
*/
public void setDimension(final String dimension) {
final boolean changed = !Objects.equals(this.dimension, dimension);
this.dimension = dimension;

if (changed) {
this.spawnPosition = DEFAULT_SPAWN_POSITION;
}
}
}