private Builder(WrappedAttribute template) { if (template != null) { baseValue = template.getBaseValue(); attributeKey = template.getAttributeKey(); packet = template.getParentPacket(); modifiers = template.getModifiers(); } }
/** * Construct a new wrapped attribute around a specific NMS instance. * @param handle - handle to a NMS AttributeSnapshot. * @return The attribute wrapper. * @throws IllegalArgumentException If the handle is not a AttributeSnapshot. */ public static WrappedAttribute fromHandle(@Nonnull Object handle) { return new WrappedAttribute(handle); }
/** * Set the amount to modify in the operation. * * @param amount - the amount to supply to the new object. * @return This builder, for chaining. */ public Builder amount(double amount) { this.amount = WrappedAttribute.checkDouble(amount); return this; }
@Override public String toString() { return "WrappedAttribute[key=" + getAttributeKey() + ", base=" + getBaseValue() + ", final=" + getFinalValue() + ", modifiers=" + getModifiers() + "]"; }
@Override public int hashCode() { if (attributeModifiers == null) getModifiers(); return Objects.hashCode(getAttributeKey(), getBaseValue(), attributeModifiers); }
if (attribute.getAttributeKey().equals("generic.maxHealth")) { packets.clear(); builder = WrappedAttribute.newBuilder(); builder.attributeKey("generic.maxHealth"); builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth()); } else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity()) { builder = WrappedAttribute.newBuilder(attribute); } else { builder = WrappedAttribute.newBuilder(); builder.attributeKey("generic.maxHealth"); builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth());
/** * Construct an attribute with the same key and name, but a different list of modifiers. * @param modifiers - attribute modifiers. * @return The new attribute. */ public WrappedAttribute withModifiers(Collection<WrappedAttributeModifier> modifiers) { return newBuilder(this).modifiers(modifiers).build(); }
/** * Retrieve an attribute modifier by UUID. * @param id - the id to look for. * @return The single attribute modifier with the given ID. */ public WrappedAttributeModifier getModifierByUUID(UUID id) { if (hasModifier(id)) { for (WrappedAttributeModifier modifier : getModifiers()) { if (Objects.equal(modifier.getUUID(), id)) { return modifier; } } } return null; }
Collection<WrappedAttributeModifier> modifiers = getModifiers(); double x = getBaseValue(); double y = 0;
/** * Retrieve the final computed value. * @return The final value. */ public double getFinalValue() { if (Double.isNaN(computedValue)) { computedValue = computeValue(); } return computedValue; }
/** * Determine if the attribute has a given attribute modifier, identified by UUID. * @param id - the id to check for. * @return TRUE if it does, FALSE otherwise. */ public boolean hasModifier(UUID id) { return getModifiers().contains(WrappedAttributeModifier.newBuilder(id).build()); }
/** * Retrieve an immutable set of all the attribute modifiers that will compute the final value of this attribute. * @return Every attribute modifier. */ public Set<WrappedAttributeModifier> getModifiers() { if (attributeModifiers == null) { @SuppressWarnings("unchecked") Collection<Object> collection = (Collection<Object>) modifier.withType(Collection.class).read(0); // Convert to an equivalent wrapper ConvertedSet<Object, WrappedAttributeModifier> converted = new ConvertedSet<Object, WrappedAttributeModifier>(getSetSafely(collection)) { @Override protected Object toInner(WrappedAttributeModifier outer) { return outer.getHandle(); } @Override protected WrappedAttributeModifier toOuter(Object inner) { return WrappedAttributeModifier.fromHandle(inner); } }; attributeModifiers = new CachedSet<WrappedAttributeModifier>(converted); } return Collections.unmodifiableSet(attributeModifiers); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj instanceof WrappedAttribute) { WrappedAttribute other = (WrappedAttribute) obj; if (getBaseValue() == other.getBaseValue() && Objects.equal(getAttributeKey(), other.getAttributeKey())) { return getModifiers().stream() .filter((elem) -> !other.getModifiers().contains(elem)) .count() == 0; } } return false; }
builder = WrappedAttribute.newBuilder(); builder.attributeKey("generic.maxHealth"); builder.baseValue(getMaxHealth());
WrappedAttribute.Builder builder = WrappedAttribute.newBuilder().attributeKey("generic.maxHealth");
return new WrappedAttribute(handle);
/** * Change the base value of the attribute. * <p> * The modifiers will automatically supply a value if this is unset. * @param baseValue - the base value value. * @return This builder, for chaining. */ public Builder baseValue(double baseValue) { this.baseValue = checkDouble(baseValue); return this; }