Search in sources :

Example 16 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaVersion by ViaVersion.

the class EntityRewriter method handleMetadata.

@Override
public void handleMetadata(int entityId, List<Metadata> metadataList, UserConnection connection) {
    EntityType type = tracker(connection).entityType(entityId);
    // Count index for fast removal
    int i = 0;
    for (Metadata metadata : metadataList.toArray(EMPTY_ARRAY)) {
        // Call handlers implementing the old handleMetadata
        if (!callOldMetaHandler(entityId, type, metadata, metadataList, connection)) {
            metadataList.remove(i--);
            continue;
        }
        MetaHandlerEvent event = null;
        for (MetaFilter filter : metadataFilters) {
            if (!filter.isFiltered(type, metadata)) {
                continue;
            }
            if (event == null) {
                // Only initialize when needed and share event instance
                event = new MetaHandlerEventImpl(connection, type, entityId, metadata, metadataList);
            }
            try {
                filter.handler().handle(event, metadata);
            } catch (Exception e) {
                logException(e, type, metadataList, metadata);
                metadataList.remove(i--);
                break;
            }
            if (event.cancelled()) {
                // Remove meta, decrease list index counter, and break current filter loop
                metadataList.remove(i--);
                break;
            }
        }
        if (event != null && event.extraMeta() != null) {
            // Finally, add newly created meta
            metadataList.addAll(event.extraMeta());
        }
        i++;
    }
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) MetaFilter(com.viaversion.viaversion.rewriter.meta.MetaFilter) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) MetaHandlerEvent(com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent) MetaHandlerEventImpl(com.viaversion.viaversion.rewriter.meta.MetaHandlerEventImpl)

Example 17 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata 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)

Example 18 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaVersion by ViaVersion.

the class MetadataRewriter1_11To1_10 method rewriteEntityType.

public static EntityType rewriteEntityType(int numType, List<Metadata> metadata) {
    Optional<EntityType> optType = EntityType.findById(numType);
    if (!optType.isPresent()) {
        Via.getManager().getPlatform().getLogger().severe("Error: could not find Entity type " + numType + " with metadata: " + metadata);
        return null;
    }
    EntityType type = optType.get();
    try {
        if (type.is(EntityType.GUARDIAN)) {
            // ElderGuardian - 4
            Optional<Metadata> options = getById(metadata, 12);
            if (options.isPresent()) {
                if ((((byte) options.get().getValue()) & 0x04) == 0x04) {
                    return EntityType.ELDER_GUARDIAN;
                }
            }
        }
        if (type.is(EntityType.SKELETON)) {
            // WitherSkeleton - 5
            // Stray - 6
            Optional<Metadata> options = getById(metadata, 12);
            if (options.isPresent()) {
                if (((int) options.get().getValue()) == 1) {
                    return EntityType.WITHER_SKELETON;
                }
                if (((int) options.get().getValue()) == 2) {
                    return EntityType.STRAY;
                }
            }
        }
        if (type.is(EntityType.ZOMBIE)) {
            // ZombieVillager - 27
            // Husk - 23
            Optional<Metadata> options = getById(metadata, 13);
            if (options.isPresent()) {
                int value = (int) options.get().getValue();
                if (value > 0 && value < 6) {
                    // Add profession type to new metadata
                    metadata.add(new Metadata(16, MetaType1_9.VarInt, value - 1));
                    return EntityType.ZOMBIE_VILLAGER;
                }
                if (value == 6) {
                    return EntityType.HUSK;
                }
            }
        }
        if (type.is(EntityType.HORSE)) {
            // SkeletonHorse - 28
            // ZombieHorse - 29
            // Donkey - 31
            // Mule - 32
            Optional<Metadata> options = getById(metadata, 14);
            if (options.isPresent()) {
                if (((int) options.get().getValue()) == 0) {
                    return EntityType.HORSE;
                }
                if (((int) options.get().getValue()) == 1) {
                    return EntityType.DONKEY;
                }
                if (((int) options.get().getValue()) == 2) {
                    return EntityType.MULE;
                }
                if (((int) options.get().getValue()) == 3) {
                    return EntityType.ZOMBIE_HORSE;
                }
                if (((int) options.get().getValue()) == 4) {
                    return EntityType.SKELETON_HORSE;
                }
            }
        }
    } catch (Exception e) {
        if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("An error occurred with entity type rewriter");
            Via.getPlatform().getLogger().warning("Metadata: " + metadata);
            e.printStackTrace();
        }
    }
    return type;
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types.EntityType) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

