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);
}
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;
}
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);
}
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;
}
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));
}
Aggregations