Search in sources :

Example 1 with AttributeInstance

use of org.bukkit.attribute.AttributeInstance in project Minigames by AddstarMC.

the class MinigamePlayerManager method revertToCheckpoint.

public void revertToCheckpoint(MinigamePlayer player) {
    RevertCheckpointEvent event = new RevertCheckpointEvent(player);
    Bukkit.getServer().getPluginManager().callEvent(event);
    if (!event.isCancelled()) {
        player.teleport(player.getCheckpoint());
        player.addRevert();
        player.sendInfoMessage(MinigameUtils.getLang("player.checkpoint.revert"));
        // Reset the player's health and extinguish flames when they revert
        Player p = player.getPlayer();
        if ((p != null) && (p.isOnline())) {
            p.setFireTicks(0);
            AttributeInstance maxHealth = p.getAttribute(Attribute.GENERIC_MAX_HEALTH);
            if (maxHealth != null) {
                p.setHealth(maxHealth.getValue());
            }
            p.setFoodLevel(20);
            p.setSaturation(20f);
            p.setRemainingAir(p.getMaximumAir());
        }
    }
}
Also used : Player(org.bukkit.entity.Player) MinigamePlayer(au.com.mineauz.minigames.objects.MinigamePlayer) AttributeInstance(org.bukkit.attribute.AttributeInstance)

Example 2 with AttributeInstance

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

the class EntityAttributeModifiers method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
        try {
            MapTag input = mechanism.valueAsType(MapTag.class);
            Attributable ent = getAttributable();
            for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
                Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                    continue;
                }
                for (AttributeModifier modifier : instance.getModifiers()) {
                    instance.removeModifier(modifier);
                }
                for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
                    instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
                }
            }
        } catch (Throwable ex) {
            Debug.echoError(ex);
        }
    }
    // -->
    if (mechanism.matches("add_attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
        try {
            MapTag input = mechanism.valueAsType(MapTag.class);
            Attributable ent = getAttributable();
            for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
                Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                    continue;
                }
                for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
                    instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
                }
            }
        } catch (Throwable ex) {
            Debug.echoError(ex);
        }
    }
    // -->
    if (mechanism.matches("remove_attribute_modifiers") && mechanism.requireObject(ListTag.class)) {
        ArrayList<String> inputList = new ArrayList<>(mechanism.valueAsType(ListTag.class));
        Attributable ent = getAttributable();
        for (String toRemove : new ArrayList<>(inputList)) {
            if (new ElementTag(toRemove).matchesEnum(Attribute.class)) {
                inputList.remove(toRemove);
                Attribute attr = Attribute.valueOf(toRemove.toUpperCase());
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                    continue;
                }
                for (AttributeModifier modifier : instance.getModifiers()) {
                    instance.removeModifier(modifier);
                }
            }
        }
        for (String toRemove : inputList) {
            UUID id = UUID.fromString(toRemove);
            for (Attribute attr : Attribute.values()) {
                AttributeInstance instance = ent.getAttribute(attr);
                if (instance == null) {
                    continue;
                }
                for (AttributeModifier modifer : instance.getModifiers()) {
                    if (modifer.getUniqueId().equals(id)) {
                        instance.removeModifier(modifer);
                        break;
                    }
                }
            }
        }
    }
    if (mechanism.matches("attributes") && mechanism.hasValue()) {
        Deprecations.legacyAttributeProperties.warn(mechanism.context);
        Attributable ent = getAttributable();
        ListTag list = mechanism.valueAsType(ListTag.class);
        for (String str : list) {
            List<String> subList = CoreUtilities.split(str, '/');
            Attribute attr = Attribute.valueOf(EscapeTagBase.unEscape(subList.get(0)).toUpperCase());
            AttributeInstance instance = ent.getAttribute(attr);
            if (instance == null) {
                mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                continue;
            }
            instance.setBaseValue(Double.parseDouble(subList.get(1)));
            for (AttributeModifier modifier : instance.getModifiers()) {
                instance.removeModifier(modifier);
            }
            for (int x = 2; x < subList.size(); x += 4) {
                String slot = subList.get(x + 3).toUpperCase();
                AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), EscapeTagBase.unEscape(subList.get(x)), Double.parseDouble(subList.get(x + 1)), AttributeModifier.Operation.valueOf(subList.get(x + 2).toUpperCase()), slot.equals("ANY") ? null : EquipmentSlot.valueOf(slot));
                instance.addModifier(modifier);
            }
        }
    }
}
Also used : Attribute(org.bukkit.attribute.Attribute) ArrayList(java.util.ArrayList) AttributeModifier(org.bukkit.attribute.AttributeModifier) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) Attributable(org.bukkit.attribute.Attributable) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) AttributeInstance(org.bukkit.attribute.AttributeInstance) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID) Map(java.util.Map)

