3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-11-17 05:20:10 +01:00

Spawn falling blocks at correct position

Dieser Commit ist enthalten in:
FlorianMichael 2024-08-26 22:28:12 +02:00
Ursprung a57dd7064c
Commit 3a4014623e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: C2FB87E71C425126
3 geänderte Dateien mit 105 neuen und 21 gelöschten Zeilen

Datei anzeigen

@ -20,22 +20,26 @@ package com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.rewriter;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler;
import com.viaversion.viabackwards.api.entities.storage.EntityReplacement;
import com.viaversion.viabackwards.api.rewriters.EntityRewriter;
import com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.Protocol1_19_4To1_19_3;
import com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.storage.LinkedEntityStorage;
import com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.storage.EntityTracker1_19_4;
import com.viaversion.viaversion.api.data.entity.StoredEntityData;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19_3;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19_4;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityData;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.version.Types1_19_3;
import com.viaversion.viaversion.api.type.types.version.Types1_19_4;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.protocols.v1_13_2to1_14.packet.ClientboundPackets1_14;
import com.viaversion.viaversion.protocols.v1_19_1to1_19_3.packet.ClientboundPackets1_19_3;
import com.viaversion.viaversion.protocols.v1_19_3to1_19_4.packet.ClientboundPackets1_19_4;
import com.viaversion.viaversion.util.TagUtil;
@ -48,10 +52,40 @@ public final class EntityPacketRewriter1_19_4 extends EntityRewriter<Clientbound
@Override
public void registerPackets() {
registerTrackerWithData1_19(ClientboundPackets1_19_4.ADD_ENTITY, EntityTypes1_19_4.FALLING_BLOCK);
registerRemoveEntities(ClientboundPackets1_19_4.REMOVE_ENTITIES);
registerSetEntityData(ClientboundPackets1_19_4.SET_ENTITY_DATA, Types1_19_4.ENTITY_DATA_LIST, Types1_19_3.ENTITY_DATA_LIST);
protocol.registerClientbound(ClientboundPackets1_19_4.ADD_ENTITY, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Entity id
map(Types.UUID); // Entity UUID
map(Types.VAR_INT); // Entity type
map(Types.DOUBLE); // X
map(Types.DOUBLE); // Y
map(Types.DOUBLE); // Z
map(Types.BYTE); // Pitch
map(Types.BYTE); // Yaw
map(Types.BYTE); // Head yaw
map(Types.VAR_INT); // Data
handler(wrapper -> {
final int entityId = wrapper.get(Types.VAR_INT, 0);
final int entityType = wrapper.get(Types.VAR_INT, 1);
// First track (and remap) entity, then put storage for block display entity
getSpawnTrackerWithDataHandler1_19(EntityTypes1_19_4.FALLING_BLOCK).handle(wrapper);
if (entityType != EntityTypes1_19_4.BLOCK_DISPLAY.getId()) {
return;
}
final StoredEntityData data = tracker(wrapper.user()).entityData(entityId);
if (data != null) {
data.put(new LinkedEntityStorage());
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_19_4.LOGIN, new PacketHandlers() {
@Override
public void register() {
@ -142,6 +176,40 @@ public final class EntityPacketRewriter1_19_4 extends EntityRewriter<Clientbound
final int duration = wrapper.read(Types.VAR_INT);
wrapper.write(Types.VAR_INT, duration == -1 ? 999999 : duration);
});
// Track the position of block display entities to later spawn the linked entities, we will put them
// as passengers but the spawn position needs to be in the players view distance
protocol.registerClientbound(ClientboundPackets1_19_4.TELEPORT_ENTITY, wrapper -> {
final int entityId = wrapper.passthrough(Types.VAR_INT);
final double x = wrapper.passthrough(Types.DOUBLE);
final double y = wrapper.passthrough(Types.DOUBLE);
final double z = wrapper.passthrough(Types.DOUBLE);
final EntityTracker1_19_4 tracker = tracker(wrapper.user());
final LinkedEntityStorage storage = tracker.linkedEntityStorage(entityId);
if (storage == null) {
return;
}
storage.setCoordinates(x, y, z, false);
});
final PacketHandler entityPositionHandler = wrapper -> {
final int entityId = wrapper.passthrough(Types.VAR_INT);
final double x = wrapper.passthrough(Types.SHORT) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
final double y = wrapper.passthrough(Types.SHORT) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
final double z = wrapper.passthrough(Types.SHORT) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
final EntityTracker1_19_4 tracker = tracker(wrapper.user());
final LinkedEntityStorage storage = tracker.linkedEntityStorage(entityId);
if (storage == null) {
return;
}
storage.setCoordinates(x, y, z, true);
};
protocol.registerClientbound(ClientboundPackets1_19_4.MOVE_ENTITY_POS, entityPositionHandler);
protocol.registerClientbound(ClientboundPackets1_19_4.MOVE_ENTITY_POS_ROT, entityPositionHandler);
}
@Override
@ -180,10 +248,14 @@ public final class EntityPacketRewriter1_19_4 extends EntityRewriter<Clientbound
final int value = data.value();
final EntityTracker1_19_4 tracker = tracker(event.user());
tracker.clearLinkedEntityStorage(event.entityId());
tracker.clearLinkedEntities(event.entityId());
final int linkedEntity = tracker.spawnEntity(EntityTypes1_19_3.FALLING_BLOCK, value);
tracker.entityData(event.entityId()).put(new LinkedEntityStorage(linkedEntity));
final LinkedEntityStorage storage = tracker.linkedEntityStorage(event.entityId());
if (storage == null) {
return;
}
final int linkedEntity = tracker.spawnEntity(EntityTypes1_19_3.FALLING_BLOCK, storage.x(), storage.y(), storage.z(), value);
storage.setEntities(linkedEntity);
final PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_19_3.SET_PASSENGERS, event.user());
wrapper.write(Types.VAR_INT, event.entityId()); // Entity id

Datei anzeigen

@ -39,16 +39,16 @@ public final class EntityTracker1_19_4 extends EntityTrackerBase {
super(connection, EntityTypes1_19_4.PLAYER);
}
public int spawnEntity(final EntityTypes1_19_3 entityType, final int data) {
public int spawnEntity(final EntityTypes1_19_3 entityType, final double x, final double y, final double z, final int data) {
final int entityId = nextEntityId();
final PacketWrapper addEntity = PacketWrapper.create(ClientboundPackets1_19_3.ADD_ENTITY, user());
addEntity.write(Types.VAR_INT, entityId); // Entity id
addEntity.write(Types.UUID, UUID.randomUUID()); // Entity UUID
addEntity.write(Types.VAR_INT, entityType.getId()); // Entity type
addEntity.write(Types.DOUBLE, 0.0); // X
addEntity.write(Types.DOUBLE, 0.0); // Y
addEntity.write(Types.DOUBLE, 0.0); // Z
addEntity.write(Types.DOUBLE, x); // X
addEntity.write(Types.DOUBLE, y); // Y
addEntity.write(Types.DOUBLE, z); // Z
addEntity.write(Types.BYTE, (byte) 0); // Pitch
addEntity.write(Types.BYTE, (byte) 0); // Yaw
addEntity.write(Types.BYTE, (byte) 0); // Head yaw
@ -63,34 +63,36 @@ public final class EntityTracker1_19_4 extends EntityTrackerBase {
return entityId;
}
@Override
public void clearEntities() {
for (final int id : entities.keySet()) {
clearLinkedEntityStorage(id);
clearLinkedEntities(id);
}
super.clearEntities();
}
@Override
public void removeEntity(final int id) {
clearLinkedEntityStorage(id);
clearLinkedEntities(id);
super.removeEntity(id);
}
public void clearLinkedEntityStorage(final int id) {
final TrackedEntity entity = entity(id);
if (entity == null || !entity.hasData()) {
return;
}
final LinkedEntityStorage storage = entity.data().get(LinkedEntityStorage.class);
if (storage != null) {
public void clearLinkedEntities(final int id) {
final LinkedEntityStorage storage = linkedEntityStorage(id);
if (storage != null && storage.entities() != null) {
storage.remove(user());
generatedEntities.remove(id);
}
}
public LinkedEntityStorage linkedEntityStorage(final int id) {
final TrackedEntity entity = entity(id);
if (entity != null && entity.hasData()) {
return entity.data().get(LinkedEntityStorage.class);
}
return null;
}
private int nextEntityId() {
final int entityId = -ThreadLocalRandom.current().nextInt(10_000);
if (generatedEntities.contains(entityId)) {

Datei anzeigen

@ -17,6 +17,7 @@
*/
package com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.storage;
import com.viaversion.viabackwards.api.entities.storage.EntityPositionStorage;
import com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.Protocol1_19_4To1_19_3;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.connection.UserConnection;
@ -24,7 +25,17 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.protocols.v1_19_1to1_19_3.packet.ClientboundPackets1_19_3;
public record LinkedEntityStorage(int... entities) implements StorableObject {
public class LinkedEntityStorage extends EntityPositionStorage implements StorableObject {
private int[] entities;
public int[] entities() {
return entities;
}
public void setEntities(final int... entities) {
this.entities = entities;
}
public void remove(final UserConnection connection) {
final PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_19_3.REMOVE_ENTITIES, connection);
@ -32,5 +43,4 @@ public record LinkedEntityStorage(int... entities) implements StorableObject {
wrapper.send(Protocol1_19_4To1_19_3.class);
}
}