Search in sources :

Example 6 with EntityTracker

use of com.viaversion.viaversion.api.data.entity.EntityTracker in project ViaVersion by ViaVersion.

the class PlayerPackets method register.

public static void register(Protocol protocol) {
    protocol.registerClientbound(ClientboundPackets1_14.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            // Level Seed
            handler(wrapper -> wrapper.write(Type.LONG, 0L));
        }
    });
    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(wrapper -> {
                // Register Type ID
                EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_15To1_14_4.class);
                int entityId = wrapper.get(Type.INT, 0);
                tracker.addEntity(entityId, Entity1_15Types.PLAYER);
            });
            // Level Seed
            handler(wrapper -> wrapper.write(Type.LONG, 0L));
            // 3 - Max Players
            map(Type.UNSIGNED_BYTE);
            // 4 - Level Type
            map(Type.STRING);
            // 5 - View Distance
            map(Type.VAR_INT);
            // 6 - Reduce Debug Info
            map(Type.BOOLEAN);
            // Show Death Screen
            handler(wrapper -> wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn()));
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) Protocol1_15To1_14_4(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) Protocol(com.viaversion.viaversion.api.protocol.Protocol) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Via(com.viaversion.viaversion.api.Via) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_15To1_14_4(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4)

Example 7 with EntityTracker

use of com.viaversion.viaversion.api.data.entity.EntityTracker in project ViaVersion by ViaVersion.

the class SpawnPackets method register.

public static void register(Protocol1_9To1_8 protocol) {
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    // 1 - UUID
                    wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
                }
            });
            // 2 - Type
            map(Type.BYTE);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    int typeID = wrapper.get(Type.BYTE, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, true));
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 3 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 5 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            // Create last 3 shorts
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Data (1st Integer)
                    int data = wrapper.get(Type.INT, 0);
                    short vX = 0, vY = 0, vZ = 0;
                    if (data > 0) {
                        vX = wrapper.read(Type.SHORT);
                        vY = wrapper.read(Type.SHORT);
                        vZ = wrapper.read(Type.SHORT);
                    }
                    wrapper.write(Type.SHORT, vX);
                    wrapper.write(Type.SHORT, vY);
                    wrapper.write(Type.SHORT, vZ);
                }
            });
            // Handle potions
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    final int entityID = wrapper.get(Type.VAR_INT, 0);
                    // Data
                    final int data = wrapper.get(Type.INT, 0);
                    int typeID = wrapper.get(Type.BYTE, 0);
                    if (Entity1_10Types.getTypeFromId(typeID, true) == Entity1_10Types.EntityType.SPLASH_POTION) {
                        // Convert this to meta data, woo!
                        PacketWrapper metaPacket = wrapper.create(ClientboundPackets1_9.ENTITY_METADATA, new PacketHandler() {

                            @Override
                            public void handle(PacketWrapper wrapper) throws Exception {
                                wrapper.write(Type.VAR_INT, entityID);
                                List<Metadata> meta = new ArrayList<>();
                                // Potion
                                Item item = new DataItem(373, (byte) 1, (short) data, null);
                                // Rewrite so that it gets the right nbt
                                ItemRewriter.toClient(item);
                                // TEMP FIX FOR POTIONS UNTIL WE FIGURE OUT HOW TO TRANSFORM SENT PACKETS
                                Metadata potion = new Metadata(5, MetaType1_9.Slot, item);
                                meta.add(potion);
                                wrapper.write(Types1_9.METADATA_LIST, meta);
                            }
                        });
                        // Fix packet order
                        wrapper.send(Protocol1_9To1_8.class);
                        metaPacket.send(Protocol1_9To1_8.class);
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_EXPERIENCE_ORB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 1 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 2 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 3 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Amount to spawn
            map(Type.SHORT);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_GLOBAL_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Type
            map(Type.BYTE);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Currently only lightning uses this
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.LIGHTNING);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 2 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 3 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    // 1 - UUID
                    wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
                }
            });
            // 2 - Type
            map(Type.UNSIGNED_BYTE);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    int typeID = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, false));
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 3 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 5 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 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);
            map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.hasEntity(entityId)) {
                        protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
                    } else {
                        Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId);
                        metadataList.clear();
                    }
                }
            });
            // Handler for meta data
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.handleMetadata(entityID, metadataList);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.PAINTING);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    // 1 - UUID
                    wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
                }
            });
            // 2 - Title
            map(Type.STRING);
            // 3 - Position
            map(Type.POSITION);
            // 4 - Direction
            map(Type.BYTE);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.PLAYER);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 2 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 3 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            handler(new // Handle discontinued player hand item
            PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    short item = wrapper.read(Type.SHORT);
                    if (item != 0) {
                        PacketWrapper packet = PacketWrapper.create(ClientboundPackets1_9.ENTITY_EQUIPMENT, null, wrapper.user());
                        packet.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
                        packet.write(Type.VAR_INT, 0);
                        packet.write(Type.ITEM, new DataItem(item, (byte) 1, (short) 0, null));
                        try {
                            packet.send(Protocol1_9To1_8.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.hasEntity(entityId)) {
                        protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
                    } else {
                        Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId);
                        metadataList.clear();
                    }
                }
            });
            // Handler for meta data
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.handleMetadata(entityID, metadataList);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.DESTROY_ENTITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entities to destroy
            map(Type.VAR_INT_ARRAY_PRIMITIVE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int[] entities = wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0);
                    EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    for (int entity : entities) {
                        // EntityTracker
                        tracker.removeEntity(entity);
                    }
                }
            });
        }
    });
}
Also used : EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) List(java.util.List) MetadataRewriter1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8)