Example 3 with AttributeInstance

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

the class EntityAttributeBaseValues method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <EntityTag.has_attribute[<attribute>]>
    // @returns ElementTag(Boolean)
    // @group properties
    // @description
    // Returns whether the entity has the named attribute.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "has_attribute", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.has_attribute[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        return new ElementTag(object.getAttributable().getAttribute(attr) != null);
    });
    // <--[tag]
    // @attribute <EntityTag.attribute_value[<attribute>]>
    // @returns ElementTag(Decimal)
    // @mechanism EntityTag.attribute_base_values
    // @group properties
    // @description
    // Returns the final calculated value of the named attribute for the entity.
    // See also <@link tag EntityTag.has_attribute>.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "attribute_value", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.attribute_value[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        AttributeInstance instance = object.getAttributable().getAttribute(attr);
        if (instance == null) {
            attribute.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + object.entity.getBukkitEntityType().name());
            return null;
        }
        return new ElementTag(instance.getValue());
    });
    // <--[tag]
    // @attribute <EntityTag.attribute_base_value[<attribute>]>
    // @returns ElementTag(Decimal)
    // @mechanism EntityTag.attribute_base_values
    // @group properties
    // @description
    // Returns the base value of the named attribute for the entity.
    // See also <@link tag EntityTag.has_attribute>.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "attribute_base_value", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.attribute_base_value[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        AttributeInstance instance = object.getAttributable().getAttribute(attr);
        if (instance == null) {
            attribute.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + object.entity.getBukkitEntityType().name());
            return null;
        }
        return new ElementTag(instance.getBaseValue());
    });
    // <--[tag]
    // @attribute <EntityTag.attribute_default_value[<attribute>]>
    // @returns ElementTag(Decimal)
    // @mechanism EntityTag.attribute_base_values
    // @group properties
    // @description
    // Returns the default value of the named attribute for the entity.
    // See also <@link tag EntityTag.has_attribute>.
    // Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
    // See also <@link language attribute modifiers>.
    // -->
    PropertyParser.<EntityAttributeBaseValues, ElementTag>registerTag(ElementTag.class, "attribute_default_value", (attribute, object) -> {
        if (!(attribute.hasParam() && attribute.getParamElement().matchesEnum(Attribute.class))) {
            attribute.echoError("Invalid entity.attribute_default_value[...] input: must be a valid attribute name.");
            return null;
        }
        Attribute attr = Attribute.valueOf(attribute.getParam().toUpperCase());
        AttributeInstance instance = object.getAttributable().getAttribute(attr);
        if (instance == null) {
            attribute.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + object.entity.getBukkitEntityType().name());
            return null;
        }
        return new ElementTag(instance.getDefaultValue());
    });
}
Also used : Attribute(org.bukkit.attribute.Attribute) AttributeInstance(org.bukkit.attribute.AttributeInstance) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 4 with AttributeInstance

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

