Search in sources :

Example 11 with AttributeInstance

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

the class EntityUtils method heal.

/**
 * Heals an entity by a specific amount.
 *
 * @param target the entity to heal
 * @param amount the amount of health to regain
 * @param reason the reason supplied to the {@link EntityRegainHealthEvent}
 * @return whether any health was regained this way
 * @throws IllegalArgumentException if the specified target has no {@link Attribute#GENERIC_MAX_HEALTH}
 */
public static boolean heal(@NotNull LivingEntity target, double amount, EntityRegainHealthEvent.RegainReason reason) {
    if (target.isDead() || amount <= 0) {
        return false;
    }
    final AttributeInstance attribute = target.getAttribute(Attribute.GENERIC_MAX_HEALTH);
    if (attribute == null) {
        throw new IllegalArgumentException("The specified LivingEntity has no " + Attribute.GENERIC_MAX_HEALTH + " attribute");
    }
    final double maxHealth = attribute.getValue();
    final double currentHealth = target.getHealth();
    if (currentHealth >= maxHealth) {
        // already max health
        return false;
    }
    EntityRegainHealthEvent event = new EntityRegainHealthEvent(target, amount, reason);
    EventFactory.getInstance().callEvent(event);
    if (event.isCancelled() || event.getAmount() <= 0) {
        return false;
    }
    target.setHealth(Math.min(maxHealth, currentHealth + event.getAmount()));
    return true;
}
Also used : EntityRegainHealthEvent(org.bukkit.event.entity.EntityRegainHealthEvent) AttributeInstance(org.bukkit.attribute.AttributeInstance)

Example 12 with AttributeInstance

use of org.bukkit.attribute.AttributeInstance in project Prism-Bukkit by prism.

the class AbstractHorseSerializer method serializer.

@Override
protected void serializer(Entity entity) {
    final AbstractHorse h = (AbstractHorse) entity;
    // TODO: Cleanup
    if (entity.getType() == EntityType.HORSE) {
        Horse horse = (Horse) h;
        horseColor = horse.getColor().name();
        style = horse.getStyle().name();
        saddle = ItemUtils.smallString(horse.getInventory().getSaddle());
        armor = ItemUtils.smallString(horse.getInventory().getArmor());
    } else if (entity.getType() == EntityType.LLAMA) {
        Llama llama = (Llama) h;
        horseColor = llama.getColor().name();
        saddle = ItemUtils.smallString(llama.getInventory().getDecor());
    } else if (entity.getType() == EntityType.MULE || entity.getType() == EntityType.DONKEY || entity.getType() == EntityType.ZOMBIE_HORSE || entity.getType() == EntityType.SKELETON_HORSE) {
        // Actually a saddle
        saddle = ItemUtils.smallString(h.getInventory().getItem(0));
    }
    if (entity instanceof ChestedHorse) {
        chest = ((ChestedHorse) entity).isCarryingChest();
    }
    dom = h.getDomestication();
    maxDom = h.getMaxDomestication();
    jump = h.getJumpStrength();
    maxHealth = h.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue();
    AttributeInstance attributeInstance = h.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
    if (attributeInstance != null) {
        movementSpeed = attributeInstance.getBaseValue();
    }
}
Also used : AbstractHorse(org.bukkit.entity.AbstractHorse) Llama(org.bukkit.entity.Llama) ChestedHorse(org.bukkit.entity.ChestedHorse) Horse(org.bukkit.entity.Horse) AbstractHorse(org.bukkit.entity.AbstractHorse) ChestedHorse(org.bukkit.entity.ChestedHorse) AttributeInstance(org.bukkit.attribute.AttributeInstance)

Example 13 with AttributeInstance

use of org.bukkit.attribute.AttributeInstance in project NoCheatPlus by NoCheatPlus.

the class BukkitAttributeAccess method getSprintAttributeMultiplier.

@Override
public double getSprintAttributeMultiplier(final Player player) {
    final AttributeInstance attrInst = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
    final AttributeModifier mod = getModifier(attrInst, AttribUtil.ID_SPRINT_BOOST);
    return mod == null ? 1.0 : getMultiplier(mod);
}
Also used : AttributeInstance(org.bukkit.attribute.AttributeInstance) AttributeModifier(org.bukkit.attribute.AttributeModifier)

Example 14 with AttributeInstance

use of org.bukkit.attribute.AttributeInstance in project NoCheatPlus by NoCheatPlus.

the class BukkitAttributeAccess method getSpeedAttributeMultiplier.

@Override
public double getSpeedAttributeMultiplier(final Player player) {
    final AttributeInstance attrInst = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
    final double val = attrInst.getValue() / attrInst.getBaseValue();
    final AttributeModifier mod = getModifier(attrInst, AttribUtil.ID_SPRINT_BOOST);
    return mod == null ? val : (val / getMultiplier(mod));
}
Also used : AttributeInstance(org.bukkit.attribute.AttributeInstance) AttributeModifier(org.bukkit.attribute.AttributeModifier)

Example 15 with AttributeInstance

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

Aggregations

AttributeInstance (org.bukkit.attribute.AttributeInstance)23 LivingEntity (org.bukkit.entity.LivingEntity)8 Attribute (org.bukkit.attribute.Attribute)5 AttributeModifier (org.bukkit.attribute.AttributeModifier)5 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 MapTag (com.denizenscript.denizencore.objects.core.MapTag)3 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)3 Attributable (org.bukkit.attribute.Attributable)3 Horse (org.bukkit.entity.Horse)3 Player (org.bukkit.entity.Player)3 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 ISoliniaLivingEntity (com.solinia.solinia.Interfaces.ISoliniaLivingEntity)2 ISoliniaPlayer (com.solinia.solinia.Interfaces.ISoliniaPlayer)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 DamageSource (net.minecraft.world.damagesource.DamageSource)2 net.minecraft.world.entity (net.minecraft.world.entity)2