use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14 in project ViaVersion by ViaVersion.
the class MetadataRewriter1_14To1_13_2 method handleMetadata.
@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
metadata.setMetaType(Types1_14.META_TYPES.byId(metadata.metaType().typeId()));
EntityTracker1_14 tracker = tracker(connection);
if (metadata.metaType() == Types1_14.META_TYPES.itemType) {
protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
} else if (metadata.metaType() == Types1_14.META_TYPES.blockStateType) {
// Convert to new block id
int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));
} else if (metadata.metaType() == Types1_14.META_TYPES.particleType) {
rewriteParticle((Particle) metadata.getValue());
}
if (type == null)
return;
// Metadata 6 added to abstract_entity
if (metadata.id() > 5) {
metadata.setId(metadata.id() + 1);
}
if (metadata.id() == 8 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) {
final float v = ((Number) metadata.getValue()).floatValue();
if (Float.isNaN(v) && Via.getConfig().is1_14HealthNaNFix()) {
metadata.setValue(1F);
}
}
// Metadata 12 added to living_entity
if (metadata.id() > 11 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) {
metadata.setId(metadata.id() + 1);
}
if (type.isOrHasParent(Entity1_14Types.ABSTRACT_INSENTIENT)) {
if (metadata.id() == 13) {
tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) | // New attacking metadata
(tracker.getInsentientData(entityId) & 0x4)));
metadata.setValue(tracker.getInsentientData(entityId));
}
}
if (type.isOrHasParent(Entity1_14Types.PLAYER)) {
if (entityId != tracker.clientEntityId()) {
if (metadata.id() == 0) {
byte flags = ((Number) metadata.getValue()).byteValue();
// Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize
tracker.setEntityFlags(entityId, flags);
} else if (metadata.id() == 7) {
tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0);
}
if (metadata.id() == 0 || metadata.id() == 7) {
metadatas.add(new Metadata(6, Types1_14.META_TYPES.poseType, recalculatePlayerPose(entityId, tracker)));
}
}
} else if (type.isOrHasParent(Entity1_14Types.ZOMBIE)) {
if (metadata.id() == 16) {
tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
((boolean) metadata.getValue() ? 0x4 : 0)));
// "Are hands held up"
metadatas.remove(metadata);
metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
} else if (metadata.id() > 16) {
metadata.setId(metadata.id() - 1);
}
}
if (type.isOrHasParent(Entity1_14Types.MINECART_ABSTRACT)) {
if (metadata.id() == 10) {
// New block format
int data = (int) metadata.getValue();
metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));
}
} else if (type.is(Entity1_14Types.HORSE)) {
if (metadata.id() == 18) {
metadatas.remove(metadata);
int armorType = (int) metadata.getValue();
Item armorItem = null;
if (armorType == 1) {
// iron armor
armorItem = new DataItem(protocol.getMappingData().getNewItemId(727), (byte) 1, (short) 0, null);
} else if (armorType == 2) {
// gold armor
armorItem = new DataItem(protocol.getMappingData().getNewItemId(728), (byte) 1, (short) 0, null);
} else if (armorType == 3) {
// diamond armor
armorItem = new DataItem(protocol.getMappingData().getNewItemId(729), (byte) 1, (short) 0, null);
}
PacketWrapper equipmentPacket = PacketWrapper.create(ClientboundPackets1_14.ENTITY_EQUIPMENT, null, connection);
equipmentPacket.write(Type.VAR_INT, entityId);
equipmentPacket.write(Type.VAR_INT, 4);
equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, armorItem);
equipmentPacket.scheduleSend(Protocol1_14To1_13_2.class);
}
} else if (type.is(Entity1_14Types.VILLAGER)) {
if (metadata.id() == 15) {
// plains
metadata.setTypeAndValue(Types1_14.META_TYPES.villagerDatatType, new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0));
}
} else if (type.is(Entity1_14Types.ZOMBIE_VILLAGER)) {
if (metadata.id() == 18) {
// plains
metadata.setTypeAndValue(Types1_14.META_TYPES.villagerDatatType, new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0));
}
} else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
if (metadata.id() >= 9) {
// New piercing
metadata.setId(metadata.id() + 1);
}
} else if (type.is(Entity1_14Types.FIREWORK_ROCKET)) {
if (metadata.id() == 8) {
metadata.setMetaType(Types1_14.META_TYPES.optionalVarIntType);
if (metadata.getValue().equals(0)) {
// https://bugs.mojang.com/browse/MC-111480
metadata.setValue(null);
}
}
} else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_SKELETON)) {
if (metadata.id() == 14) {
tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
((boolean) metadata.getValue() ? 0x4 : 0)));
// "Is swinging arms"
metadatas.remove(metadata);
metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
}
}
if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) {
if (metadata.id() == 14) {
tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
(((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0)));
// "Has target (aggressive state)"
metadatas.remove(metadata);
metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
}
}
if (type.is(Entity1_14Types.WITCH) || type.is(Entity1_14Types.RAVAGER) || type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) {
if (metadata.id() >= 14) {
// 19w13 added a new boolean (raid participant - is celebrating) with id 14
metadata.setId(metadata.id() + 1);
}
}
}
use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14 in project ViaVersion by ViaVersion.
the class InventoryPackets method registerPackets.
@Override
public void registerPackets() {
registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Short windowId = wrapper.read(Type.UNSIGNED_BYTE);
String type = wrapper.read(Type.STRING);
JsonElement title = wrapper.read(Type.COMPONENT);
COMPONENT_REWRITER.processText(title);
Short slots = wrapper.read(Type.UNSIGNED_BYTE);
if (type.equals("EntityHorse")) {
wrapper.setId(0x1F);
int entityId = wrapper.read(Type.INT);
wrapper.write(Type.UNSIGNED_BYTE, windowId);
wrapper.write(Type.VAR_INT, slots.intValue());
wrapper.write(Type.INT, entityId);
} else {
wrapper.setId(0x2E);
wrapper.write(Type.VAR_INT, windowId.intValue());
int typeId = -1;
switch(type) {
case "minecraft:crafting_table":
typeId = 11;
break;
case "minecraft:furnace":
typeId = 13;
break;
case "minecraft:dropper":
case "minecraft:dispenser":
typeId = 6;
break;
case "minecraft:enchanting_table":
typeId = 12;
break;
case "minecraft:brewing_stand":
typeId = 10;
break;
case "minecraft:villager":
typeId = 18;
break;
case "minecraft:beacon":
typeId = 8;
break;
case "minecraft:anvil":
typeId = 7;
break;
case "minecraft:hopper":
typeId = 15;
break;
case "minecraft:shulker_box":
typeId = 19;
break;
case "minecraft:container":
case "minecraft:chest":
default:
if (slots > 0 && slots <= 54) {
typeId = slots / 9 - 1;
}
break;
}
if (typeId == -1) {
Via.getPlatform().getLogger().warning("Can't open inventory for 1.14 player! Type: " + type + " Size: " + slots);
}
wrapper.write(Type.VAR_INT, typeId);
wrapper.write(Type.COMPONENT, title);
}
}
});
}
});
registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
// Channel
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String channel = wrapper.get(Type.STRING, 0);
if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) {
wrapper.setId(0x27);
wrapper.resetReader();
// Remove channel
wrapper.read(Type.STRING);
int windowId = wrapper.read(Type.INT);
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
tracker.setLatestTradeWindowId(windowId);
wrapper.write(Type.VAR_INT, windowId);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Output Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Has second item
boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
if (secondItem) {
// Second Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
// Trade disabled
wrapper.passthrough(Type.BOOLEAN);
// Number of tools uses
wrapper.passthrough(Type.INT);
// Maximum number of trade uses
wrapper.passthrough(Type.INT);
wrapper.write(Type.INT, 0);
wrapper.write(Type.INT, 0);
wrapper.write(Type.FLOAT, 0f);
}
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.BOOLEAN, false);
} else if (channel.equals("minecraft:book_open") || channel.equals("book_open")) {
int hand = wrapper.read(Type.VAR_INT);
wrapper.clearPacket();
wrapper.setId(0x2D);
wrapper.write(Type.VAR_INT, hand);
}
}
});
}
});
registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int size = wrapper.passthrough(Type.VAR_INT);
int deleted = 0;
for (int i = 0; i < size; i++) {
// Recipe Identifier
String id = wrapper.read(Type.STRING);
String type = wrapper.read(Type.STRING);
if (REMOVED_RECIPE_TYPES.contains(type)) {
deleted++;
continue;
}
wrapper.write(Type.STRING, type);
wrapper.write(Type.STRING, id);
recipeRewriter.handle(wrapper, type);
}
wrapper.set(Type.VAR_INT, 0, size - deleted);
});
}
});
registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Selecting trade now moves the items, we need to resync the inventory
PacketWrapper resyncPacket = wrapper.create(0x08);
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
// 0 - Window ID
resyncPacket.write(Type.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId()));
// 1 - Slot
resyncPacket.write(Type.SHORT, ((short) -999));
// 2 - Button - End left click
resyncPacket.write(Type.BYTE, (byte) 2);
// 3 - Action number
resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt()));
// 4 - Mode - Drag
resyncPacket.write(Type.VAR_INT, 5);
CompoundTag tag = new CompoundTag();
// Tags with NaN are not equal
tag.put("force_resync", new DoubleTag(Double.NaN));
// 5 - Clicked Item
resyncPacket.write(Type.FLAT_VAR_INT_ITEM, new DataItem(1, (byte) 1, (short) 0, tag));
resyncPacket.scheduleSendToServer(Protocol1_14To1_13_2.class);
}
});
}
});
registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
}
use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14 in project ViaVersion by ViaVersion.
the class EntityPackets method register.
public static void register(Protocol1_14To1_13_2 protocol) {
MetadataRewriter1_14To1_13_2 metadataRewriter = protocol.get(MetadataRewriter1_14To1_13_2.class);
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_ENTITY, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity id
map(Type.VAR_INT);
// 1 - UUID
map(Type.UUID);
// 2 - Type
map(Type.BYTE, Type.VAR_INT);
// 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);
// Track Entity
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
int typeId = wrapper.get(Type.VAR_INT, 1);
Entity1_13Types.EntityType type1_13 = Entity1_13Types.getTypeFromId(typeId, true);
typeId = metadataRewriter.newEntityId(type1_13.getId());
EntityType type1_14 = Entity1_14Types.getTypeFromId(typeId);
if (type1_14 != null) {
int data = wrapper.get(Type.INT, 0);
if (type1_14.is(Entity1_14Types.FALLING_BLOCK)) {
wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (type1_14.is(Entity1_14Types.MINECART)) {
// default is 0 = rideable minecart
switch(data) {
case 1:
typeId = Entity1_14Types.CHEST_MINECART.getId();
break;
case 2:
typeId = Entity1_14Types.FURNACE_MINECART.getId();
break;
case 3:
typeId = Entity1_14Types.TNT_MINECART.getId();
break;
case 4:
typeId = Entity1_14Types.SPAWNER_MINECART.getId();
break;
case 5:
typeId = Entity1_14Types.HOPPER_MINECART.getId();
break;
case 6:
typeId = Entity1_14Types.COMMAND_BLOCK_MINECART.getId();
break;
}
} else if ((type1_14.is(Entity1_14Types.ITEM) && data > 0) || type1_14.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
if (type1_14.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
wrapper.set(Type.INT, 0, data - 1);
}
// send velocity in separate packet, 1.14 is now ignoring the velocity
PacketWrapper velocity = wrapper.create(0x45);
velocity.write(Type.VAR_INT, entityId);
velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 0));
velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 1));
velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 2));
velocity.scheduleSend(Protocol1_14To1_13_2.class);
}
// Register Type ID
wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class).addEntity(entityId, type1_14);
}
wrapper.set(Type.VAR_INT, 1, typeId);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.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_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST));
}
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PAINTING, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.UUID);
map(Type.VAR_INT);
map(Type.POSITION, Type.POSITION1_14);
map(Type.BYTE);
}
});
protocol.registerClientbound(ClientboundPackets1_13.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_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST, Entity1_14Types.PLAYER));
}
});
protocol.registerClientbound(ClientboundPackets1_13.ENTITY_ANIMATION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
short animation = wrapper.passthrough(Type.UNSIGNED_BYTE);
if (animation == 2) {
// Leave bed
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
int entityId = wrapper.get(Type.VAR_INT, 0);
tracker.setSleeping(entityId, false);
PacketWrapper metadataPacket = wrapper.create(ClientboundPackets1_14.ENTITY_METADATA);
metadataPacket.write(Type.VAR_INT, entityId);
List<Metadata> metadataList = new LinkedList<>();
if (tracker.clientEntityId() != entityId) {
metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
}
metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, null));
metadataPacket.write(Types1_14.METADATA_LIST, metadataList);
metadataPacket.scheduleSend(Protocol1_14To1_13_2.class);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.USE_BED, ClientboundPackets1_14.ENTITY_METADATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
int entityId = wrapper.get(Type.VAR_INT, 0);
tracker.setSleeping(entityId, true);
Position position = wrapper.read(Type.POSITION);
List<Metadata> metadataList = new LinkedList<>();
metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, position));
if (tracker.clientEntityId() != entityId) {
metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
}
wrapper.write(Types1_14.METADATA_LIST, metadataList);
}
});
}
});
metadataRewriter.registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES);
metadataRewriter.registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
}
use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14 in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(Protocol1_14To1_13_2 protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, null);
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_BREAK_ANIMATION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.POSITION, Type.POSITION1_14);
map(Type.BYTE);
}
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION, Type.POSITION1_14);
}
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketRemapper() {
@Override
public void registerMap() {
// Location
map(Type.POSITION, Type.POSITION1_14);
// Action id
map(Type.UNSIGNED_BYTE);
// Action param
map(Type.UNSIGNED_BYTE);
// Block id - /!\ NOT BLOCK STATE
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockId(wrapper.get(Type.VAR_INT, 0)));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION, Type.POSITION1_14);
map(Type.VAR_INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.VAR_INT, 0);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id));
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.SERVER_DIFFICULTY, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.UNSIGNED_BYTE);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Added in 19w11a. Maybe https://bugs.mojang.com/browse/MC-44471 ?
wrapper.write(Type.BOOLEAN, false);
}
});
}
});
blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE);
protocol.registerClientbound(ClientboundPackets1_13.EXPLOSION, new PacketRemapper() {
@Override
public void registerMap() {
// X
map(Type.FLOAT);
// Y
map(Type.FLOAT);
// Z
map(Type.FLOAT);
// Radius
map(Type.FLOAT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
for (int i = 0; i < 3; i++) {
float coord = wrapper.get(Type.FLOAT, i);
if (coord < 0f) {
coord = (int) coord;
wrapper.set(Type.FLOAT, i, coord);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk chunk = wrapper.read(new Chunk1_13Type(clientWorld));
wrapper.write(new Chunk1_14Type(), chunk);
int[] motionBlocking = new int[16 * 16];
int[] worldSurface = new int[16 * 16];
for (int s = 0; s < chunk.getSections().length; s++) {
ChunkSection section = chunk.getSections()[s];
if (section == null)
continue;
boolean hasBlock = false;
for (int i = 0; i < section.getPaletteSize(); i++) {
int old = section.getPaletteEntry(i);
int newId = protocol.getMappingData().getNewBlockStateId(old);
if (!hasBlock && newId != air && newId != voidAir && newId != caveAir) {
// air, void_air, cave_air
hasBlock = true;
}
section.setPaletteEntry(i, newId);
}
if (!hasBlock) {
section.setNonAirBlocksCount(0);
continue;
}
int nonAirBlockCount = 0;
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
int id = section.getFlatBlock(x, y, z);
if (id != air && id != voidAir && id != caveAir) {
nonAirBlockCount++;
// +1 (top of the block)
worldSurface[x + z * 16] = y + s * 16 + 1;
}
if (protocol.getMappingData().getMotionBlocking().contains(id)) {
// +1 (top of the block)
motionBlocking[x + z * 16] = y + s * 16 + 1;
}
// Manually update light for non full blocks (block light must not be sent)
if (Via.getConfig().isNonFullBlockLightFix() && protocol.getMappingData().getNonFullBlocks().contains(id)) {
setNonFullLight(chunk, section, s, x, y, z);
}
}
}
}
section.setNonAirBlocksCount(nonAirBlockCount);
}
CompoundTag heightMap = new CompoundTag();
heightMap.put("MOTION_BLOCKING", new LongArrayTag(encodeHeightMap(motionBlocking)));
heightMap.put("WORLD_SURFACE", new LongArrayTag(encodeHeightMap(worldSurface)));
chunk.setHeightMap(heightMap);
PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_14.UPDATE_LIGHT);
lightPacket.write(Type.VAR_INT, chunk.getX());
lightPacket.write(Type.VAR_INT, chunk.getZ());
// all 18 bits set if ground up
int skyLightMask = chunk.isFullChunk() ? 0x3ffff : 0;
int blockLightMask = 0;
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection sec = chunk.getSections()[i];
if (sec == null)
continue;
if (!chunk.isFullChunk() && sec.getLight().hasSkyLight()) {
skyLightMask |= (1 << (i + 1));
}
blockLightMask |= (1 << (i + 1));
}
lightPacket.write(Type.VAR_INT, skyLightMask);
lightPacket.write(Type.VAR_INT, blockLightMask);
// empty sky light mask
lightPacket.write(Type.VAR_INT, 0);
// empty block light mask
lightPacket.write(Type.VAR_INT, 0);
// only do this on the initial chunk send (not when chunk.isGroundUp() is false)
if (chunk.isFullChunk())
// chunk below 0
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT);
for (ChunkSection section : chunk.getSections()) {
if (section == null || !section.getLight().hasSkyLight()) {
if (chunk.isFullChunk()) {
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT);
}
continue;
}
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, section.getLight().getSkyLight());
}
if (chunk.isFullChunk())
// chunk above 255
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT);
for (ChunkSection section : chunk.getSections()) {
if (section == null)
continue;
lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, section.getLight().getBlockLight());
}
EntityTracker1_14 entityTracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
int diffX = Math.abs(entityTracker.getChunkCenterX() - chunk.getX());
int diffZ = Math.abs(entityTracker.getChunkCenterZ() - chunk.getZ());
if (entityTracker.isForceSendCenterChunk() || diffX >= SERVERSIDE_VIEW_DISTANCE || diffZ >= SERVERSIDE_VIEW_DISTANCE) {
// Set center chunk
PacketWrapper fakePosLook = wrapper.create(ClientboundPackets1_14.UPDATE_VIEW_POSITION);
fakePosLook.write(Type.VAR_INT, chunk.getX());
fakePosLook.write(Type.VAR_INT, chunk.getZ());
fakePosLook.send(Protocol1_14To1_13_2.class);
entityTracker.setChunkCenterX(chunk.getX());
entityTracker.setChunkCenterZ(chunk.getZ());
}
lightPacket.send(Protocol1_14To1_13_2.class);
// Remove light references from chunk sections
for (ChunkSection section : chunk.getSections()) {
if (section != null) {
section.setLight(null);
}
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Effect Id
map(Type.INT);
// Location
map(Type.POSITION, Type.POSITION1_14);
// Data
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) {
// Play record
wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data));
} else if (id == 2001) {
// Block break + block break sound
wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.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(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Store the player
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
int entityId = wrapper.get(Type.INT, 0);
Entity1_14Types entType = Entity1_14Types.PLAYER;
// Register Type ID
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
tracker.addEntity(entityId, entType);
tracker.setClientEntityId(entityId);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// 19w11a removed difficulty from join game
short difficulty = wrapper.read(Type.UNSIGNED_BYTE);
PacketWrapper difficultyPacket = wrapper.create(ClientboundPackets1_14.SERVER_DIFFICULTY);
difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty);
// Unknown value added in 19w11a
difficultyPacket.write(Type.BOOLEAN, false);
difficultyPacket.scheduleSend(protocol.getClass());
// Max Players
wrapper.passthrough(Type.UNSIGNED_BYTE);
// Level Type
wrapper.passthrough(Type.STRING);
// Serverside view distance, added in 19w13a
wrapper.write(Type.VAR_INT, SERVERSIDE_VIEW_DISTANCE);
}
});
handler(wrapper -> {
// Manually send the packet
wrapper.send(Protocol1_14To1_13_2.class);
wrapper.cancel();
// View distance has to be sent after the join packet
sendViewDistancePacket(wrapper.user());
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.MAP_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.VAR_INT);
map(Type.BYTE);
map(Type.BOOLEAN);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// new value, probably if the map is locked (added in 19w02a), old maps are not locked
wrapper.write(Type.BOOLEAN, false);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Dimension ID
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
EntityTracker1_14 entityTracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
// The client may reset the center chunk if dimension is changed
entityTracker.setForceSendCenterChunk(true);
}
});
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// 19w11a removed difficulty from respawn
short difficulty = wrapper.read(Type.UNSIGNED_BYTE);
PacketWrapper difficultyPacket = wrapper.create(ClientboundPackets1_14.SERVER_DIFFICULTY);
difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty);
// Unknown value added in 19w11a
difficultyPacket.write(Type.BOOLEAN, false);
difficultyPacket.scheduleSend(protocol.getClass());
}
});
handler(wrapper -> {
// Manually send the packet and update the viewdistance after
wrapper.send(Protocol1_14To1_13_2.class);
wrapper.cancel();
sendViewDistancePacket(wrapper.user());
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_POSITION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION, Type.POSITION1_14);
}
});
}
Aggregations