Search in sources :

Example 66 with Attribute

use of org.bukkit.attribute.Attribute in project Magma-1.16.x by magmafoundation.

the class CraftMetaItem method applyModifiers.

static void applyModifiers(Multimap<Attribute, AttributeModifier> modifiers, CompoundNBT tag, ItemMetaKey key) {
    if (modifiers == null || modifiers.isEmpty()) {
        return;
    }
    ListNBT list = new ListNBT();
    for (Map.Entry<Attribute, AttributeModifier> entry : modifiers.entries()) {
        if (entry.getKey() == null || entry.getValue() == null) {
            continue;
        }
        net.minecraft.entity.ai.attributes.AttributeModifier nmsModifier = CraftAttributeInstance.convert(entry.getValue());
        CompoundNBT sub = nmsModifier.save();
        if (sub.isEmpty()) {
            continue;
        }
        String name = entry.getKey().getKey().toString();
        if (name == null || name.isEmpty()) {
            continue;
        }
        // Attribute Name
        sub.putString(ATTRIBUTES_IDENTIFIER.NBT, name);
        if (entry.getValue().getSlot() != null) {
            EquipmentSlotType slot = CraftEquipmentSlot.getNMS(entry.getValue().getSlot());
            if (slot != null) {
                sub.putString(ATTRIBUTES_SLOT.NBT, slot.getName());
            }
        }
        list.add(sub);
    }
    tag.put(key.NBT, list);
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) Attribute(org.bukkit.attribute.Attribute) EquipmentSlotType(net.minecraft.inventory.EquipmentSlotType) AttributeModifier(org.bukkit.attribute.AttributeModifier) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CraftAttributeMap(org.bukkit.craftbukkit.v1_16_R3.attribute.CraftAttributeMap)

Example 67 with Attribute

use of org.bukkit.attribute.Attribute in project Magma-1.16.x by magmafoundation.

the class CraftMetaItem method buildModifiers.

static Multimap<Attribute, AttributeModifier> buildModifiers(CompoundNBT tag, ItemMetaKey key) {
    Multimap<Attribute, AttributeModifier> modifiers = LinkedHashMultimap.create();
    if (!tag.contains(key.NBT, CraftMagicNumbers.NBT.TAG_LIST)) {
        return modifiers;
    }
    ListNBT mods = tag.getList(key.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
    int size = mods.size();
    for (int i = 0; i < size; i++) {
        CompoundNBT entry = mods.getCompound(i);
        if (entry.isEmpty()) {
            // entry is not an actual CompoundNBT. getCompound returns empty CompoundNBT in that case
            continue;
        }
        net.minecraft.entity.ai.attributes.AttributeModifier nmsModifier = net.minecraft.entity.ai.attributes.AttributeModifier.load(entry);
        if (nmsModifier == null) {
            continue;
        }
        AttributeModifier attribMod = CraftAttributeInstance.convert(nmsModifier);
        String attributeName = entry.getString(ATTRIBUTES_IDENTIFIER.NBT);
        if (attributeName == null || attributeName.isEmpty()) {
            continue;
        }
        Attribute attribute = CraftAttributeMap.fromMinecraft(attributeName);
        if (attribute == null) {
            continue;
        }
        if (entry.contains(ATTRIBUTES_SLOT.NBT, CraftMagicNumbers.NBT.TAG_STRING)) {
            String slotName = entry.getString(ATTRIBUTES_SLOT.NBT);
            if (slotName == null || slotName.isEmpty()) {
                modifiers.put(attribute, attribMod);
                continue;
            }
            EquipmentSlot slot = null;
            try {
                slot = CraftEquipmentSlot.getSlot(EquipmentSlotType.byName(slotName.toLowerCase(Locale.ROOT)));
            } catch (IllegalArgumentException ex) {
            // SPIGOT-4551 - Slot is invalid, should really match nothing but this is undefined behaviour anyway
            }
            if (slot == null) {
                modifiers.put(attribute, attribMod);
                continue;
            }
            attribMod = new AttributeModifier(attribMod.getUniqueId(), attribMod.getName(), attribMod.getAmount(), attribMod.getOperation(), slot);
        }
        modifiers.put(attribute, attribMod);
    }
    return modifiers;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) Attribute(org.bukkit.attribute.Attribute) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) CraftEquipmentSlot(org.bukkit.craftbukkit.v1_16_R3.CraftEquipmentSlot) AttributeModifier(org.bukkit.attribute.AttributeModifier) ListNBT(net.minecraft.nbt.ListNBT)

Example 68 with Attribute

use of org.bukkit.attribute.Attribute in project LoliServer by Loli-Server.

the class CraftMetaItem method applyModifiers.

