Search in sources :

Example 1 with StoredEntityData

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

the class SoundPackets1_14 method registerPackets.

@Override
protected void registerPackets() {
    SoundRewriter soundRewriter = new SoundRewriter(protocol);
    soundRewriter.registerSound(ClientboundPackets1_14.SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_14.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_14.STOP_SOUND);
    // Entity Sound Effect
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_SOUND, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                int soundId = wrapper.read(Type.VAR_INT);
                int newId = protocol.getMappingData().getSoundMappings().getNewId(soundId);
                if (newId == -1)
                    return;
                int category = wrapper.read(Type.VAR_INT);
                int entityId = wrapper.read(Type.VAR_INT);
                StoredEntityData storedEntity = wrapper.user().getEntityTracker(protocol.getClass()).entityData(entityId);
                EntityPositionStorage1_14 entityStorage;
                if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) {
                    ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId);
                    return;
                }
                float volume = wrapper.read(Type.FLOAT);
                float pitch = wrapper.read(Type.FLOAT);
                int x = (int) (entityStorage.getX() * 8D);
                int y = (int) (entityStorage.getY() * 8D);
                int z = (int) (entityStorage.getZ() * 8D);
                PacketWrapper soundPacket = wrapper.create(ClientboundPackets1_13.SOUND);
                soundPacket.write(Type.VAR_INT, newId);
                soundPacket.write(Type.VAR_INT, category);
                soundPacket.write(Type.INT, x);
                soundPacket.write(Type.INT, y);
                soundPacket.write(Type.INT, z);
                soundPacket.write(Type.FLOAT, volume);
                soundPacket.write(Type.FLOAT, pitch);
                soundPacket.send(Protocol1_13_2To1_14.class);
            });
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) Type(com.viaversion.viaversion.api.type.Type) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) RewriterBase(com.viaversion.viaversion.api.rewriter.RewriterBase) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)

Example 2 with StoredEntityData

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

the class EntityPackets1_11 method registerRewrites.