the class EntityHelperImpl method getDamageTo.

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
    EnumMonsterType monsterType;
    if (target instanceof LivingEntity) {
        monsterType = ((CraftLivingEntity) target).getHandle().getMonsterType();
    } else {
        monsterType = EnumMonsterType.UNDEFINED;
    }
    double damage = 0;
    AttributeInstance attrib = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
    if (attrib != null) {
        damage = attrib.getValue();
    }
    if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
        damage += EnchantmentManager.a(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
    }
    if (damage <= 0) {
        return 0;
    }
    if (target != null) {
        DamageSource source;
        if (attacker instanceof Player) {
            source = DamageSource.playerAttack(((CraftPlayer) attacker).getHandle());
        } else {
            source = DamageSource.mobAttack(((CraftLivingEntity) attacker).getHandle());
        }
        net.minecraft.server.v1_16_R3.Entity nmsTarget = ((CraftEntity) target).getHandle();
        if (nmsTarget.isInvulnerable(source)) {
            return 0;
        }
        if (!(nmsTarget instanceof EntityLiving)) {
            return damage;
        }
        EntityLiving livingTarget = (EntityLiving) nmsTarget;
        damage = CombatMath.a((float) damage, (float) livingTarget.getArmorStrength(), (float) livingTarget.getAttributeInstance(GenericAttributes.ARMOR_TOUGHNESS).getValue());
        int enchantDamageModifier = EnchantmentManager.a(livingTarget.getArmorItems(), source);
        if (enchantDamageModifier > 0) {
            damage = CombatMath.a((float) damage, (float) enchantDamageModifier);
        }
    }
    return damage;
}
Also used : net.minecraft.server.v1_16_R3(net.minecraft.server.v1_16_R3) AttributeInstance(org.bukkit.attribute.AttributeInstance)

Example 5 with AttributeInstance

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

the class EntityHelperImpl method getDamageTo.

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
    MobType monsterType;
    if (target instanceof LivingEntity) {
        monsterType = ((CraftLivingEntity) target).getHandle().getMobType();
    } else {
        monsterType = MobType.UNDEFINED;
    }
    double damage = 0;
    AttributeInstance attrib = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
    if (attrib != null) {
        damage = attrib.getValue();
    }
    if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
        damage += EnchantmentHelper.getDamageBonus(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
    }
    if (damage <= 0) {
        return 0;
    }
    if (target != null) {
        DamageSource source;
        if (attacker instanceof Player) {
            source = DamageSource.playerAttack(((CraftPlayer) attacker).getHandle());
        } else {
            source = DamageSource.mobAttack(((CraftLivingEntity) attacker).getHandle());
        }
        net.minecraft.world.entity.Entity nmsTarget = ((CraftEntity) target).getHandle();
        if (nmsTarget.isInvulnerableTo(source)) {
            return 0;
        }
        if (!(nmsTarget instanceof net.minecraft.world.entity.LivingEntity)) {
            return damage;
        }
        net.minecraft.world.entity.LivingEntity livingTarget = (net.minecraft.world.entity.LivingEntity) nmsTarget;
        damage = CombatRules.getDamageAfterAbsorb((float) damage, (float) livingTarget.getArmorValue(), (float) livingTarget.getAttributeValue(Attributes.ARMOR_TOUGHNESS));
        int enchantDamageModifier = EnchantmentHelper.getDamageProtection(livingTarget.getArmorSlots(), source);
        if (enchantDamageModifier > 0) {
            damage = CombatRules.getDamageAfterMagicAbsorb((float) damage, (float) enchantDamageModifier);
        }
    }
    return damage;
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) DamageSource(net.minecraft.world.damagesource.DamageSource) LivingEntity(org.bukkit.entity.LivingEntity) AttributeInstance(org.bukkit.attribute.AttributeInstance) org.bukkit.entity(org.bukkit.entity) org.bukkit.craftbukkit.v1_18_R1.entity(org.bukkit.craftbukkit.v1_18_R1.entity) net.minecraft.world.entity(net.minecraft.world.entity)

Aggregations

AttributeInstance (org.bukkit.attribute.AttributeInstance)15 Attribute (org.bukkit.attribute.Attribute)5 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 AttributeModifier (org.bukkit.attribute.AttributeModifier)4 MapTag (com.denizenscript.denizencore.objects.core.MapTag)3 Attributable (org.bukkit.attribute.Attributable)3 LivingEntity (org.bukkit.entity.LivingEntity)3 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 Map (java.util.Map)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 DamageSource (net.minecraft.world.damagesource.DamageSource)2 net.minecraft.world.entity (net.minecraft.world.entity)2 org.bukkit.entity (org.bukkit.entity)2 AbstractHorse (org.bukkit.entity.AbstractHorse)2 ChestedHorse (org.bukkit.entity.ChestedHorse)2 Horse (org.bukkit.entity.Horse)2 Llama (org.bukkit.entity.Llama)2 Player (org.bukkit.entity.Player)2