use of org.bukkit.attribute.AttributeModifier in project Arcade2 by ShootGame.
the class AttributeModifierParser method parsePrimitive.
@Override
protected ParserResult<AttributeModifier> parsePrimitive(Node node, String name, String value) throws ParserException {
AttributeModifier.Operation operation = this.operationParser.parse(node.property("operation")).orDefault(AttributeModifier.Operation.ADD_NUMBER);
double amount = this.amountParser.parseWithDefinition(node, name, value).orFail();
UUID uniqueId = this.fastUUID.next();
return ParserResult.fine(node, name, value, new AttributeModifier(uniqueId, this.computeName(uniqueId, operation, amount), amount, operation));
}
use of org.bukkit.attribute.AttributeModifier in project Arcade2 by ShootGame.
the class BoundedModifierParser method parseNode.
@Override
protected ParserResult<BoundedModifier> parseNode(Node node, String name, String value) throws ParserException {
AttributeKey key = this.keyParser.parse(node.property("attribute", "attribute-key", "attributekey", "attr", "key")).orFail();
AttributeModifier modifier = this.modifierParser.parseWithDefinition(node, name, value).orFail();
return ParserResult.fine(node, name, value, new BoundedModifier(key, modifier));
}
use of org.bukkit.attribute.AttributeModifier in project Glowstone by GlowstoneMC.
the class EntityPropertyCodec method encode.
@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;
}
use of org.bukkit.attribute.AttributeModifier in project Denizen-For-Bukkit by DenizenScript.
the class EntityAttributeModifiers method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
try {
MapTag input = mechanism.valueAsType(MapTag.class);
Attributable ent = getAttributable();
for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
for (AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}
for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
}
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
}
// -->
if (mechanism.matches("add_attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
try {
MapTag input = mechanism.valueAsType(MapTag.class);
Attributable ent = getAttributable();
for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
}
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
}
// -->
if (mechanism.matches("remove_attribute_modifiers") && mechanism.requireObject(ListTag.class)) {
ArrayList<String> inputList = new ArrayList<>(mechanism.valueAsType(ListTag.class));
Attributable ent = getAttributable();
for (String toRemove : new ArrayList<>(inputList)) {
if (new ElementTag(toRemove).matchesEnum(Attribute.class)) {
inputList.remove(toRemove);
Attribute attr = Attribute.valueOf(toRemove.toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
for (AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}
}
}
for (String toRemove : inputList) {
UUID id = UUID.fromString(toRemove);
for (Attribute attr : Attribute.values()) {
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
continue;
}
for (AttributeModifier modifer : instance.getModifiers()) {
if (modifer.getUniqueId().equals(id)) {
instance.removeModifier(modifer);
break;
}
}
}
}
}
if (mechanism.matches("attributes") && mechanism.hasValue()) {
Deprecations.legacyAttributeProperties.warn(mechanism.context);
Attributable ent = getAttributable();
ListTag list = mechanism.valueAsType(ListTag.class);
for (String str : list) {
List<String> subList = CoreUtilities.split(str, '/');
Attribute attr = Attribute.valueOf(EscapeTagBase.unEscape(subList.get(0)).toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
instance.setBaseValue(Double.parseDouble(subList.get(1)));
for (AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}
for (int x = 2; x < subList.size(); x += 4) {
String slot = subList.get(x + 3).toUpperCase();
AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), EscapeTagBase.unEscape(subList.get(x)), Double.parseDouble(subList.get(x + 1)), AttributeModifier.Operation.valueOf(subList.get(x + 2).toUpperCase()), slot.equals("ANY") ? null : EquipmentSlot.valueOf(slot));
instance.addModifier(modifier);
}
}
}
}
use of org.bukkit.attribute.AttributeModifier in project Denizen-For-Bukkit by DenizenScript.
the class EntityAttributeModifiers method modiferForMap.
public static AttributeModifier modiferForMap(Attribute attr, MapTag map) {
ObjectTag name = map.getObject("name");
ObjectTag amount = map.getObject("amount");
ObjectTag operation = map.getObject("operation");
ObjectTag slot = map.getObject("slot");
ObjectTag id = map.getObject("id");
AttributeModifier.Operation operationValue;
EquipmentSlot slotValue;
UUID idValue;
double amountValue;
try {
operationValue = AttributeModifier.Operation.valueOf(operation.toString().toUpperCase());
} catch (IllegalArgumentException ex) {
Debug.echoError("Attribute modifier operation '" + operation + "' does not exist.");
return null;
}
try {
idValue = id == null ? UUID.randomUUID() : UUID.fromString(id.toString());
} catch (IllegalArgumentException ex) {
Debug.echoError("Attribute modifier ID '" + id + "' is not a valid UUID.");
return null;
}
try {
slotValue = slot == null || CoreUtilities.equalsIgnoreCase(slot.toString(), "any") ? null : EquipmentSlot.valueOf(slot.toString().toUpperCase());
} catch (IllegalArgumentException ex) {
Debug.echoError("Attribute modifier slot '" + slot + "' is not a valid slot.");
return null;
}
try {
amountValue = Double.parseDouble(amount.toString());
} catch (NumberFormatException ex) {
Debug.echoError("Attribute modifier amount '" + amount + "' is not a valid decimal number.");
return null;
}
return new AttributeModifier(idValue, name == null ? attr.name() : name.toString(), amountValue, operationValue, slotValue);
}
Aggregations