@Override
protected void registerRewrites() {
    // Guardian
    mapEntityTypeWithData(Entity1_11Types.EntityType.ELDER_GUARDIAN, Entity1_11Types.EntityType.GUARDIAN);
    // Skeletons
    mapEntityTypeWithData(Entity1_11Types.EntityType.WITHER_SKELETON, Entity1_11Types.EntityType.SKELETON).spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(1)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.STRAY, Entity1_11Types.EntityType.SKELETON).plainName().spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(2)));
    // Zombies
    mapEntityTypeWithData(Entity1_11Types.EntityType.HUSK, Entity1_11Types.EntityType.ZOMBIE).plainName().spawnMetadata(storage -> handleZombieType(storage, 6));
    mapEntityTypeWithData(Entity1_11Types.EntityType.ZOMBIE_VILLAGER, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 1));
    // Horses
    // Nob able to ride the horse without having the MetaType sent.
    mapEntityTypeWithData(Entity1_11Types.EntityType.HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(0)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.DONKEY, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(1)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.MULE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(2)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.SKELETON_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(4)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.ZOMBIE_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(3)));
    // New mobs
    mapEntityTypeWithData(Entity1_11Types.EntityType.EVOCATION_FANGS, Entity1_11Types.EntityType.SHULKER);
    mapEntityTypeWithData(Entity1_11Types.EntityType.EVOCATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).plainName();
    mapEntityTypeWithData(Entity1_11Types.EntityType.VEX, Entity1_11Types.EntityType.BAT).plainName();
    // Base Profession
    mapEntityTypeWithData(Entity1_11Types.EntityType.VINDICATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).plainName().spawnMetadata(storage -> storage.add(new Metadata(13, MetaType1_9.VarInt, 4)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.LIAMA, Entity1_11Types.EntityType.HORSE).plainName().spawnMetadata(storage -> storage.add(getHorseMetaType(1)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.LIAMA_SPIT, Entity1_11Types.EntityType.SNOWBALL);
    mapObjectType(Entity1_11Types.ObjectType.LIAMA_SPIT, Entity1_11Types.ObjectType.SNOWBALL, -1);
    // Replace with endertorchthingies
    mapObjectType(Entity1_11Types.ObjectType.EVOCATION_FANGS, Entity1_11Types.ObjectType.FALLING_BLOCK, 198 | 1 << 12);
    // Handle ElderGuardian & target metadata
    filter().filterFamily(Entity1_11Types.EntityType.GUARDIAN).index(12).handler((event, meta) -> {
        boolean b = (boolean) meta.getValue();
        int bitmask = b ? 0x02 : 0;
        if (event.entityType() == Entity1_11Types.EntityType.ELDER_GUARDIAN) {
            bitmask |= 0x04;
        }
        meta.setTypeAndValue(MetaType1_9.Byte, (byte) bitmask);
    });
    // Handle skeleton swing
    filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_SKELETON).index(12).toIndex(13);
    /*
            ZOMBIE CHANGES
         */
    filter().filterFamily(Entity1_11Types.EntityType.ZOMBIE).handler((event, meta) -> {
        switch(meta.id()) {
            case 13:
                event.cancel();
                return;
            case 14:
                event.setIndex(15);
                break;
            case 15:
                event.setIndex(14);
                break;
            // Profession
            case 16:
                event.setIndex(13);
                meta.setValue(1 + (int) meta.getValue());
                break;
        }
    });
    // Handle Evocation Illager
    filter().type(Entity1_11Types.EntityType.EVOCATION_ILLAGER).index(12).handler((event, meta) -> {
        event.setIndex(13);
        // Change the profession for the states
        meta.setTypeAndValue(MetaType1_9.VarInt, ((Byte) meta.getValue()).intValue());
    });
    // Handle Vex (Remove this field completely since the position is not updated correctly when idling for bats. Sad ):
    filter().type(Entity1_11Types.EntityType.VEX).index(12).handler((event, meta) -> {
        meta.setValue((byte) 0x00);
    });
    // Handle VindicationIllager
    filter().type(Entity1_11Types.EntityType.VINDICATION_ILLAGER).index(12).handler((event, meta) -> {
        event.setIndex(13);
        meta.setTypeAndValue(MetaType1_9.VarInt, ((Number) meta.getValue()).intValue() == 1 ? 2 : 4);
    });
    /*
            HORSES
         */
    // Handle horse flags
    filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_HORSE).index(13).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        byte b = (byte) meta.getValue();
        if (data.has(ChestedHorseStorage.class) && data.get(ChestedHorseStorage.class).isChested()) {
            // Chested
            b |= 0x08;
            meta.setValue(b);
        }
    });
    // Create chested horse storage
    filter().filterFamily(Entity1_11Types.EntityType.CHESTED_HORSE).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        if (!data.has(ChestedHorseStorage.class)) {
            data.put(new ChestedHorseStorage());
        }
    });
    // Handle horse armor
    filter().type(Entity1_11Types.EntityType.HORSE).index(16).toIndex(17);
    // Handle chested horse
    filter().filterFamily(Entity1_11Types.EntityType.CHESTED_HORSE).index(15).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        ChestedHorseStorage storage = data.get(ChestedHorseStorage.class);
        boolean b = (boolean) meta.getValue();
        storage.setChested(b);
        event.cancel();
    });
    // Get rid of Liama metadata
    filter().type(Entity1_11Types.EntityType.LIAMA).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        ChestedHorseStorage storage = data.get(ChestedHorseStorage.class);
        int index = event.index();
        // Store them for later (:
        switch(index) {
            case 16:
                storage.setLiamaStrength((int) meta.getValue());
                event.cancel();
                break;
            case 17:
                storage.setLiamaCarpetColor((int) meta.getValue());
                event.cancel();
                break;
            case 18:
                storage.setLiamaVariant((int) meta.getValue());
                event.cancel();
                break;
        }
    });
    // Handle Horse (Correct owner)
    filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_HORSE).index(14).toIndex(16);
    // Handle villager - Change non-existing profession
    filter().type(Entity1_11Types.EntityType.VILLAGER).index(13).handler((event, meta) -> {
        if ((int) meta.getValue() == 5) {
            meta.setValue(0);
        }
    });
    // handle new Shulker color meta
    filter().type(Entity1_11Types.EntityType.SHULKER).cancel(15);
}
Also used : WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) ChestedHorseStorage(com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage)

