Don't use -1 to indicate no vehicle

This commit is contained in:
Nassim Jahnke 2024-09-04 19:21:06 +02:00
parent 75537d3f85
commit cd427a2a39
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
3 changed files with 12 additions and 22 deletions

View File

@ -135,7 +135,6 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol<ClientboundPack
@Override
public void init(final UserConnection connection) {
addEntityTracker(connection, new EntityTrackerBase(connection, EntityTypes1_20_5.PLAYER));
connection.put(new ClientVehicleStorage());
}
@Override

View File

@ -122,7 +122,7 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter<Clientbound
wrapper.write(Types.VAR_INT, 64); // Sea level
trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world);
wrapper.user().get(ClientVehicleStorage.class).clear();
wrapper.user().remove(ClientVehicleStorage.class);
});
protocol.registerClientbound(ClientboundPackets1_21.PLAYER_POSITION, wrapper -> {
@ -157,31 +157,36 @@ public final class EntityPacketRewriter1_21_2 extends EntityRewriter<Clientbound
final int vehicleId = wrapper.passthrough(Types.VAR_INT);
final int[] passengerIds = wrapper.passthrough(Types.VAR_INT_ARRAY_PRIMITIVE);
final ClientVehicleStorage storage = wrapper.user().get(ClientVehicleStorage.class);
if (vehicleId == storage.vehicleId()) {
storage.clear();
if (storage != null && vehicleId == storage.vehicleId()) {
wrapper.user().remove(ClientVehicleStorage.class);
}
final int clientEntityId = tracker(wrapper.user()).clientEntityId();
for (final int passenger : passengerIds) {
if (passenger == clientEntityId) {
storage.setVehicleId(vehicleId);
wrapper.user().put(new ClientVehicleStorage(vehicleId));
break;
}
}
});
protocol.appendClientbound(ClientboundPackets1_21.REMOVE_ENTITIES, wrapper -> {
final ClientVehicleStorage vehicleStorage = wrapper.user().get(ClientVehicleStorage.class);
if (vehicleStorage == null) {
return;
}
final int[] entityIds = wrapper.get(Types.VAR_INT_ARRAY_PRIMITIVE, 0);
for (final int entityId : entityIds) {
if (entityId == vehicleStorage.vehicleId()) {
vehicleStorage.clear();
wrapper.user().remove(ClientVehicleStorage.class);
break;
}
}
});
protocol.registerServerbound(ServerboundPackets1_21_2.PLAYER_INPUT, wrapper -> {
// Previously only used while in a vehicle, now always sent
// Filter them appropriately
if (wrapper.user().get(ClientVehicleStorage.class).vehicleId() == -1) {
if (!wrapper.user().has(ClientVehicleStorage.class)) {
wrapper.cancel();
return;
}

View File

@ -19,19 +19,5 @@ package com.viaversion.viaversion.protocols.v1_21to1_21_2.storage;
import com.viaversion.viaversion.api.connection.StorableObject;
public class ClientVehicleStorage implements StorableObject {
private int vehicleId = -1;
public int vehicleId() {
return this.vehicleId;
}
public void setVehicleId(final int vehicleId) {
this.vehicleId = vehicleId;
}
public void clear() {
this.vehicleId = -1;
}
public record ClientVehicleStorage(int vehicleId) implements StorableObject {
}