mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-30 03:43:51 -07:00
SPIGOT-1916: Attribute modifiers for ItemStacks
By: Senmori <thesenmori@gmail.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.util.NumberConversions;
|
||||
|
||||
/**
|
||||
@@ -16,20 +17,25 @@ public class AttributeModifier implements ConfigurationSerializable {
|
||||
private final String name;
|
||||
private final double amount;
|
||||
private final Operation operation;
|
||||
private final EquipmentSlot slot;
|
||||
|
||||
public AttributeModifier(String name, double amount, Operation operation) {
|
||||
this(UUID.randomUUID(), name, amount, operation);
|
||||
}
|
||||
|
||||
public AttributeModifier(UUID uuid, String name, double amount, Operation operation) {
|
||||
Validate.notNull(uuid, "uuid");
|
||||
Validate.notEmpty(name, "Name cannot be empty");
|
||||
Validate.notNull(operation, "operation");
|
||||
this(uuid, name, amount, operation, null);
|
||||
}
|
||||
|
||||
public AttributeModifier(UUID uuid, String name, double amount, Operation operation, EquipmentSlot slot) {
|
||||
Validate.notNull(uuid, "UUID cannot be null");
|
||||
Validate.notEmpty(name, "Name cannot be empty");
|
||||
Validate.notNull(operation, "Operation cannot be null");
|
||||
this.uuid = uuid;
|
||||
this.name = name;
|
||||
this.amount = amount;
|
||||
this.operation = operation;
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,6 +74,16 @@ public class AttributeModifier implements ConfigurationSerializable {
|
||||
return operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link EquipmentSlot} this AttributeModifier is active on,
|
||||
* or null if this modifier is applicable for any slot.
|
||||
*
|
||||
* @return the slot
|
||||
*/
|
||||
public EquipmentSlot getSlot() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> data = new HashMap<String, Object>();
|
||||
@@ -75,10 +91,37 @@ public class AttributeModifier implements ConfigurationSerializable {
|
||||
data.put("name", name);
|
||||
data.put("operation", operation.ordinal());
|
||||
data.put("amount", amount);
|
||||
if (slot != null) {
|
||||
data.put("slot", slot.name());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof AttributeModifier)) {
|
||||
return false;
|
||||
}
|
||||
AttributeModifier mod = (AttributeModifier) other;
|
||||
boolean slots = (this.slot != null ? (this.slot == mod.slot) : mod.slot != null);
|
||||
return this.uuid.equals(mod.uuid) && this.name.equals(mod.name) && this.amount == mod.amount && this.operation == mod.operation && slots;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AttributeModifier{"
|
||||
+ "uuid=" + this.uuid.toString()
|
||||
+ ", name=" + this.name
|
||||
+ ", operation=" + this.operation.name()
|
||||
+ ", amount=" + this.amount
|
||||
+ ", slot=" + (this.slot != null ? this.slot.name() : "")
|
||||
+ "}";
|
||||
}
|
||||
|
||||
public static AttributeModifier deserialize(Map<String, Object> args) {
|
||||
if (args.containsKey("slot")) {
|
||||
return new AttributeModifier(UUID.fromString((String) args.get("uuid")), (String) args.get("name"), NumberConversions.toDouble(args.get("amount")), Operation.values()[NumberConversions.toInt(args.get("operation"))], EquipmentSlot.valueOf((args.get("slot").toString().toUpperCase())));
|
||||
}
|
||||
return new AttributeModifier(UUID.fromString((String) args.get("uuid")), (String) args.get("name"), NumberConversions.toDouble(args.get("amount")), Operation.values()[NumberConversions.toInt(args.get("operation"))]);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user