Search in sources :

Example 1 with Property

use of net.glowstone.entity.AttributeManager.Property in project Glowstone by GlowstoneMC.

the class EntityPropertyCodec method encode.

@Override
public ByteBuf encode(ByteBuf buf, EntityPropertyMessage message) throws IOException {
    ByteBufUtils.writeVarInt(buf, message.getId());
    Map<String, Property> props = message.getProperties();
    buf.writeInt(props.size());
    for (Entry<String, Property> property : props.entrySet()) {
        ByteBufUtils.writeUTF8(buf, property.getKey());
        buf.writeDouble(property.getValue().getValue());
        Collection<AttributeModifier> modifiers = property.getValue().getModifiers();
        if (modifiers == null) {
            ByteBufUtils.writeVarInt(buf, 0);
        } else {
            ByteBufUtils.writeVarInt(buf, modifiers.size());
            for (AttributeModifier modifier : modifiers) {
                GlowBufUtils.writeUuid(buf, modifier.getUniqueId());
                buf.writeDouble(modifier.getAmount());
                buf.writeByte(modifier.getOperation().ordinal());
            }
        }
    }
    return buf;
}
Also used : AttributeModifier(org.bukkit.attribute.AttributeModifier) Property(net.glowstone.entity.AttributeManager.Property)

Example 2 with Property

use of net.glowstone.entity.AttributeManager.Property in project Glowstone by GlowstoneMC.

the class LivingEntityStore method save.

