/** * Construct an attribute modifier wrapper around a given NMS instance. * @param handle - the NMS instance. * @return The created attribute modifier. * @throws IllegalArgumentException If the handle is not an AttributeModifier. */ public static WrappedAttributeModifier fromHandle(@Nonnull Object handle) { return new WrappedAttributeModifier(handle); }
@Override protected WrappedAttributeModifier toOuter(Object inner) { return WrappedAttributeModifier.fromHandle(inner); } };
private Builder(WrappedAttributeModifier template) { if (template != null) { operation = template.getOperation(); name = template.getName(); amount = template.getAmount(); uuid = template.getUUID(); } }
/** * Construct an attribute modifier wrapper around a NMS instance. * @param handle - the NMS instance. * @param uuid - the UUID. * @param name - the human readable name. * @param amount - the amount. * @param operation - the operation. */ protected WrappedAttributeModifier(@Nonnull Object handle, UUID uuid, String name, double amount, Operation operation) { this(uuid, name, amount, operation); // Initialize handle and modifier setHandle(handle); initializeModifier(handle); }
if (modifier.getOperation().getId() == phase) { switch (phase) { case 0: // Adding phase x += modifier.getAmount(); break; case 1: // Multiply percentage y += x * modifier.getAmount(); break; case 2: y *= 1 + modifier.getAmount(); break; default :
@Override protected Object toInner(WrappedAttributeModifier outer) { return outer.getHandle(); }
/** * Determine if a given modifier is equal to the current modifier. * <p> * Two modifiers are considered equal if they use the same UUID. * @param obj - the object to check against. * @return TRUE if the given object is the same, FALSE otherwise. */ public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof WrappedAttributeModifier) { WrappedAttributeModifier other = (WrappedAttributeModifier) obj; // Ensure they are equal return Objects.equal(uuid, other.getUUID()); } return false; }
@Override public String toString() { return "[amount=" + amount + ", operation=" + operation + ", name='" + name + "', id=" + uuid + ", serialize=" + isPendingSynchronization() + "]"; }
/** * 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()); }
/** * Construct an attribute modifier wrapper around a given NMS instance. * @param handle - the NMS instance. */ @SuppressWarnings("unchecked") protected WrappedAttributeModifier(@Nonnull Object handle) { // Update handle and modifier super(MinecraftReflection.getAttributeModifierClass()); setHandle(handle); initializeModifier(handle); // Load final values, caching them this.uuid = (UUID) modifier.withType(UUID.class).read(0); StructureModifier<String> stringMod = modifier.withType(String.class); if (stringMod.size() == 0) { Supplier<String> supplier = (Supplier<String>) modifier.withType(Supplier.class).read(0); this.name = supplier; } else { this.name = () -> stringMod.read(0); } this.amount = (Double) modifier.withType(double.class).read(0); this.operation = Operation.fromId((Integer) modifier.withType(int.class).read(0)); }
/** * Retrieve the unwrapped modifiers. * @return Unwrapped modifiers. */ private Set<Object> getUnwrappedModifiers() { Set<Object> output = Sets.newHashSet(); for (WrappedAttributeModifier modifier : modifiers) { output.add(modifier.getHandle()); } return output; }
/** * 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; }
return new WrappedAttributeModifier( ATTRIBUTE_MODIFIER_CONSTRUCTOR.newInstance( uuid, name, amount, operation.getId()),