Search in sources :

Example 1 with MetadataRewriter1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8 in project ViaVersion by ViaVersion.

the class EntityPackets method register.

public static void register(Protocol1_9To1_8 protocol) {
    // Attach Entity Packet
    protocol.registerClientbound(ClientboundPackets1_8.ATTACH_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Vehicle
            map(Type.INT);
            // Leash boolean is removed in new versions
            map(Type.BOOLEAN, new ValueTransformer<Boolean, Void>(Type.NOTHING) {

                @Override
                public Void transform(PacketWrapper wrapper, Boolean inputValue) throws Exception {
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (!inputValue) {
                        int passenger = wrapper.get(Type.INT, 0);
                        int vehicle = wrapper.get(Type.INT, 1);
                        // Don't send current packet
                        wrapper.cancel();
                        PacketWrapper passengerPacket = wrapper.create(ClientboundPackets1_9.SET_PASSENGERS);
                        if (vehicle == -1) {
                            if (!tracker.getVehicleMap().containsKey(passenger))
                                // Cancel
                                return null;
                            passengerPacket.write(Type.VAR_INT, tracker.getVehicleMap().remove(passenger));
                            passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] {});
                        } else {
                            passengerPacket.write(Type.VAR_INT, vehicle);
                            passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { passenger });
                            tracker.getVehicleMap().put(passenger, vehicle);
                        }
                        // Send the packet
                        passengerPacket.send(Protocol1_9To1_8.class);
                    }
                    return null;
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 2 - Y - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 3 - Z - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 4 - Pitch
            map(Type.BYTE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - On Ground
            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_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        if (tracker.getKnownHolograms().contains(entityID)) {
                            Double newValue = wrapper.get(Type.DOUBLE, 1);
                            newValue += (Via.getConfig().getHologramYOffset());
                            wrapper.set(Type.DOUBLE, 1, newValue);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION_AND_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X
            map(Type.BYTE, toNewShort);
            // 2 - Y
            map(Type.BYTE, toNewShort);
            // 3 - Z
            map(Type.BYTE, toNewShort);
            // 4 - Yaw
            map(Type.BYTE);
            // 5 - Pitch
            map(Type.BYTE);
            // 6 - On Ground
            map(Type.BOOLEAN);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X
            map(Type.BYTE, toNewShort);
            // 2 - Y
            map(Type.BYTE, toNewShort);
            // 3 - Z
            map(Type.BYTE, toNewShort);
            // 4 - On Ground
            map(Type.BOOLEAN);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EQUIPMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Slot ID
            map(Type.SHORT, new ValueTransformer<Short, Integer>(Type.VAR_INT) {

                @Override
                public Integer transform(PacketWrapper wrapper, Short slot) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    int receiverId = wrapper.user().getEntityTracker(Protocol1_9To1_8.class).clientEntityId();
                    // (In 1.9+, every client treats the received the same: 0=hand, 1=offhand, 2-5=armor)
                    if (entityId == receiverId) {
                        return slot.intValue() + 2;
                    }
                    return slot > 0 ? slot.intValue() + 1 : slot.intValue();
                }
            });
            // 2 - Item
            map(Type.ITEM);
            // Item Rewriter
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item stack = wrapper.get(Type.ITEM, 0);
                    ItemRewriter.toClient(stack);
                }
            });
            // Blocking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    Item stack = wrapper.get(Type.ITEM, 0);
                    if (stack != null) {
                        if (Protocol1_9To1_8.isSword(stack.identifier())) {
                            entityTracker.getValidBlocking().add(entityID);
                            return;
                        }
                    }
                    entityTracker.getValidBlocking().remove(entityID);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_METADATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Metadata List
            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 {
                        // Buffer
                        tracker.addMetadataToBuffer(entityId, metadataList);
                        wrapper.cancel();
                    }
                }
            });
            // 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);
                }
            });
            // Cancel packet if list empty
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    if (metadataList.isEmpty()) {
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Effect ID
            map(Type.BYTE);
            // 2 - Amplifier
            map(Type.BYTE);
            // 3 - Duration
            map(Type.VAR_INT);
            handler(new // Handle effect indicator
            PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // In 1.8 = true->Show particles : false->Hide particles
                    boolean showParticles = wrapper.read(Type.BOOLEAN);
                    boolean newEffect = Via.getConfig().isNewEffectIndicator();
                    // 0: hide, 1: shown without indictator, 2: shown with indicator, 3: hide with beacon indicator but we don't use it.
                    wrapper.write(Type.BYTE, (byte) (showParticles ? newEffect ? 2 : 1 : 0));
                }
            });
        }
    });
    protocol.cancelClientbound(ClientboundPackets1_8.UPDATE_ENTITY_NBT);
    protocol.registerClientbound(ClientboundPackets1_8.COMBAT_EVENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Event id
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.VAR_INT, 0) == 2) {
                        // entity dead
                        // Player id
                        wrapper.passthrough(Type.VAR_INT);
                        // Entity id
                        wrapper.passthrough(Type.INT);
                        Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_PROPERTIES, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (!Via.getConfig().isMinimizeCooldown())
                        return;
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (wrapper.get(Type.VAR_INT, 0) != tracker.getProvidedEntityId()) {
                        return;
                    }
                    int propertiesToRead = wrapper.read(Type.INT);
                    Map<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> properties = new HashMap<>(propertiesToRead);
                    for (int i = 0; i < propertiesToRead; i++) {
                        String key = wrapper.read(Type.STRING);
                        Double value = wrapper.read(Type.DOUBLE);
                        int modifiersToRead = wrapper.read(Type.VAR_INT);
                        List<Triple<UUID, Double, Byte>> modifiers = new ArrayList<>(modifiersToRead);
                        for (int j = 0; j < modifiersToRead; j++) {
                            modifiers.add(new Triple<>(wrapper.read(Type.UUID), // Amount
                            wrapper.read(Type.DOUBLE), // Operation
                            wrapper.read(Type.BYTE)));
                        }
                        properties.put(key, new Pair<>(value, modifiers));
                    }
                    // == Why 15.9? ==
                    // Higher values hides the cooldown but it bugs visual animation on hand
                    // when removing item from hand with inventory gui
                    properties.put("generic.attackSpeed", new Pair<Double, List<Triple<UUID, Double, Byte>>>(15.9, // Neutralize modifiers
                    ImmutableList.of(// Tool and weapon modifier
                    new Triple<>(UUID.fromString("FA233E1C-4180-4865-B01B-BCCE9785ACA3"), 0.0, (byte) 0), // Dig speed
                    new Triple<>(UUID.fromString("AF8B6E3F-3328-4C0A-AA36-5BA2BB9DBEF3"), 0.0, (byte) 2), // Dig slow down
                    new Triple<>(UUID.fromString("55FCED67-E92A-486E-9800-B47F202C4386"), 0.0, (byte) 2))));
                    wrapper.write(Type.INT, properties.size());
                    for (Map.Entry<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> entry : properties.entrySet()) {
                        // Key
                        wrapper.write(Type.STRING, entry.getKey());
                        // Value
                        wrapper.write(Type.DOUBLE, entry.getValue().key());
                        wrapper.write(Type.VAR_INT, entry.getValue().value().size());
                        for (Triple<UUID, Double, Byte> modifier : entry.getValue().value()) {
                            wrapper.write(Type.UUID, modifier.first());
                            // Amount
                            wrapper.write(Type.DOUBLE, modifier.second());
                            // Operation
                            wrapper.write(Type.BYTE, modifier.third());
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_ANIMATION, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 3) {
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.ENTITY_ACTION, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 1);
                    if (action == 6 || action == 8)
                        wrapper.cancel();
                    if (action == 7) {
                        wrapper.set(Type.VAR_INT, 1, 6);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.INTERACT_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID (Target)
            map(Type.VAR_INT);
            // 1 - Action Type
            map(Type.VAR_INT);
            // Cancel second hand to prevent double interact
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.get(Type.VAR_INT, 1);
                    if (type == 2) {
                        // 2 - X
                        wrapper.passthrough(Type.FLOAT);
                        // 3 - Y
                        wrapper.passthrough(Type.FLOAT);
                        // 4 - Z
                        wrapper.passthrough(Type.FLOAT);
                    }
                    if (type == 0 || type == 2) {
                        // 2/5 - Hand
                        int hand = wrapper.read(Type.VAR_INT);
                        if (hand == 1)
                            wrapper.cancel();
                    }
                }
            });
        }
    });
}
Also used : HashMap(java.util.HashMap) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ArrayList(java.util.ArrayList) Item(com.viaversion.viaversion.api.minecraft.item.Item) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MetadataRewriter1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8) UUID(java.util.UUID) Pair(com.viaversion.viaversion.util.Pair) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) Triple(com.viaversion.viaversion.util.Triple) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) HashMap(java.util.HashMap) Map(java.util.Map) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Example 2 with MetadataRewriter1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8 in project ViaVersion by ViaVersion.