Example 3 with StoredEntityData

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

the class EntityPositionHandler method getStorage.

public EntityPositionStorage getStorage(UserConnection user, int entityId) {
    StoredEntityData storedEntity = entityRewriter.tracker(user).entityData(entityId);
    EntityPositionStorage entityStorage;
    if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage.class)) == null) {
        ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId + " in " + storageClass.getSimpleName());
        return null;
    }
    return entityStorage;
}
Also used : StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData)

Example 4 with StoredEntityData

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

the class EntityPositionHandler method cacheEntityPosition.

public void cacheEntityPosition(PacketWrapper wrapper, double x, double y, double z, boolean create, boolean relative) throws Exception {
    int entityId = wrapper.get(Type.VAR_INT, 0);
    StoredEntityData storedEntity = entityRewriter.tracker(wrapper.user()).entityData(entityId);
    if (storedEntity == null) {
        if (Via.getManager().isDebug()) {
            // There is too many plugins violating this out there, and reading seems to be hard! :>
            ViaBackwards.getPlatform().getLogger().warning("Stored entity with id " + entityId + " missing at position: " + x + " - " + y + " - " + z + " in " + storageClass.getSimpleName());
            if (entityId == -1 && x == 0 && y == 0 && z == 0) {
                ViaBackwards.getPlatform().getLogger().warning("DO NOT REPORT THIS TO VIA, THIS IS A PLUGIN ISSUE");
            } else if (!warnedForMissingEntity) {
                warnedForMissingEntity = true;
                ViaBackwards.getPlatform().getLogger().warning("This is very likely caused by a plugin sending a teleport packet for an entity outside of the player's range.");
            }
        }
        return;
    }
    EntityPositionStorage positionStorage;
    if (create) {
        positionStorage = storageSupplier.get();
        storedEntity.put(positionStorage);
    } else {
        positionStorage = storedEntity.get(storageClass);
        if (positionStorage == null) {
            ViaBackwards.getPlatform().getLogger().warning("Stored entity with id " + entityId + " missing " + storageClass.getSimpleName());
            return;
        }
    }
    positionStorage.setCoordinates(x, y, z, relative);
}
Also used : StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData)

Example 5 with StoredEntityData

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

the class EntityTrackerBase method entityData.

@Override
@Nullable
public StoredEntityData entityData(int id) {
    Preconditions.checkArgument(entityData != null, "Entity data storage has to be explicitly enabled via the constructor");
    EntityType type = entityType(id);
    return type != null ? entityData.computeIfAbsent(id, s -> new StoredEntityImpl(type)) : null;
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Int2ObjectSyncMap(space.vectrix.flare.fastutil.Int2ObjectSyncMap) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ClientEntityIdChangeListener(com.viaversion.viaversion.api.data.entity.ClientEntityIdChangeListener) Preconditions(com.google.common.base.Preconditions) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)10 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)3 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)3 WindowTracker (com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.WindowTracker)2 Preconditions (com.google.common.base.Preconditions)1 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)1 WrappedMetadata (com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)1 SoundRewriter (com.viaversion.viabackwards.api.rewriters.SoundRewriter)1 ChestedHorseStorage (com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage)1 ParrotStorage (com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ParrotStorage)1 ShoulderTracker (com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker)1 Protocol1_13_2To1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)1 EntityPositionStorage1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14)1 WolfDataMaskStorage (com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage)1 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 ClientEntityIdChangeListener (com.viaversion.viaversion.api.data.entity.ClientEntityIdChangeListener)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 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)1