Search in sources :

Example 1 with EntityTracker1_11

use of com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11 in project ViaVersion by ViaVersion.

the class Protocol1_11To1_10 method registerPackets.

@Override
protected void registerPackets() {
    entityRewriter.register();
    itemRewriter.register();
    registerClientbound(ClientboundPackets1_9_3.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);
            // Track Entity
            handler(entityRewriter.objectTrackerHandler());
        }
    });
    registerClientbound(ClientboundPackets1_9_3.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.UNSIGNED_BYTE, 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_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    // Change Type :)
                    int type = wrapper.get(Type.VAR_INT, 1);
                    Entity1_11Types.EntityType entType = MetadataRewriter1_11To1_10.rewriteEntityType(type, wrapper.get(Types1_9.METADATA_LIST, 0));
                    if (entType != null) {
                        wrapper.set(Type.VAR_INT, 1, entType.getId());
                        // Register Type ID
                        wrapper.user().getEntityTracker(Protocol1_11To1_10.class).addEntity(entityId, entType);
                        entityRewriter.handleMetadata(entityId, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
                    }
                }
            });
        }
    });
    new SoundRewriter(this, this::getNewSoundId).registerSound(ClientboundPackets1_9_3.SOUND);
    registerClientbound(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Collected entity id
            map(Type.VAR_INT);
            // 1 - Collector entity id
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 2 - Pickup Count
                    wrapper.write(Type.VAR_INT, 1);
                }
            });
        }
    });
    entityRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
    registerClientbound(ClientboundPackets1_9_3.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - x
            map(Type.DOUBLE);
            // 2 - y
            map(Type.DOUBLE);
            // 3 - z
            map(Type.DOUBLE);
            // 4 - yaw
            map(Type.BYTE);
            // 5 - pitch
            map(Type.BYTE);
            // 6 - onGround
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    if (Via.getConfig().isHologramPatch()) {
                        EntityTracker1_11 tracker = wrapper.user().getEntityTracker(Protocol1_11To1_10.class);
                        if (tracker.isHologram(entityID)) {
                            Double newValue = wrapper.get(Type.DOUBLE, 1);
                            newValue -= (Via.getConfig().getHologramYOffset());
                            wrapper.set(Type.DOUBLE, 1, newValue);
                        }
                    }
                }
            });
        }
    });
    entityRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 0);
                    // Handle the new ActionBar
                    if (action >= 2) {
                        wrapper.set(Type.VAR_INT, 0, action + 1);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action ID
            map(Type.UNSIGNED_BYTE);
            // 2 - Action Param
            map(Type.UNSIGNED_BYTE);
            // 3 - Block Type
            map(Type.VAR_INT);
            // Cheap hack to ensure it's always right block
            handler(new PacketHandler() {

                @Override
                public void handle(final PacketWrapper actionWrapper) throws Exception {
                    if (Via.getConfig().isPistonAnimationPatch()) {
                        int id = actionWrapper.get(Type.VAR_INT, 0);
                        if (id == 33 || id == 29) {
                            actionWrapper.cancel();
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action
            map(Type.UNSIGNED_BYTE);
            // 2 - NBT data
            map(Type.NBT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    CompoundTag tag = wrapper.get(Type.NBT, 0);
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1)
                        EntityIdRewriter.toClientSpawner(tag);
                    if (tag.contains("id"))
                        // Handle new identifier
                        ((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier((String) tag.get("id").getValue()));
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.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.passthrough(new Chunk1_9_3_4Type(clientWorld));
                    if (chunk.getBlockEntities() == null)
                        return;
                    for (CompoundTag tag : chunk.getBlockEntities()) {
                        if (tag.contains("id")) {
                            String identifier = ((StringTag) tag.get("id")).getValue();
                            if (identifier.equals("MobSpawner")) {
                                EntityIdRewriter.toClientSpawner(tag);
                            }
                            // Handle new identifier
                            ((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier(identifier));
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.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 -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 1);
                clientChunks.setEnvironment(dimensionId);
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 0);
                clientWorld.setEnvironment(dimensionId);
            });
        }
    });
    this.registerClientbound(ClientboundPackets1_9_3.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // effectID
            this.map(Type.INT);
            // pos
            this.map(Type.POSITION);
            // effectData
            this.map(Type.INT);
            // serverwide / global
            this.map(Type.BOOLEAN);
            handler(packetWrapper -> {
                int effectID = packetWrapper.get(Type.INT, 0);
                if (effectID == 2002) {
                    int data = packetWrapper.get(Type.INT, 1);
                    boolean isInstant = false;
                    Pair<Integer, Boolean> newData = PotionColorMapping.getNewData(data);
                    if (newData == null) {
                        Via.getPlatform().getLogger().warning("Received unknown 1.11 -> 1.10.2 potion data (" + data + ")");
                        data = 0;
                    } else {
                        data = newData.key();
                        isInstant = newData.value();
                    }
                    if (isInstant) {
                        packetWrapper.set(Type.INT, 0, 2007);
                    }
                    packetWrapper.set(Type.INT, 1, data);
                }
            });
        }
    });
    /*
            INCOMING PACKETS
        */
    registerServerbound(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Location
            map(Type.POSITION);
            // 1 - Face
            map(Type.VAR_INT);
            // 2 - Hand
            map(Type.VAR_INT);
            map(Type.FLOAT, toOldByte);
            map(Type.FLOAT, toOldByte);
            map(Type.FLOAT, toOldByte);
        }
    });
    registerServerbound(ServerboundPackets1_9_3.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Message
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 100 character limit on older servers
                    String msg = wrapper.get(Type.STRING, 0);
                    if (msg.length() > 100) {
                        wrapper.set(Type.STRING, 0, msg.substring(0, 100));
                    }
                }
            });
        }
    });
}
Also used : EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) MetadataRewriter1_11To1_10(com.viaversion.viaversion.protocols.protocol1_11to1_10.metadata.MetadataRewriter1_11To1_10) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Types1_9(com.viaversion.viaversion.api.type.types.version.Types1_9) PotionColorMapping(com.viaversion.viaversion.protocols.protocol1_11to1_10.data.PotionColorMapping) Pair(com.viaversion.viaversion.util.Pair) EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) Entity1_11Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_11to1_10.packets.InventoryPackets) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) Entity1_11Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) Pair(com.viaversion.viaversion.util.Pair)

