3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-25 15:50:10 +01:00

Fix ACCEPT_TELEPORTATION packet order (#11)

Dieser Commit ist enthalten in:
RK_01 2024-10-22 11:13:45 +02:00 committet von GitHub
Ursprung 82199d9f39
Commit 2b37178482
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
4 geänderte Dateien mit 175 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -46,6 +46,8 @@ import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ServerboundPacke
import com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter.BlockItemPacketRewriter1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter.ComponentRewriter1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter.EntityPacketRewriter1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.BundleStateTracker;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.PlayerPositionStorage;
import com.viaversion.viaversion.rewriter.AttributeRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
@ -159,6 +161,14 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol<ClientboundPack
componentRewriter.processTag(wrapper.user(), wrapper.passthrough(Types.OPTIONAL_TAG));
}
});
registerClientbound(ClientboundPackets1_21.BUNDLE_DELIMITER, wrapper -> wrapper.user().get(BundleStateTracker.class).toggleBundling());
registerServerbound(ServerboundPackets1_21_2.PONG, wrapper -> {
final int id = wrapper.passthrough(Types.INT); // id
if (wrapper.user().get(PlayerPositionStorage.class).checkPong(id)) {
wrapper.cancel();
}
});
}
private void clientInformation(final PacketWrapper wrapper) {
@ -215,6 +225,8 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol<ClientboundPack
@Override
public void init(final UserConnection connection) {
addEntityTracker(connection, new EntityTrackerBase(connection, EntityTypes1_21_2.PLAYER));
connection.put(new BundleStateTracker());
connection.put(new PlayerPositionStorage());
}
@Override

Datei anzeigen

@ -32,9 +32,12 @@ import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacke
import com.viaversion.viaversion.protocols.v1_21to1_21_2.Protocol1_21To1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ClientboundPackets1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ServerboundPackets1_21_2;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.BundleStateTracker;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.ClientVehicleStorage;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.storage.PlayerPositionStorage;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.RegistryDataRewriter;
import java.util.concurrent.ThreadLocalRandom;
public final class EntityPacketRewriter1_21_2 extends EntityRewriter<ClientboundPacket1_21, Protocol1_21To1_21_2> {
@ -151,6 +154,27 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter<Clientbound
final int teleportId = wrapper.read(Types.VAR_INT);
wrapper.set(Types.VAR_INT, 0, teleportId);
// Accept teleportation and player position were swapped.
// Send a ping first to then capture and send the player position the accept teleportation
final boolean isBundling = wrapper.user().get(BundleStateTracker.class).isBundling();
if (!isBundling) {
final PacketWrapper bundleStart = wrapper.create(ClientboundPackets1_21_2.BUNDLE_DELIMITER);
bundleStart.send(Protocol1_21To1_21_2.class);
}
final int pingId = ThreadLocalRandom.current().nextInt();
wrapper.user().get(PlayerPositionStorage.class).addPendingPong(pingId);
final PacketWrapper ping = wrapper.create(ClientboundPackets1_21_2.PING);
ping.write(Types.INT, pingId); // id
ping.send(Protocol1_21To1_21_2.class);
wrapper.send(Protocol1_21To1_21_2.class);
wrapper.cancel();
if (!isBundling) {
final PacketWrapper bundleEnd = wrapper.create(ClientboundPackets1_21_2.BUNDLE_DELIMITER);
bundleEnd.send(Protocol1_21To1_21_2.class);
}
});
protocol.registerClientbound(ClientboundPackets1_21.SET_PASSENGERS, wrapper -> {
@ -236,12 +260,20 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter<Clientbound
readOnGround(wrapper);
});
protocol.registerServerbound(ServerboundPackets1_21_2.MOVE_PLAYER_POS_ROT, wrapper -> {
wrapper.passthrough(Types.DOUBLE); // X
wrapper.passthrough(Types.DOUBLE); // Y
wrapper.passthrough(Types.DOUBLE); // Z
wrapper.passthrough(Types.FLOAT); // Yaw
wrapper.passthrough(Types.FLOAT); // Pitch
final double x = wrapper.passthrough(Types.DOUBLE); // X
final double y = wrapper.passthrough(Types.DOUBLE); // Y
final double z = wrapper.passthrough(Types.DOUBLE); // Z
final float yaw = wrapper.passthrough(Types.FLOAT); // Yaw
final float pitch = wrapper.passthrough(Types.FLOAT); // Pitch
readOnGround(wrapper);
final PlayerPositionStorage playerPositionStorage = wrapper.user().get(PlayerPositionStorage.class);
if (playerPositionStorage.checkCaptureNextPlayerPositionPacket()) {
// Capture this packet and send it after accept teleportation
final boolean onGround = wrapper.get(Types.BOOLEAN, 0);
playerPositionStorage.setPlayerPosition(new PlayerPositionStorage.PlayerPosition(x, y, z, yaw, pitch, onGround));
wrapper.cancel();
}
});
protocol.registerServerbound(ServerboundPackets1_21_2.MOVE_PLAYER_ROT, wrapper -> {
wrapper.passthrough(Types.FLOAT); // Yaw
@ -249,6 +281,15 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter<Clientbound
readOnGround(wrapper);
});
protocol.registerServerbound(ServerboundPackets1_21_2.MOVE_PLAYER_STATUS_ONLY, this::readOnGround);
protocol.registerServerbound(ServerboundPackets1_21_2.ACCEPT_TELEPORTATION, wrapper -> {
final PlayerPositionStorage playerPositionStorage = wrapper.user().get(PlayerPositionStorage.class);
if (playerPositionStorage.hasPlayerPosition()) {
// Send move player after accept teleportation
wrapper.sendToServer(Protocol1_21To1_21_2.class);
wrapper.cancel();
playerPositionStorage.sendMovePlayerPosRot(wrapper.user());
}
});
}
private RegistryDataRewriter registryDataRewriter() {

Datei anzeigen

@ -0,0 +1,34 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 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.viaversion.protocols.v1_21to1_21_2.storage;
import com.viaversion.viaversion.api.connection.StorableObject;
public class BundleStateTracker implements StorableObject {
private boolean bundling;
public boolean isBundling() {
return this.bundling;
}
public void toggleBundling() {
this.bundling = !this.bundling;
}
}

Datei anzeigen

@ -0,0 +1,83 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 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.viaversion.protocols.v1_21to1_21_2.storage;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPackets1_20_5;
import com.viaversion.viaversion.protocols.v1_21to1_21_2.Protocol1_21To1_21_2;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
public class PlayerPositionStorage implements StorableObject {
private final IntSet pendingPongs = new IntOpenHashSet();
private boolean captureNextPlayerPositionPacket;
private PlayerPosition playerPosition;
public void addPendingPong(final int id) {
if (!this.pendingPongs.add(id)) {
throw new IllegalStateException("Pong already pending for id " + id);
}
}
public boolean checkPong(final int id) {
if (this.pendingPongs.remove(id)) {
this.captureNextPlayerPositionPacket = true;
return true;
} else {
return false;
}
}
public boolean checkCaptureNextPlayerPositionPacket() {
if (this.captureNextPlayerPositionPacket) {
this.captureNextPlayerPositionPacket = false;
return true;
} else {
return false;
}
}
public void setPlayerPosition(final PlayerPosition playerPosition) {
this.playerPosition = playerPosition;
}
public boolean hasPlayerPosition() {
return this.playerPosition != null;
}
public void sendMovePlayerPosRot(final UserConnection user) {
final PacketWrapper movePlayerPosRot = PacketWrapper.create(ServerboundPackets1_20_5.MOVE_PLAYER_POS_ROT, user);
movePlayerPosRot.write(Types.DOUBLE, this.playerPosition.x); // X
movePlayerPosRot.write(Types.DOUBLE, this.playerPosition.y); // Y
movePlayerPosRot.write(Types.DOUBLE, this.playerPosition.z); // Z
movePlayerPosRot.write(Types.FLOAT, this.playerPosition.yaw); // Yaw
movePlayerPosRot.write(Types.FLOAT, this.playerPosition.pitch); // Pitch
movePlayerPosRot.write(Types.BOOLEAN, this.playerPosition.onGround); // On Ground
movePlayerPosRot.sendToServer(Protocol1_21To1_21_2.class);
this.captureNextPlayerPositionPacket = false;
this.playerPosition = null;
}
public record PlayerPosition(double x, double y, double z, float yaw, float pitch, boolean onGround) {
}
}