Search in sources :

Example 11 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier in project Denizen-For-Bukkit by DenizenScript.

the class EntityAttributeModifiers method getAttributes.

@Deprecated
public ListTag getAttributes() {
    ListTag list = new ListTag();
    for (Attribute attribute : Attribute.values()) {
        AttributeInstance instance = getAttributable().getAttribute(attribute);
        if (instance == null) {
            continue;
        }
        StringBuilder modifiers = new StringBuilder();
        for (AttributeModifier modifier : instance.getModifiers()) {
            modifiers.append("/").append(stringify(modifier));
        }
        list.add(EscapeTagBase.escape(attribute.name()) + "/" + instance.getBaseValue() + modifiers.toString());
    }
    return list;
}
Also used : Attribute(org.bukkit.attribute.Attribute) AttributeInstance(org.bukkit.attribute.AttributeInstance) AttributeModifier(org.bukkit.attribute.AttributeModifier) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 12 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier in project Glowstone by GlowstoneMC.

the class LivingEntityStore method load.

// these tags that apply to living entities only are documented as global:
// - short "Air"
// - string "CustomName"
// - bool "CustomNameVisible"
// todo: the following tags
// - float "AbsorptionAmount"
// - short "HurtTime"
// - int "HurtByTimestamp"
// - short "DeathTime"
// - bool "PersistenceRequired"
// on ActiveEffects, bool "ShowParticles"
@Override
public void load(T entity, CompoundTag compound) {
    super.load(entity, compound);
    compound.readShort("Air", entity::setRemainingAir);
    compound.readString("CustomName", entity::setCustomName);
    compound.readBoolean("CustomNameVisible", entity::setCustomNameVisible);
    if (!compound.readFloat("HealF", entity::setHealth)) {
        compound.readShort("Health", entity::setHealth);
    }
    compound.readShort("AttackTime", entity::setNoDamageTicks);
    compound.readBoolean("FallFlying", entity::setFallFlying);
    compound.iterateCompoundList("ActiveEffects", effect -> {
        // should really always have every field, but be forgiving if possible
        if (!effect.isByte("Id") || !effect.isInt("Duration")) {
            return;
        }
        PotionEffectType type = PotionEffectType.getById(effect.getByte("Id"));
        int duration = effect.getInt("Duration");
        if (type == null || duration < 0) {
            return;
        }
        final int amplifier = compound.tryGetInt("Amplifier").orElse(0);
        boolean ambient = compound.getBoolean("Ambient", false);
        // bool "ShowParticles"
        entity.addPotionEffect(new PotionEffect(type, duration, amplifier, ambient), true);
    });
    EntityEquipment equip = entity.getEquipment();
    if (equip != null) {
        loadEquipment(entity, equip, compound);
    }
    compound.readBoolean("CanPickUpLoot", entity::setCanPickupItems);
    AttributeManager am = entity.getAttributeManager();
    compound.iterateCompoundList("Attributes", tag -> {
        if (!tag.isString("Name") || !tag.isDouble("Base")) {
            return;
        }
        List<AttributeModifier> modifiers = new ArrayList<>();
        tag.iterateCompoundList("Modifiers", modifierTag -> {
            if (modifierTag.isDouble("Amount") && modifierTag.isString("Name") && modifierTag.isInt("Operation") && modifierTag.isLong("UUIDLeast") && modifierTag.isLong("UUIDMost")) {
                modifiers.add(new AttributeModifier(new UUID(modifierTag.getLong("UUIDLeast"), modifierTag.getLong("UUIDMost")), modifierTag.getString("Name"), modifierTag.getDouble("Amount"), AttributeModifier.Operation.values()[modifierTag.getInt("Operation")]));
            }
        });
        AttributeManager.Key key = AttributeManager.Key.fromName(tag.getString("Name"));
        am.setProperty(key, tag.getDouble("Base"), modifiers);
    });
    Optional<CompoundTag> maybeLeash = compound.tryGetCompound("Leash");
    if (maybeLeash.isPresent()) {
        CompoundTag leash = maybeLeash.get();
        if (!leash.readUniqueId("UUIDMost", "UUIDLeast", entity::setLeashHolderUniqueId) && leash.isInt("X") && leash.isInt("Y") && leash.isInt("Z")) {
            int x = leash.getInt("X");
            int y = leash.getInt("Y");
            int z = leash.getInt("Z");
            LeashHitch leashHitch = GlowLeashHitch.getLeashHitchAt(new Location(entity.getWorld(), x, y, z).getBlock());
            entity.setLeashHolder(leashHitch);
        }
    } else {
        compound.readBoolean("Leashed", leashSet -> {
            if (leashSet) {
                // We know that there was something leashed, but not what entity it was
                // This can happen, when for example Minecart got leashed
                // We still have to make sure that we drop a Leash Item
                entity.setLeashHolderUniqueId(UUID.randomUUID());
            }
        });
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) PotionEffectType(org.bukkit.potion.PotionEffectType) ArrayList(java.util.ArrayList) AttributeModifier(org.bukkit.attribute.AttributeModifier) AttributeManager(net.glowstone.entity.AttributeManager) EntityEquipment(org.bukkit.inventory.EntityEquipment) LeashHitch(org.bukkit.entity.LeashHitch) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) UUID(java.util.UUID) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 13 with AttributeModifier

use of org.bukkit.attribute.AttributeModifier 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

AttributeModifier (org.bukkit.attribute.AttributeModifier)13 UUID (java.util.UUID)6 ListTag (com.denizenscript.denizencore.objects.core.ListTag)4 ArrayList (java.util.ArrayList)4 AttributeInstance (org.bukkit.attribute.AttributeInstance)4 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)3 MapTag (com.denizenscript.denizencore.objects.core.MapTag)3 Map (java.util.Map)3 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 Attribute (com.denizenscript.denizencore.tags.Attribute)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 AttributeManager (net.glowstone.entity.AttributeManager)2 Property (net.glowstone.entity.AttributeManager.Property)2 GlowLeashHitch (net.glowstone.entity.objects.GlowLeashHitch)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 Location (org.bukkit.Location)2 Attribute (org.bukkit.attribute.Attribute)2 LeashHitch (org.bukkit.entity.LeashHitch)2 EntityEquipment (org.bukkit.inventory.EntityEquipment)2 EquipmentSlot (org.bukkit.inventory.EquipmentSlot)2