Example 8 with EntityTracker

use of com.viaversion.viaversion.api.data.entity.EntityTracker in project ViaVersion by ViaVersion.

the class EntityPackets method registerPackets.

@Override
public void registerPackets() {
    registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_17Types.FALLING_BLOCK);
    registerTracker(ClientboundPackets1_16_2.SPAWN_MOB);
    registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_17Types.PLAYER);
    registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_17.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16_2.DESTROY_ENTITIES, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
                wrapper.cancel();
                EntityTracker entityTracker = wrapper.user().getEntityTracker(Protocol1_17To1_16_4.class);
                for (int entityId : entityIds) {
                    entityTracker.removeEntity(entityId);
                    // Send individual remove packets
                    PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY);
                    newPacket.write(Type.VAR_INT, entityId);
                    newPacket.send(Protocol1_17To1_16_4.class);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity id
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Collection length is now a var int
                wrapper.write(Type.VAR_INT, wrapper.read(Type.INT));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.BYTE);
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Dismount vehicle
                wrapper.write(Type.BOOLEAN, false);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.COMBAT_EVENT, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Combat packet actions have been split into individual packets (the content hasn't changed)
                int type = wrapper.read(Type.VAR_INT);
                ClientboundPacketType packetType;
                switch(type) {
                    case 0:
                        packetType = ClientboundPackets1_17.COMBAT_ENTER;
                        break;
                    case 1:
                        packetType = ClientboundPackets1_17.COMBAT_END;
                        break;
                    case 2:
                        packetType = ClientboundPackets1_17.COMBAT_KILL;
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid combat type received: " + type);
                }
                wrapper.setId(packetType.getId());
            });
        }
    });
    // The parent class of the other entity move packets that is never actually used has finally been removed from the id list
    protocol.cancelClientbound(ClientboundPackets1_16_2.ENTITY_MOVEMENT);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Types1_17(com.viaversion.viaversion.api.type.types.version.Types1_17) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) Type(com.viaversion.viaversion.api.type.Type) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)

Example 9 with EntityTracker

use of com.viaversion.viaversion.api.data.entity.EntityTracker in project ViaVersion by ViaVersion.

the class EntityPackets method registerPackets.

