Search in sources :

Example 1 with WorldNameTracker

use of com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker in project ViaBackwards by ViaVersion.

the class EntityPackets1_16 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_16.SPAWN_ENTITY, 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 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            handler(wrapper -> {
                EntityType entityType = typeFromId(wrapper.get(Type.VAR_INT, 1));
                if (entityType == Entity1_16Types.LIGHTNING_BOLT) {
                    // Map to old weather entity packet
                    wrapper.cancel();
                    PacketWrapper spawnLightningPacket = wrapper.create(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY);
                    // Entity id
                    spawnLightningPacket.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
                    // Lightning type
                    spawnLightningPacket.write(Type.BYTE, (byte) 1);
                    // X
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 0));
                    // Y
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 1));
                    // Z
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 2));
                    spawnLightningPacket.send(Protocol1_15_2To1_16.class);
                }
            });
            handler(getSpawnTrackerWithDataHandler(Entity1_16Types.FALLING_BLOCK));
        }
    });
    registerSpawnTracker(ClientboundPackets1_16.SPAWN_MOB);
    protocol.registerClientbound(ClientboundPackets1_16.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                // Grab the tracker for world names
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // World Name
                String nextWorldName = wrapper.read(Type.STRING);
                // Seed
                wrapper.passthrough(Type.LONG);
                // Gamemode
                wrapper.passthrough(Type.UNSIGNED_BYTE);
                // Previous gamemode
                wrapper.read(Type.BYTE);
                // Grab client world
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimension = wrapper.get(Type.INT, 0);
                // Send a dummy respawn with a different dimension if the world name was different and the same dimension was used
                if (clientWorld.getEnvironment() != null && dimension == clientWorld.getEnvironment().getId() && (wrapper.user().isClientSide() || Via.getPlatform().isProxy() || // Hotfix for https://github.com/ViaVersion/ViaBackwards/issues/381
                wrapper.user().getProtocolInfo().getProtocolVersion() <= ProtocolVersion.v1_12_2.getVersion() || !nextWorldName.equals(worldNameTracker.getWorldName()))) {
                    PacketWrapper packet = wrapper.create(ClientboundPackets1_15.RESPAWN);
                    packet.write(Type.INT, dimension == 0 ? -1 : 0);
                    packet.write(Type.LONG, 0L);
                    packet.write(Type.UNSIGNED_BYTE, (short) 0);
                    packet.write(Type.STRING, "default");
                    packet.send(Protocol1_15_2To1_16.class);
                }
                clientWorld.setEnvironment(dimension);
                // Level type
                wrapper.write(Type.STRING, "default");
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
                // Keep all playerdata
                wrapper.read(Type.BOOLEAN);
                // Finally update the world name
                worldNameTracker.setWorldName(nextWorldName);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous gamemode
            map(Type.BYTE, Type.NOTHING);
            // World list
            map(Type.STRING_ARRAY, Type.NOTHING);
            // whatever this is
            map(Type.NBT, Type.NOTHING);
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // Save the world name
                worldNameTracker.setWorldName(wrapper.read(Type.STRING));
            });
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                clientChunks.setEnvironment(wrapper.get(Type.INT, 1));
                tracker(wrapper.user()).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER);
                // Level type
                wrapper.write(Type.STRING, "default");
                // View distance
                wrapper.passthrough(Type.VAR_INT);
                // Reduced debug info
                wrapper.passthrough(Type.BOOLEAN);
                // Show death screen
                wrapper.passthrough(Type.BOOLEAN);
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_16.SPAWN_EXPERIENCE_ORB, Entity1_16Types.EXPERIENCE_ORB);
    // F Spawn Global Object, it is no longer with us :(
    registerTracker(ClientboundPackets1_16.SPAWN_PAINTING, Entity1_16Types.PAINTING);
    registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16Types.PLAYER);
    registerRemoveEntities(ClientboundPackets1_16.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_14.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                int size = wrapper.passthrough(Type.INT);
                for (int i = 0; i < size; i++) {
                    String attributeIdentifier = wrapper.read(Type.STRING);
                    String oldKey = protocol.getMappingData().getAttributeMappings().get(attributeIdentifier);
                    wrapper.write(Type.STRING, oldKey != null ? oldKey : attributeIdentifier.replace("minecraft:", ""));
                    wrapper.passthrough(Type.DOUBLE);
                    int modifierSize = wrapper.passthrough(Type.VAR_INT);
                    for (int j = 0; j < modifierSize; j++) {
                        wrapper.passthrough(Type.UUID);
                        wrapper.passthrough(Type.DOUBLE);
                        wrapper.passthrough(Type.BYTE);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(packetWrapper -> {
                int action = packetWrapper.passthrough(Type.VAR_INT);
                int playerCount = packetWrapper.passthrough(Type.VAR_INT);
                for (int i = 0; i < playerCount; i++) {
                    packetWrapper.passthrough(Type.UUID);
                    if (action == 0) {
                        // Add
                        packetWrapper.passthrough(Type.STRING);
                        int properties = packetWrapper.passthrough(Type.VAR_INT);
                        for (int j = 0; j < properties; j++) {
                            packetWrapper.passthrough(Type.STRING);
                            packetWrapper.passthrough(Type.STRING);
                            if (packetWrapper.passthrough(Type.BOOLEAN)) {
                                packetWrapper.passthrough(Type.STRING);
                            }
                        }
                        packetWrapper.passthrough(Type.VAR_INT);
                        packetWrapper.passthrough(Type.VAR_INT);
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display Name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    } else if (action == 1) {
                        // Update Game Mode
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 2) {
                        // Update Ping
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 3) {
                        // Update Display Name
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    }
                // 4 = Remove Player
                }
            });
        }
    });
}
Also used : Particle(com.viaversion.viaversion.api.type.types.Particle) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) WolfDataMaskStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Via(com.viaversion.viaversion.api.Via) Item(com.viaversion.viaversion.api.minecraft.item.Item) ProtocolVersion(com.viaversion.viaversion.api.protocol.version.ProtocolVersion) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) Type(com.viaversion.viaversion.api.type.Type) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)

Example 2 with WorldNameTracker

use of com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker in project ViaBackwards by ViaVersion.

the class Protocol1_15_2To1_16 method init.

@Override
public void init(UserConnection user) {
    if (!user.has(ClientWorld.class)) {
        user.put(new ClientWorld(user));
    }
    user.put(new PlayerSneakStorage());
    user.put(new WorldNameTracker());
    user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, Entity1_16Types.PLAYER, true));
}
Also used : WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) PlayerSneakStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.PlayerSneakStorage) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase)

Aggregations

WorldNameTracker (com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker)2 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)2 EntityRewriter (com.viaversion.viabackwards.api.rewriters.EntityRewriter)1 Protocol1_15_2To1_16 (com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16)1 PlayerSneakStorage (com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.PlayerSneakStorage)1 WolfDataMaskStorage (com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage)1 Via (com.viaversion.viaversion.api.Via)1 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)1 Entity1_15Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types)1 Entity1_16Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types)1 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)1 Item (com.viaversion.viaversion.api.minecraft.item.Item)1 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)1 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)1 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 ValueTransformer (com.viaversion.viaversion.api.protocol.remapper.ValueTransformer)1 ProtocolVersion (com.viaversion.viaversion.api.protocol.version.ProtocolVersion)1 Type (com.viaversion.viaversion.api.type.Type)1 Particle (com.viaversion.viaversion.api.type.types.Particle)1