use of com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler in project ViaBackwards by ViaVersion.
the class EntityPackets1_14 method registerPackets.
@Override
protected void registerPackets() {
positionHandler = new EntityPositionHandler(this, EntityPositionStorage1_14.class, EntityPositionStorage1_14::new);
protocol.registerClientbound(ClientboundPackets1_14.ENTITY_STATUS, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int entityId = wrapper.passthrough(Type.INT);
byte status = wrapper.passthrough(Type.BYTE);
// Check for death status
if (status != 3)
return;
EntityTracker tracker = tracker(wrapper.user());
EntityType entityType = tracker.entityType(entityId);
if (entityType != Entity1_14Types.PLAYER)
return;
// Remove equipment, else the client will see ghost items
for (int i = 0; i <= 5; i++) {
PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_13.ENTITY_EQUIPMENT);
equipmentPacket.write(Type.VAR_INT, entityId);
equipmentPacket.write(Type.VAR_INT, i);
equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, null);
equipmentPacket.send(Protocol1_13_2To1_14.class);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.ENTITY_TELEPORT, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
handler(wrapper -> positionHandler.cacheEntityPosition(wrapper, false, false));
}
});
PacketRemapper relativeMoveHandler = new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.SHORT);
map(Type.SHORT);
map(Type.SHORT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
double x = wrapper.get(Type.SHORT, 0) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
double y = wrapper.get(Type.SHORT, 1) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
double z = wrapper.get(Type.SHORT, 2) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
positionHandler.cacheEntityPosition(wrapper, x, y, z, false, true);
}
});
}
};
protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION, relativeMoveHandler);
protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION_AND_ROTATION, relativeMoveHandler);
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_ENTITY, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity id
map(Type.VAR_INT);
// 1 - UUID
map(Type.UUID);
// 2 - Type
map(Type.VAR_INT, Type.BYTE);
// 3 - X
map(Type.DOUBLE);
// 4 - Y
map(Type.DOUBLE);
// 5 - Z
map(Type.DOUBLE);
// 6 - Pitch
map(Type.BYTE);
// 7 - Yaw
map(Type.BYTE);
// 8 - Data
map(Type.INT);
// 9 - Velocity X
map(Type.SHORT);
// 10 - Velocity Y
map(Type.SHORT);
// 11 - Velocity Z
map(Type.SHORT);
handler(getObjectTrackerHandler());
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.BYTE, 0);
int mappedId = newEntityId(id);
Entity1_13Types.EntityType entityType = Entity1_13Types.getTypeFromId(mappedId, false);
Entity1_13Types.ObjectType objectType;
if (entityType.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT)) {
objectType = Entity1_13Types.ObjectType.MINECART;
int data = 0;
switch(entityType) {
case CHEST_MINECART:
data = 1;
break;
case FURNACE_MINECART:
data = 2;
break;
case TNT_MINECART:
data = 3;
break;
case SPAWNER_MINECART:
data = 4;
break;
case HOPPER_MINECART:
data = 5;
break;
case COMMAND_BLOCK_MINECART:
data = 6;
break;
}
if (data != 0)
wrapper.set(Type.INT, 0, data);
} else {
objectType = Entity1_13Types.ObjectType.fromEntityType(entityType).orElse(null);
}
if (objectType == null)
return;
wrapper.set(Type.BYTE, 0, (byte) objectType.getId());
int data = wrapper.get(Type.INT, 0);
if (objectType == Entity1_13Types.ObjectType.FALLING_BLOCK) {
int blockState = wrapper.get(Type.INT, 0);
int combined = protocol.getMappingData().getNewBlockStateId(blockState);
wrapper.set(Type.INT, 0, combined);
} else if (entityType.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW)) {
wrapper.set(Type.INT, 0, data + 1);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_MOB, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Entity UUID
map(Type.UUID);
// 2 - Entity Type
map(Type.VAR_INT);
// 3 - X
map(Type.DOUBLE);
// 4 - Y
map(Type.DOUBLE);
// 5 - Z
map(Type.DOUBLE);
// 6 - Yaw
map(Type.BYTE);
// 7 - Pitch
map(Type.BYTE);
// 8 - Head Pitch
map(Type.BYTE);
// 9 - Velocity X
map(Type.SHORT);
// 10 - Velocity Y
map(Type.SHORT);
// 11 - Velocity Z
map(Type.SHORT);
// 12 - Metadata
map(Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int type = wrapper.get(Type.VAR_INT, 1);
EntityType entityType = Entity1_14Types.getTypeFromId(type);
addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType);
int oldId = newEntityId(type);
if (oldId == -1) {
EntityData entityData = entityDataForType(entityType);
if (entityData == null) {
ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13.2 entity type for 1.14 entity type " + type + "/" + entityType);
wrapper.cancel();
} else {
wrapper.set(Type.VAR_INT, 1, entityData.replacementId());
}
} else {
wrapper.set(Type.VAR_INT, 1, oldId);
}
}
});
// Handle entity type & metadata
handler(getMobSpawnRewriter(Types1_13_2.METADATA_LIST));
}
});
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_EXPERIENCE_ORB, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity id
map(Type.VAR_INT);
// Needs to be mapped for the position cache
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EXPERIENCE_ORB));
}
});
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_GLOBAL_ENTITY, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity id
map(Type.VAR_INT);
map(Type.BYTE);
// Needs to be mapped for the position cache
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.LIGHTNING_BOLT));
}
});
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PAINTING, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.VAR_INT);
map(Type.POSITION1_14, Type.POSITION);
map(Type.BYTE);
// Track entity
handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.PAINTING));
}
});
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PLAYER, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Player UUID
map(Type.UUID);
// 2 - X
map(Type.DOUBLE);
// 3 - Y
map(Type.DOUBLE);
// 4 - Z
map(Type.DOUBLE);
// 5 - Yaw
map(Type.BYTE);
// 6 - Pitch
map(Type.BYTE);
// 7 - Metadata
map(Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
handler(getTrackerAndMetaHandler(Types1_13_2.METADATA_LIST, Entity1_14Types.PLAYER));
handler(wrapper -> positionHandler.cacheEntityPosition(wrapper, true, false));
}
});
registerRemoveEntities(ClientboundPackets1_14.DESTROY_ENTITIES);
registerMetadataRewriter(ClientboundPackets1_14.ENTITY_METADATA, Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
protocol.registerClientbound(ClientboundPackets1_14.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.INT);
// 1 - Gamemode
map(Type.UNSIGNED_BYTE);
// 2 - Dimension
map(Type.INT);
handler(getTrackerHandler(Entity1_14Types.PLAYER, Type.INT));
handler(getDimensionHandler(1));
handler(wrapper -> {
short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
wrapper.write(Type.UNSIGNED_BYTE, difficulty);
// Max Players
wrapper.passthrough(Type.UNSIGNED_BYTE);
// Level Type
wrapper.passthrough(Type.STRING);
// Read View Distance
wrapper.read(Type.VAR_INT);
// TODO Track client position
// Manually add position storage
/*int entitiyId = wrapper.get(Type.INT, 0);
StoredEntityData storedEntity = protocol.getEntityRewriter().tracker(wrapper.user()).entityData(entitiyId);
storedEntity.put(new EntityPositionStorage1_14());*/
});
}
});
protocol.registerClientbound(ClientboundPackets1_14.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Dimension ID
map(Type.INT);
handler(wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
wrapper.write(Type.UNSIGNED_BYTE, difficulty);
wrapper.user().get(ChunkLightStorage.class).clear();
});
}
});
}
Aggregations