static void applyModifiers(Multimap<Attribute, AttributeModifier> modifiers, CompoundNBT tag, ItemMetaKey key) {
    if (modifiers == null || modifiers.isEmpty()) {
        return;
    }
    ListNBT list = new ListNBT();
    for (Map.Entry<Attribute, AttributeModifier> entry : modifiers.entries()) {
        if (entry.getKey() == null || entry.getValue() == null) {
            continue;
        }
        net.minecraft.entity.ai.attributes.AttributeModifier nmsModifier = CraftAttributeInstance.convert(entry.getValue());
        CompoundNBT sub = nmsModifier.save();
        if (sub.isEmpty()) {
            continue;
        }
        String name = entry.getKey().getKey().toString();
        if (name == null || name.isEmpty()) {
            continue;
        }
        // Attribute Name
        sub.putString(ATTRIBUTES_IDENTIFIER.NBT, name);
        if (entry.getValue().getSlot() != null) {
            EquipmentSlotType slot = CraftEquipmentSlot.getNMS(entry.getValue().getSlot());
            if (slot != null) {
                sub.putString(ATTRIBUTES_SLOT.NBT, slot.getName());
            }
        }
        list.add(sub);
    }
    tag.put(key.NBT, list);
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) Attribute(org.bukkit.attribute.Attribute) EquipmentSlotType(net.minecraft.inventory.EquipmentSlotType) AttributeModifier(org.bukkit.attribute.AttributeModifier) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CraftAttributeMap(org.bukkit.craftbukkit.v1_16_R3.attribute.CraftAttributeMap)

Example 69 with Attribute

use of org.bukkit.attribute.Attribute in project LevelTools by byteful.

the class PDCLevelToolsItem method getItemStack.

@Override
@NotNull
public ItemStack getItemStack() {
    final ItemStack stack = LevelToolsUtil.buildItemStack(this.stack, enchantments, getLevel(), getXp(), getMaxXp());
    attributes.forEach((attribute, modifier) -> {
        final ItemMeta meta = stack.getItemMeta();
        assert meta != null : "ItemMeta is null! Should not happen.";
        final Attribute attr = Attribute.valueOf(attribute.replace(".", "_").toUpperCase(Locale.ROOT).trim());
        final AttributeModifier mod = new AttributeModifier(attribute, modifier, AttributeModifier.Operation.ADD_NUMBER);
        meta.addAttributeModifier(attr, mod);
        stack.setItemMeta(meta);
    });
    return stack;
}
Also used : Attribute(org.bukkit.attribute.Attribute) ItemStack(org.bukkit.inventory.ItemStack) AttributeModifier(org.bukkit.attribute.AttributeModifier) ItemMeta(org.bukkit.inventory.meta.ItemMeta) NotNull(org.jetbrains.annotations.NotNull)

Example 70 with Attribute

use of org.bukkit.attribute.Attribute in project proskillapi by promcteam.

the class PlayerData method scaleStat.

/**
 * Scales a stat value using the player's attributes
 *
 * @param stat         stat key
 * @param defaultValue the default value come with vanilla Minecraft, <strong>Only needed for custom stats and Speed</strong>
 * @param min          min value
 * @param max          max value
 * @return modified value
 */
public double scaleStat(String stat, double defaultValue, double min, double max) {
    Player player = this.getPlayer();
    if (player != null) {
        if (!SkillAPI.getSettings().isWorldEnabled(player.getWorld())) {
            return defaultValue;
        }
    }
    final AttributeManager manager = SkillAPI.getAttributeManager();
    if (manager == null) {
        return defaultValue;
    }
    double modified = defaultValue;
    final List<AttributeManager.Attribute> matches = manager.forStat(stat);
    if (matches != null) {
        for (final AttributeManager.Attribute attribute : matches) {
            int amount = this.getAttribute(attribute.getKey());
            if (amount > 0) {
                modified = attribute.modifyStat(stat, modified, amount);
            }
        }
    }
    // Stats come with modifier api
    if (this.statModifiers.containsKey(stat)) {
        double multiplier = 1;
        for (PlayerStatModifier modifier : this.getStatModifiers(stat)) {
            switch(modifier.getOperation()) {
                case ADD_NUMBER:
                    modified = modifier.applyOn(modified);
                    break;
                case MULTIPLY_PERCENTAGE:
                    multiplier = modifier.applyOn(multiplier);
                    break;
            }
        }
        modified = modified * multiplier;
    }
    return Math.max(min, Math.min(max, modified));
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) AttributeManager(com.sucy.skill.manager.AttributeManager) Attribute(org.bukkit.attribute.Attribute) NBTAttribute(mc.promcteam.engine.api.meta.NBTAttribute)

Aggregations

Attribute (org.bukkit.attribute.Attribute)75 AttributeModifier (org.bukkit.attribute.AttributeModifier)50 ItemMeta (org.bukkit.inventory.meta.ItemMeta)25 ArrayList (java.util.ArrayList)19 Map (java.util.Map)19 AttributeInstance (org.bukkit.attribute.AttributeInstance)18 ItemStack (org.bukkit.inventory.ItemStack)16 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)13 HashMap (java.util.HashMap)12 List (java.util.List)12 UUID (java.util.UUID)12 Enchantment (org.bukkit.enchantments.Enchantment)12 PotionEffect (org.bukkit.potion.PotionEffect)11 Material (org.bukkit.Material)9 Attributable (org.bukkit.attribute.Attributable)9 Player (org.bukkit.entity.Player)9 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)8 Pattern (org.bukkit.block.banner.Pattern)8 EntityType (org.bukkit.entity.EntityType)8 BannerMeta (org.bukkit.inventory.meta.BannerMeta)8