@Override
public void registerPackets() {
    registerMetadataRewriter(ClientboundPackets1_17_1.ENTITY_METADATA, Types1_17.METADATA_LIST, Types1_18.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_17_1.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Hardcore
            map(Type.BOOLEAN);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous Gamemode
            map(Type.BYTE);
            // World List
            map(Type.STRING_ARRAY);
            // Registry
            map(Type.NBT);
            // Current dimension data
            map(Type.NBT);
            // World
            map(Type.STRING);
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.VAR_INT);
            handler(wrapper -> {
                int chunkRadius = wrapper.passthrough(Type.VAR_INT);
                // Simulation distance
                wrapper.write(Type.VAR_INT, chunkRadius);
            });
            handler(worldDataTrackerHandler(1));
            handler(biomeSizeTracker());
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17_1.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Current dimension data
            map(Type.NBT);
            // World
            map(Type.STRING);
            handler(wrapper -> {
                final String world = wrapper.get(Type.STRING, 0);
                final EntityTracker tracker = tracker(wrapper.user());
                if (!world.equals(tracker.currentWorld())) {
                    wrapper.user().get(ChunkLightStorage.class).clear();
                }
            });
            handler(worldDataTrackerHandler(0));
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) Type(com.viaversion.viaversion.api.type.Type) Particle(com.viaversion.viaversion.api.type.types.Particle) Types1_18(com.viaversion.viaversion.api.type.types.version.Types1_18) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) ChunkLightStorage(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.storage.ChunkLightStorage) ClientboundPackets1_17_1(com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Types1_17(com.viaversion.viaversion.api.type.types.version.Types1_17) Protocol1_18To1_17_1(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.Protocol1_18To1_17_1) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

Example 10 with EntityTracker

use of com.viaversion.viaversion.api.data.entity.EntityTracker in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(final Protocol1_18To1_17_1 protocol) {
    protocol.registerClientbound(ClientboundPackets1_17_1.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                final short id = wrapper.read(Type.UNSIGNED_BYTE);
                final int newId = BlockEntityIds.newId(id);
                wrapper.write(Type.VAR_INT, newId);
                handleSpawners(newId, wrapper.passthrough(Type.NBT));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17_1.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final int chunkX = wrapper.passthrough(Type.VAR_INT);
                final int chunkZ = wrapper.passthrough(Type.VAR_INT);
                if (wrapper.user().get(ChunkLightStorage.class).isLoaded(chunkX, chunkZ)) {
                    if (!Via.getConfig().cache1_17Light()) {
                        // Light packets updating already sent chunks are the same as before
                        return;
                    }
                // Pass through and cache light data
                } else {
                    // Cancel and cache the light data
                    wrapper.cancel();
                }
                final boolean trustEdges = wrapper.passthrough(Type.BOOLEAN);
                final long[] skyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final long[] blockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final long[] emptySkyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final long[] emptyBlockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
                final int skyLightLenght = wrapper.passthrough(Type.VAR_INT);
                final byte[][] skyLight = new byte[skyLightLenght][];
                for (int i = 0; i < skyLightLenght; i++) {
                    skyLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
                }
                final int blockLightLength = wrapper.passthrough(Type.VAR_INT);
                final byte[][] blockLight = new byte[blockLightLength][];
                for (int i = 0; i < blockLightLength; i++) {
                    blockLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
                }
                final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
                lightStorage.storeLight(chunkX, chunkZ, new ChunkLightStorage.ChunkLight(trustEdges, skyLightMask, blockLightMask, emptySkyLightMask, emptyBlockLightMask, skyLight, blockLight));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17_1.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
                final Chunk oldChunk = wrapper.read(new Chunk1_17Type(tracker.currentWorldSectionHeight()));
                final List<BlockEntity> blockEntities = new ArrayList<>(oldChunk.getBlockEntities().size());
                for (final CompoundTag tag : oldChunk.getBlockEntities()) {
                    final NumberTag xTag = tag.get("x");
                    final NumberTag yTag = tag.get("y");
                    final NumberTag zTag = tag.get("z");
                    final StringTag idTag = tag.get("id");
                    if (xTag == null || yTag == null || zTag == null || idTag == null) {
                        continue;
                    }
                    final String id = idTag.getValue();
                    final int typeId = protocol.getMappingData().blockEntityIds().getInt(id.replace("minecraft:", ""));
                    if (typeId == -1) {
                        Via.getPlatform().getLogger().warning("Unknown block entity: " + id);
                    }
                    handleSpawners(typeId, tag);
                    final byte packedXZ = (byte) ((xTag.asInt() & 15) << 4 | (zTag.asInt() & 15));
                    blockEntities.add(new BlockEntityImpl(packedXZ, yTag.asShort(), typeId, tag));
                }
                final int[] biomeData = oldChunk.getBiomeData();
                final ChunkSection[] sections = oldChunk.getSections();
                for (int i = 0; i < sections.length; i++) {
                    ChunkSection section = sections[i];
                    if (section == null) {
                        // There's no section mask anymore
                        section = new ChunkSectionImpl();
                        sections[i] = section;
                        section.setNonAirBlocksCount(0);
                        final DataPaletteImpl blockPalette = new DataPaletteImpl(ChunkSection.SIZE);
                        blockPalette.addId(0);
                        section.addPalette(PaletteType.BLOCKS, blockPalette);
                    }
                    // Fill biome palette
                    final DataPaletteImpl biomePalette = new DataPaletteImpl(ChunkSection.BIOME_SIZE);
                    section.addPalette(PaletteType.BIOMES, biomePalette);
                    final int offset = i * ChunkSection.BIOME_SIZE;
                    for (int biomeIndex = 0, biomeArrayIndex = offset; biomeIndex < ChunkSection.BIOME_SIZE; biomeIndex++, biomeArrayIndex++) {
                        // Also catch invalid biomes with id -1
                        final int biome = biomeData[biomeArrayIndex];
                        biomePalette.setIdAt(biomeIndex, biome != -1 ? biome : 0);
                    }
                }
                final Chunk chunk = new Chunk1_18(oldChunk.getX(), oldChunk.getZ(), sections, oldChunk.getHeightMap(), blockEntities);
                wrapper.write(new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), MathUtil.ceilLog2(tracker.biomesSent())), chunk);
                final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
                final boolean alreadyLoaded = !lightStorage.addLoadedChunk(chunk.getX(), chunk.getZ());
                // Append light data to chunk packet
                final ChunkLightStorage.ChunkLight light = Via.getConfig().cache1_17Light() ? lightStorage.getLight(chunk.getX(), chunk.getZ()) : lightStorage.removeLight(chunk.getX(), chunk.getZ());
                if (light == null) {
                    Via.getPlatform().getLogger().warning("No light data found for chunk at " + chunk.getX() + ", " + chunk.getZ() + ". Chunk was already loaded: " + alreadyLoaded);
                    final BitSet emptyLightMask = new BitSet();
                    emptyLightMask.set(0, tracker.currentWorldSectionHeight() + 2);
                    wrapper.write(Type.BOOLEAN, false);
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
                    wrapper.write(Type.VAR_INT, 0);
                    wrapper.write(Type.VAR_INT, 0);
                } else {
                    wrapper.write(Type.BOOLEAN, light.trustEdges());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.skyLightMask());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.blockLightMask());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptySkyLightMask());
                    wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptyBlockLightMask());
                    wrapper.write(Type.VAR_INT, light.skyLight().length);
                    for (final byte[] skyLight : light.skyLight()) {
                        wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, skyLight);
                    }
                    wrapper.write(Type.VAR_INT, light.blockLight().length);
                    for (final byte[] blockLight : light.blockLight()) {
                        wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, blockLight);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_17_1.UNLOAD_CHUNK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final int chunkX = wrapper.passthrough(Type.INT);
                final int chunkZ = wrapper.passthrough(Type.INT);
                wrapper.user().get(ChunkLightStorage.class).clear(chunkX, chunkZ);
            });
        }
    });
}
Also used : BlockEntityImpl(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl) MathUtil(com.viaversion.viaversion.util.MathUtil) ChunkLightStorage(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.storage.ChunkLightStorage) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) Via(com.viaversion.viaversion.api.Via) Protocol1_18To1_17_1(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.Protocol1_18To1_17_1) com.viaversion.viaversion.api.minecraft.chunks(com.viaversion.viaversion.api.minecraft.chunks) ArrayList(java.util.ArrayList) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) List(java.util.List) ClientboundPackets1_17_1(com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) BitSet(java.util.BitSet) BlockEntityIds(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.BlockEntityIds) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) BlockEntity(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) BitSet(java.util.BitSet) BlockEntityImpl(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntityImpl) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) ChunkLightStorage(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.storage.ChunkLightStorage) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) ArrayList(java.util.ArrayList) List(java.util.List) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Aggregations

EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)11 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)8 Type (com.viaversion.viaversion.api.type.Type)7 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)3 ClientboundPackets1_17_1 (com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1)3 Chunk1_17Type (com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type)3 MathUtil (com.viaversion.viaversion.util.MathUtil)3 BitSet (java.util.BitSet)3 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)2 ItemRewriter (com.viaversion.viabackwards.api.rewriters.ItemRewriter)2 WindowTracker (com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.WindowTracker)2 Via (com.viaversion.viaversion.api.Via)2 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)2 Position (com.viaversion.viaversion.api.minecraft.Position)2 BlockEntity (com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity)2 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)2 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2