Example 2 with EntityTracker1_11

use of com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11 in project ViaVersion by ViaVersion.

the class MetadataRewriter1_11To1_10 method handleMetadata.

@Override
protected void handleMetadata(int entityId, com.viaversion.viaversion.api.minecraft.entities.EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
    if (metadata.getValue() instanceof DataItem) {
        // Apply rewrite
        EntityIdRewriter.toClientItem((Item) metadata.getValue());
    }
    if (type == null)
        return;
    if (type.is(EntityType.ELDER_GUARDIAN) || type.is(EntityType.GUARDIAN)) {
        // Guardians
        int oldid = metadata.id();
        if (oldid == 12) {
            boolean val = (((byte) metadata.getValue()) & 0x02) == 0x02;
            metadata.setTypeAndValue(MetaType1_9.Boolean, val);
        }
    }
    if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) {
        // Skeletons
        int oldid = metadata.id();
        if (oldid == 12) {
            metadatas.remove(metadata);
        }
        if (oldid == 13) {
            metadata.setId(12);
        }
    }
    if (type.isOrHasParent(EntityType.ZOMBIE)) {
        // Zombie | Zombie Villager | Husk
        if (type.is(EntityType.ZOMBIE, EntityType.HUSK) && metadata.id() == 14) {
            metadatas.remove(metadata);
        } else {
            if (metadata.id() == 15) {
                metadata.setId(14);
            } else {
                if (metadata.id() == 14) {
                    metadata.setId(15);
                }
            }
        }
    }
    if (type.isOrHasParent(EntityType.ABSTRACT_HORSE)) {
        // Horses
        // Remap metadata id
        int oldid = metadata.id();
        if (oldid == 14) {
            // Type
            metadatas.remove(metadata);
        }
        if (oldid == 16) {
            // Owner
            metadata.setId(14);
        }
        if (oldid == 17) {
            // Armor
            metadata.setId(16);
        }
        // Process per type
        if (type.is(EntityType.HORSE)) {
        // Normal Horse
        } else {
            // Remove 15, 16
            if (metadata.id() == 15 || metadata.id() == 16) {
                metadatas.remove(metadata);
            }
        }
        if (type.is(EntityType.DONKEY, EntityType.MULE)) {
            // Chested Horse
            if (metadata.id() == 13) {
                if ((((byte) metadata.getValue()) & 0x08) == 0x08) {
                    metadatas.add(new Metadata(15, MetaType1_9.Boolean, true));
                } else {
                    metadatas.add(new Metadata(15, MetaType1_9.Boolean, false));
                }
            }
        }
    }
    if (type.is(EntityType.ARMOR_STAND) && Via.getConfig().isHologramPatch()) {
        Metadata flags = metaByIndex(11, metadatas);
        Metadata customName = metaByIndex(2, metadatas);
        Metadata customNameVisible = metaByIndex(3, metadatas);
        if (metadata.id() == 0 && flags != null && customName != null && customNameVisible != null) {
            byte data = (byte) metadata.getValue();
            // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true
            if ((data & 0x20) == 0x20 && ((byte) flags.getValue() & 0x01) == 0x01 && !((String) customName.getValue()).isEmpty() && (boolean) customNameVisible.getValue()) {
                EntityTracker1_11 tracker = tracker(connection);
                if (tracker.addHologram(entityId)) {
                    try {
                        // Send movement
                        PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9_3.ENTITY_POSITION, null, connection);
                        wrapper.write(Type.VAR_INT, entityId);
                        wrapper.write(Type.SHORT, (short) 0);
                        wrapper.write(Type.SHORT, (short) (128D * (-Via.getConfig().getHologramYOffset() * 32D)));
                        wrapper.write(Type.SHORT, (short) 0);
                        wrapper.write(Type.BOOLEAN, true);
                        wrapper.send(Protocol1_11To1_10.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
Also used : EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

Aggregations

PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)2 EntityTracker1_11 (com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11)2 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)1 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)1 Via (com.viaversion.viaversion.api.Via)1 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)1 Entity1_11Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types)1 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)1 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)1 AbstractProtocol (com.viaversion.viaversion.api.protocol.AbstractProtocol)1 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 ValueTransformer (com.viaversion.viaversion.api.protocol.remapper.ValueTransformer)1 ItemRewriter (com.viaversion.viaversion.api.rewriter.ItemRewriter)1 Type (com.viaversion.viaversion.api.type.Type)1 Types1_9 (com.viaversion.viaversion.api.type.types.version.Types1_9)1 PotionColorMapping (com.viaversion.viaversion.protocols.protocol1_11to1_10.data.PotionColorMapping)1 MetadataRewriter1_11To1_10 (com.viaversion.viaversion.protocols.protocol1_11to1_10.metadata.MetadataRewriter1_11To1_10)1 InventoryPackets (com.viaversion.viaversion.protocols.protocol1_11to1_10.packets.InventoryPackets)1