@Override
public void save(T entity, CompoundTag tag) {
    super.save(entity, tag);
    tag.putShort("Air", entity.getRemainingAir());
    if (entity.getCustomName() != null && !entity.getCustomName().isEmpty()) {
        tag.putString("CustomName", entity.getCustomName());
        tag.putBool("CustomNameVisible", entity.isCustomNameVisible());
    }
    tag.putFloat("HealF", entity.getHealth());
    tag.putShort("Health", (int) entity.getHealth());
    tag.putShort("AttackTime", entity.getNoDamageTicks());
    tag.putBool("FallFlying", entity.isFallFlying());
    Map<String, Property> properties = entity.getAttributeManager().getAllProperties();
    if (!properties.isEmpty()) {
        List<CompoundTag> attributes = new ArrayList<>(properties.size());
        properties.forEach((key, property) -> {
            CompoundTag attribute = new CompoundTag();
            attribute.putString("Name", key);
            attribute.putDouble("Base", property.getValue());
            Collection<AttributeModifier> modifiers = property.getModifiers();
            if (modifiers != null && !modifiers.isEmpty()) {
                List<CompoundTag> modifierTags = modifiers.stream().map(modifier -> {
                    CompoundTag modifierTag = new CompoundTag();
                    modifierTag.putDouble("Amount", modifier.getAmount());
                    modifierTag.putString("Name", modifier.getName());
                    modifierTag.putInt("Operation", modifier.getOperation().ordinal());
                    UUID uuid = modifier.getUniqueId();
                    modifierTag.putLong("UUIDLeast", uuid.getLeastSignificantBits());
                    modifierTag.putLong("UUIDMost", uuid.getMostSignificantBits());
                    return modifierTag;
                }).collect(Collectors.toList());
                attribute.putCompoundList("Modifiers", modifierTags);
            }
            attributes.add(attribute);
        });
        tag.putCompoundList("Attributes", attributes);
    }
    List<CompoundTag> effects = new LinkedList<>();
    for (PotionEffect effect : entity.getActivePotionEffects()) {
        CompoundTag effectTag = new CompoundTag();
        effectTag.putByte("Id", effect.getType().getId());
        effectTag.putByte("Amplifier", effect.getAmplifier());
        effectTag.putInt("Duration", effect.getDuration());
        effectTag.putBool("Ambient", effect.isAmbient());
        effectTag.putBool("ShowParticles", true);
        effects.add(effectTag);
    }
    tag.putCompoundList("ActiveEffects", effects);
    EntityEquipment equip = entity.getEquipment();
    if (equip != null) {
        tag.putCompoundList("HandItems", Arrays.asList(NbtSerialization.writeItem(equip.getItemInMainHand(), -1), NbtSerialization.writeItem(equip.getItemInOffHand(), -1)));
        tag.putCompoundList("ArmorItems", Arrays.asList(NbtSerialization.writeItem(equip.getBoots(), -1), NbtSerialization.writeItem(equip.getLeggings(), -1), NbtSerialization.writeItem(equip.getChestplate(), -1), NbtSerialization.writeItem(equip.getHelmet(), -1)));
        tag.putFloatList("HandDropChances", Arrays.asList(equip.getItemInMainHandDropChance(), equip.getItemInOffHandDropChance()));
        tag.putFloatList("ArmorDropChances", Arrays.asList(equip.getBootsDropChance(), equip.getLeggingsDropChance(), equip.getChestplateDropChance(), equip.getHelmetDropChance()));
    }
    tag.putBool("CanPickUpLoot", entity.getCanPickupItems());
    tag.putBool("Leashed", entity.isLeashed());
    if (entity.isLeashed()) {
        Entity leashHolder = entity.getLeashHolder();
        CompoundTag leash = new CompoundTag();
        // The empty Leash tag is still persisted tough
        if (leashHolder instanceof LeashHitch) {
            Location location = leashHolder.getLocation();
            leash.putInt("X", location.getBlockX());
            leash.putInt("Y", location.getBlockY());
            leash.putInt("Z", location.getBlockZ());
        } else if (leashHolder instanceof LivingEntity) {
            leash.putLong("UUIDMost", entity.getUniqueId().getMostSignificantBits());
            leash.putLong("UUIDLeast", entity.getUniqueId().getLeastSignificantBits());
        }
        tag.putCompound("Leash", leash);
    }
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag) Property(net.glowstone.entity.AttributeManager.Property) AttributeModifier(org.bukkit.attribute.AttributeModifier) Arrays(java.util.Arrays) Player(org.bukkit.entity.Player) LeashHitch(org.bukkit.entity.LeashHitch) InventoryUtil(net.glowstone.util.InventoryUtil) NbtSerialization(net.glowstone.io.nbt.NbtSerialization) ArrayList(java.util.ArrayList) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) Location(org.bukkit.Location) Map(java.util.Map) LinkedList(java.util.LinkedList) AttributeManager(net.glowstone.entity.AttributeManager) Entity(org.bukkit.entity.Entity) Collection(java.util.Collection) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) Collectors(java.util.stream.Collectors) ItemStack(org.bukkit.inventory.ItemStack) PotionEffect(org.bukkit.potion.PotionEffect) List(java.util.List) Optional(java.util.Optional) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) EntityEquipment(org.bukkit.inventory.EntityEquipment) PotionEffectType(org.bukkit.potion.PotionEffectType) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) PotionEffect(org.bukkit.potion.PotionEffect) ArrayList(java.util.ArrayList) AttributeModifier(org.bukkit.attribute.AttributeModifier) LinkedList(java.util.LinkedList) LivingEntity(org.bukkit.entity.LivingEntity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) EntityEquipment(org.bukkit.inventory.EntityEquipment) LeashHitch(org.bukkit.entity.LeashHitch) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) UUID(java.util.UUID) Property(net.glowstone.entity.AttributeManager.Property) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Aggregations

Property (net.glowstone.entity.AttributeManager.Property)2 AttributeModifier (org.bukkit.attribute.AttributeModifier)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 AttributeManager (net.glowstone.entity.AttributeManager)1 GlowLivingEntity (net.glowstone.entity.GlowLivingEntity)1 GlowLeashHitch (net.glowstone.entity.objects.GlowLeashHitch)1 NbtSerialization (net.glowstone.io.nbt.NbtSerialization)1 InventoryUtil (net.glowstone.util.InventoryUtil)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 Location (org.bukkit.Location)1 Entity (org.bukkit.entity.Entity)1 EntityType (org.bukkit.entity.EntityType)1