/** * Remove an attribute from this property. * * @param attributeModifier to remove from this property */ @Override public void removeModifier(AttributeModifier attributeModifier) { this.modifiers.remove(attributeModifier.getUniqueId()); onMutation(); }
/** * Add a modifier to this property. * * <p> * Attributes with the same uuid will be overridden according to * https://minecraft.gamepedia.com/Attribute#Attributes * </p> * * @param attributeModifier to add to this property */ @Override public void addModifier(AttributeModifier attributeModifier) { this.modifiers.put(attributeModifier.getUniqueId(), attributeModifier); onMutation(); }
@Override public ByteBuf encode(ByteBuf buf, EntityPropertyMessage message) throws IOException { ByteBufUtils.writeVarInt(buf, message.getId()); Map<String, Property> props = message.getProperties(); buf.writeInt(props.size()); for (Entry<String, Property> property : props.entrySet()) { ByteBufUtils.writeUTF8(buf, property.getKey()); buf.writeDouble(property.getValue().getValue()); Collection<AttributeModifier> modifiers = property.getValue().getModifiers(); if (modifiers == null) { ByteBufUtils.writeVarInt(buf, 0); } else { ByteBufUtils.writeVarInt(buf, modifiers.size()); for (AttributeModifier modifier : modifiers) { GlowBufUtils.writeUuid(buf, modifier.getUniqueId()); buf.writeDouble(modifier.getAmount()); buf.writeByte(modifier.getOperation().ordinal()); } } } return buf; } }
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());
/** * The first modifier with the given id that can be found, or null if none * is found. * * @param attrInst * @param id * @return */ private AttributeModifier getModifier(final AttributeInstance attrInst, final UUID id) { for (final AttributeModifier mod : attrInst.getModifiers()) { if (id.equals(mod.getUniqueId())) { return mod; } } return null; }
private ItemMeta refreshAttributeModifiers(ItemMeta itemMeta) { List<PowerAttributeModifier> attributeModifiers = getPower(PowerAttributeModifier.class); if (!attributeModifiers.isEmpty()) { Multimap<Attribute, AttributeModifier> old = itemMeta.getAttributeModifiers(); for (PowerAttributeModifier attributeModifier : attributeModifiers) { Attribute attribute = attributeModifier.attribute; UUID uuid = new UUID(attributeModifier.uuidMost, attributeModifier.uuidLeast); AttributeModifier modifier = new AttributeModifier( uuid, attributeModifier.name, attributeModifier.amount, attributeModifier.operation, attributeModifier.slot ); if (old != null) { old.entries().stream().filter(m -> m.getValue().getUniqueId().equals(uuid)).findAny().ifPresent( e -> itemMeta.removeAttributeModifier(e.getKey(), e.getValue()) ); } itemMeta.addAttributeModifier(attribute, modifier); } } return itemMeta; }