Example 19 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaVersion by ViaVersion.

the class MetadataRewriter1_9To1_8 method handleMetadata.

@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
    MetaIndex metaIndex = MetaIndex.searchIndex(type, metadata.id());
    if (metaIndex == null) {
        throw new Exception("Could not find valid metadata");
    }
    if (metaIndex.getNewType() == null) {
        metadatas.remove(metadata);
        return;
    }
    metadata.setId(metaIndex.getNewIndex());
    metadata.setMetaTypeUnsafe(metaIndex.getNewType());
    Object value = metadata.getValue();
    switch(metaIndex.getNewType()) {
        case Byte:
            // convert from int, byte
            if (metaIndex.getOldType() == MetaType1_8.Byte) {
                metadata.setValue(value);
            }
            if (metaIndex.getOldType() == MetaType1_8.Int) {
                metadata.setValue(((Integer) value).byteValue());
            }
            // After writing the last one
            if (metaIndex == MetaIndex.ENTITY_STATUS && type == Entity1_10Types.EntityType.PLAYER) {
                Byte val = 0;
                if ((((Byte) value) & 0x10) == 0x10) {
                    // Player eating/aiming/drinking
                    val = 1;
                }
                int newIndex = MetaIndex.PLAYER_HAND.getNewIndex();
                MetaType metaType = MetaIndex.PLAYER_HAND.getNewType();
                metadatas.add(new Metadata(newIndex, metaType, val));
            }
            break;
        case OptUUID:
            String owner = (String) value;
            UUID toWrite = null;
            if (!owner.isEmpty()) {
                try {
                    toWrite = UUID.fromString(owner);
                } catch (Exception ignored) {
                }
            }
            metadata.setValue(toWrite);
            break;
        case VarInt:
            // convert from int, short, byte
            if (metaIndex.getOldType() == MetaType1_8.Byte) {
                metadata.setValue(((Byte) value).intValue());
            }
            if (metaIndex.getOldType() == MetaType1_8.Short) {
                metadata.setValue(((Short) value).intValue());
            }
            if (metaIndex.getOldType() == MetaType1_8.Int) {
                metadata.setValue(value);
            }
            break;
        case Float:
            metadata.setValue(value);
            break;
        case String:
            metadata.setValue(value);
            break;
        case Boolean:
            if (metaIndex == MetaIndex.AGEABLE_AGE)
                metadata.setValue((Byte) value < 0);
            else
                metadata.setValue((Byte) value != 0);
            break;
        case Slot:
            metadata.setValue(value);
            ItemRewriter.toClient((Item) metadata.getValue());
            break;
        case Position:
            Vector vector = (Vector) value;
            metadata.setValue(vector);
            break;
        case Vector3F:
            EulerAngle angle = (EulerAngle) value;
            metadata.setValue(angle);
            break;
        case Chat:
            value = Protocol1_9To1_8.fixJson(value.toString());
            metadata.setValue(value);
            break;
        case BlockID:
            // Convert from int, short, byte
            metadata.setValue(((Number) value).intValue());
            break;
        default:
            metadatas.remove(metadata);
            throw new Exception("Unhandled MetaDataType: " + metaIndex.getNewType());
    }
}
Also used : Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) UUID(java.util.UUID) EulerAngle(com.viaversion.viaversion.api.minecraft.EulerAngle) Vector(com.viaversion.viaversion.api.minecraft.Vector) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 20 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata 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)

Aggregations

Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)37 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)21 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)15 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)14 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)11 Item (com.viaversion.viaversion.api.minecraft.item.Item)10 Type (com.viaversion.viaversion.api.type.Type)8 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)6 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)6 EntityData (com.viaversion.viabackwards.api.entities.storage.EntityData)5 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)5 Particle (com.viaversion.viaversion.api.type.types.Particle)5 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)5 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)4 WrappedMetadata (com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)4 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)4 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)4 ArrayList (java.util.ArrayList)4 EntityPositionHandler (com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler)3 Block (com.viaversion.viabackwards.utils.Block)3