the class EntityTracker1_9 method sendMetadataBuffer.

public void sendMetadataBuffer(int entityId) {
    List<Metadata> metadataList = metadataBuffer.get(entityId);
    if (metadataList != null) {
        PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.ENTITY_METADATA, null, user());
        wrapper.write(Type.VAR_INT, entityId);
        wrapper.write(Types1_9.METADATA_LIST, metadataList);
        Via.getManager().getProtocolManager().getProtocol(Protocol1_9To1_8.class).get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, user());
        handleMetadata(entityId, metadataList);
        if (!metadataList.isEmpty()) {
            try {
                wrapper.scheduleSend(Protocol1_9To1_8.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        metadataBuffer.remove(entityId);
    }
}
Also used : PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) MetadataRewriter1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8)

Example 3 with MetadataRewriter1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8 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)

Aggregations

Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)3 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)3 MetadataRewriter1_9To1_8 (com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8)3 Item (com.viaversion.viaversion.api.minecraft.item.Item)2 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)2 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)2 EntityTracker1_9 (com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ImmutableList (com.google.common.collect.ImmutableList)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)1 ValueTransformer (com.viaversion.viaversion.api.protocol.remapper.ValueTransformer)1 Protocol1_9To1_8 (com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)1 Pair (com.viaversion.viaversion.util.Pair)1 Triple (com.viaversion.viaversion.util.Triple)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1