Server: track boats, minecarts, armor stands, frames and paintings to clients
A server sent its clients only mobs and dropped items. Boats, every kind of minecart, armor stands, item frames, paintings and the entities Horizon keeps without simulating are now tracked as ServerEntity does: the AddEntity data (hanging entities at their block, with their direction), the synched data that differs from the defaults, equipment, passengers (ClientboundSetPassengersPacket at pairing and on change; a passenger syncs only its rotation and resyncs its position when it dismounts), and the type's update interval and range. Also vanilla-exact on the way, as the new harness found: - mobs pair with their health (always non-default) and Mob flags (no AI, left-handed); - /summon without NBT summons any type from an empty compound; - armor stands move before gravity (LivingEntity.travelInAir), so a stand at rest keeps vanilla's -0.0784 vertical motion. tools/test_server_entities_parity.py compares, from a vanilla-codec client, what vanilla's dedicated server and Horizon's track for a scene of 20 entities. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
|
||||
import net.minecraft.network.protocol.game.VecDeltaCodec;
|
||||
import net.minecraft.world.entity.EntityTypes;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
/**
|
||||
* What a vanilla client knows about the entities a server tracks to it, as
|
||||
* ClientPacketListener would apply the packets: each entity's spawn packet,
|
||||
* the entity data it was paired with and all it was sent since, its
|
||||
* equipment, attributes and passengers, and where the position syncs leave
|
||||
* it (VecDeltaCodec, as handleMoveEntity and handleEntityPositionSync).
|
||||
* Entity ids differ between servers: an entity is named by its type and the
|
||||
* block it was added in, passengers by those names. Players are left out.
|
||||
*/
|
||||
final class EntityTranscript {
|
||||
private static final class Tracked {
|
||||
final String type;
|
||||
final String add;
|
||||
final VecDeltaCodec codec = new VecDeltaCodec();
|
||||
String name;
|
||||
String pairing;
|
||||
final Map<Integer, String> data = new TreeMap<>();
|
||||
final Map<String, String> equipment = new TreeMap<>();
|
||||
final Map<String, String> attributes = new TreeMap<>();
|
||||
int[] passengers = new int[0];
|
||||
float yRot;
|
||||
float xRot;
|
||||
float headRot;
|
||||
boolean onGround;
|
||||
String motion;
|
||||
boolean removed;
|
||||
|
||||
Tracked(ClientboundAddEntityPacket p) {
|
||||
type = BuiltInRegistries.ENTITY_TYPE.getKey(p.getType()).toString();
|
||||
Vec3 at = new Vec3(p.getX(), p.getY(), p.getZ());
|
||||
codec.setBase(at);
|
||||
name = type + " " + (int) Math.floor(at.x) + "," + (int) Math.floor(at.y) + "," + (int) Math.floor(at.z);
|
||||
add = "at " + at + " rot " + p.getXRot() + "," + p.getYRot() + " head " + p.getYHeadRot() + " data " + p.getData()
|
||||
+ " motion " + p.getMovement();
|
||||
yRot = p.getYRot();
|
||||
xRot = p.getXRot();
|
||||
headRot = p.getYHeadRot();
|
||||
motion = String.valueOf(p.getMovement());
|
||||
}
|
||||
}
|
||||
|
||||
private final Map<Integer, Tracked> byId = new HashMap<>();
|
||||
private final List<Tracked> all = new ArrayList<>();
|
||||
private int unknown;
|
||||
|
||||
/** Applies one packet; false when it isn't about a tracked entity. */
|
||||
boolean accept(Packet<?> packet) {
|
||||
if (packet instanceof ClientboundAddEntityPacket p) {
|
||||
if (p.getType() == EntityTypes.PLAYER) return true;
|
||||
Tracked t = new Tracked(p);
|
||||
byId.put(p.getId(), t);
|
||||
all.add(t);
|
||||
return true;
|
||||
}
|
||||
if (packet instanceof ClientboundRemoveEntitiesPacket p) {
|
||||
p.entityIds().forEach(id -> {
|
||||
Tracked t = byId.remove(id);
|
||||
if (t != null) t.removed = true;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
Tracked t = switch (packet) {
|
||||
case ClientboundSetEntityDataPacket p -> byId.get(p.id());
|
||||
case ClientboundSetEquipmentPacket p -> byId.get(p.getEntity());
|
||||
case ClientboundUpdateAttributesPacket p -> byId.get(p.getEntityId());
|
||||
case ClientboundSetPassengersPacket p -> byId.get(p.getVehicle());
|
||||
case ClientboundEntityPositionSyncPacket p -> byId.get(p.id());
|
||||
case ClientboundSetEntityMotionPacket p -> byId.get(p.id());
|
||||
case ClientboundTeleportEntityPacket p -> byId.get(p.id());
|
||||
case ClientboundMoveEntityPacket p -> byId.get(VanillaProtocolClient.moveEntityId(p));
|
||||
case ClientboundRotateHeadPacket p -> byId.get(VanillaProtocolClient.rotateHeadId(p));
|
||||
default -> null;
|
||||
};
|
||||
if (t == null) {
|
||||
return false;
|
||||
}
|
||||
switch (packet) {
|
||||
case ClientboundSetEntityDataPacket p -> {
|
||||
for (var v : p.packedItems()) t.data.put(v.id(), net.minecraft.network.syncher.EntityDataSerializers.getSerializedId(v.serializer()) + " " + v.value());
|
||||
if (t.pairing == null) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (var v : p.packedItems()) b.append(v.id()).append('=').append(v.value()).append(' ');
|
||||
t.pairing = b.toString().trim();
|
||||
}
|
||||
}
|
||||
case ClientboundSetEquipmentPacket p -> p.getSlots().forEach(s -> t.equipment.put(s.getFirst().getName(), String.valueOf(s.getSecond())));
|
||||
case ClientboundUpdateAttributesPacket p -> p.getValues().forEach(a -> t.attributes.put(a.attribute().getRegisteredName(),
|
||||
a.base() + " " + a.modifiers()));
|
||||
case ClientboundSetPassengersPacket p -> t.passengers = p.getPassengers();
|
||||
case ClientboundEntityPositionSyncPacket p -> {
|
||||
t.codec.setBase(p.position().endPosition());
|
||||
t.yRot = p.yRot();
|
||||
t.xRot = p.xRot();
|
||||
t.onGround = p.onGround();
|
||||
}
|
||||
case ClientboundMoveEntityPacket p -> {
|
||||
if (p.hasPosition()) t.codec.setBase(p.getPositionDelta().decode(t.codec).endPosition());
|
||||
if (p.hasRotation()) {
|
||||
t.yRot = p.getYRot();
|
||||
t.xRot = p.getXRot();
|
||||
}
|
||||
t.onGround = p.isOnGround();
|
||||
}
|
||||
case ClientboundSetEntityMotionPacket p -> t.motion = String.valueOf(p.movement());
|
||||
case ClientboundTeleportEntityPacket p -> {
|
||||
t.codec.setBase(p.change().position());
|
||||
t.onGround = p.onGround();
|
||||
}
|
||||
case ClientboundRotateHeadPacket p -> t.headRot = p.getYHeadRot();
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** One line per entity, sorted, each field named. */
|
||||
List<String> lines() {
|
||||
Map<String, Integer> seen = new HashMap<>();
|
||||
for (Tracked t : all) {
|
||||
int n = seen.merge(t.name, 1, Integer::sum);
|
||||
if (n > 1) t.name += " #" + n;
|
||||
}
|
||||
Map<Integer, String> names = new HashMap<>();
|
||||
byId.forEach((id, t) -> names.put(id, t.name));
|
||||
List<String> out = new ArrayList<>();
|
||||
for (Tracked t : all) {
|
||||
List<String> riders = new ArrayList<>();
|
||||
for (int id : t.passengers) riders.add(names.getOrDefault(id, "player or unknown"));
|
||||
out.add("entity " + t.name + " | add " + t.add + " | pairing " + t.pairing + " | data " + t.data + " | equipment " + t.equipment
|
||||
+ " | attributes " + t.attributes + " | passengers " + riders + " | now " + t.codec.getBase() + " rot " + t.xRot + ","
|
||||
+ t.yRot + " head " + t.headRot + " ground " + t.onGround + " motion " + t.motion + (t.removed ? " | removed" : ""));
|
||||
}
|
||||
out.sort(null);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -113,6 +113,8 @@ public final class VanillaProtocolClient {
|
||||
private ProtocolInfo<?> inbound;
|
||||
private ProtocolInfo<?> outbound;
|
||||
private String state;
|
||||
/** With a fifth argument {@code entities}: the entity transcript mode (EntityTranscript). */
|
||||
private EntityTranscript entities;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ParityData.init();
|
||||
@@ -121,6 +123,7 @@ public final class VanillaProtocolClient {
|
||||
String name = args[2];
|
||||
Path output = Path.of(args[3]);
|
||||
VanillaProtocolClient client = new VanillaProtocolClient();
|
||||
if (args.length > 4 && args[4].equals("entities")) client.entities = new EntityTranscript();
|
||||
try {
|
||||
client.status(host, port);
|
||||
client.join(host, port, name);
|
||||
@@ -405,6 +408,18 @@ public final class VanillaProtocolClient {
|
||||
play();
|
||||
}
|
||||
|
||||
/** The entity mode's step, and how long it records the entities for (ms). */
|
||||
private static final int ENTITIES = 1000;
|
||||
private static final long ENTITY_WAIT = 20_000;
|
||||
|
||||
static int moveEntityId(net.minecraft.network.protocol.game.ClientboundMoveEntityPacket p) {
|
||||
return (int) field(p, net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.class, "entityId");
|
||||
}
|
||||
|
||||
static int rotateHeadId(net.minecraft.network.protocol.game.ClientboundRotateHeadPacket p) {
|
||||
return (int) field(p, p.getClass(), "entityId");
|
||||
}
|
||||
|
||||
private static final String[] REPLIES = {"random value 7..7", "random value x", "trigger nope"};
|
||||
|
||||
private void play() throws Exception {
|
||||
@@ -461,6 +476,7 @@ public final class VanillaProtocolClient {
|
||||
if (r != null) {
|
||||
Packet<?> packet = r.packet;
|
||||
counts.merge(r.type, 1, Integer::sum);
|
||||
if (entities != null && step >= ENTITIES) entities.accept(packet);
|
||||
if (r.type.equals("minecraft:chunk_batch_start")) ordered = false;
|
||||
if (respawning && !respawned) {
|
||||
String line = respawnLine(r);
|
||||
@@ -581,8 +597,20 @@ public final class VanillaProtocolClient {
|
||||
commandTree(p);
|
||||
}
|
||||
}
|
||||
if (entities != null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) {
|
||||
// Entity mode: the harness's console summons the scene once it reads this
|
||||
// line; everything tracked from then on is recorded until the client leaves.
|
||||
record("play-chunks", String.valueOf(chunks.size()));
|
||||
send(new ServerboundChatPacket("summon the entities", Instant.now(), 0L, Optional.empty(),
|
||||
new LastSeenMessages.Update(0, new BitSet(), (byte) 0)));
|
||||
step = ENTITIES;
|
||||
stepAt = now;
|
||||
} else if (step == ENTITIES && now - stepAt > ENTITY_WAIT) {
|
||||
for (String line : entities.lines()) record("play-entities", line);
|
||||
return;
|
||||
}
|
||||
// Once the chunks stop coming: chat, then /list, then leave.
|
||||
if (loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) {
|
||||
if (entities == null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) {
|
||||
record("play-chunks", chunks.size() + " " + chunks);
|
||||
BLOCK_ENTITIES.forEach((k, v) -> record("play-block-entities", k + " " + v));
|
||||
// Which light layers exist depends on the neighbouring
|
||||
@@ -910,8 +938,12 @@ public final class VanillaProtocolClient {
|
||||
}
|
||||
|
||||
private static Object field(Object owner, String name) {
|
||||
return field(owner, owner.getClass(), name);
|
||||
}
|
||||
|
||||
private static Object field(Object owner, Class<?> declaring, String name) {
|
||||
try {
|
||||
var f = owner.getClass().getDeclaredField(name);
|
||||
var f = declaring.getDeclaredField(name);
|
||||
f.setAccessible(true);
|
||||
return f.get(owner);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
|
||||
Reference in New Issue
Block a user