Skip to content

Commit

Permalink
Attribute serialization
Browse files Browse the repository at this point in the history
why not?
  • Loading branch information
btarg committed Dec 15, 2023
1 parent 9122ec7 commit efadb00
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ protected ItemMeta getItemMeta(ItemStack itemStack) {
}
}
meta.addItemFlags(this.flags.toArray(new ItemFlag[0]));
//TODO: add attributes here
for (var entry : this.attributes.entrySet()) {
Attribute attribute = entry.getKey();
Map<UUID, AttributeModifier> attributeModifiers = entry.getValue();
Expand Down Expand Up @@ -112,6 +111,7 @@ public void registerDefinition(CommandSender sender) {
List<String> potionEffectStrings = this.potionEffects.stream()
.map(effect -> effect.getType() + "(" + effect.getDuration() + ", " + effect.getAmplifier() + ")")
.toList();
map.put("attributes", AttributeParser.serializeAttributes(this.attributes));

map.put("enchantments", enchantmentStrings);
map.put("potionEffects", potionEffectStrings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,36 @@ public static Map<Attribute, Map<UUID, AttributeModifier>> parseAttributes(List<
Attribute attribute = Attribute.valueOf(entry.getKey());
Map<String, Object> attributeParams = entry.getValue();

String modifierName = (String) attributeParams.get("name");
double value = Double.parseDouble(String.valueOf(attributeParams.get("value")));
EquipmentSlot equipmentSlot = Objects.requireNonNullElse(EquipmentSlot.valueOf(((String) attributeParams.get("equipmentSlot")).toUpperCase()), EquipmentSlot.HAND);
String operationStr = (String) attributeParams.getOrDefault("operation", "ADD_NUMBER");
AttributeModifier.Operation operation = AttributeModifier.Operation.valueOf(operationStr.toUpperCase());

String modifierName = Optional.ofNullable((String) attributeParams.get("name")).filter(s -> !s.isBlank()).orElse(UUID.randomUUID().toString());
double value = Optional.ofNullable(attributeParams.get("value")).map(Object::toString).map(Double::parseDouble).orElse(0.0);
EquipmentSlot equipmentSlot = EquipmentSlot.valueOf(Optional.ofNullable((String) attributeParams.get("equipmentSlot")).orElse("HAND").toUpperCase());
AttributeModifier.Operation operation = AttributeModifier.Operation.valueOf(Optional.ofNullable((String) attributeParams.get("operation")).orElse("ADD_NUMBER").toUpperCase());

AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), modifierName, value, operation, equipmentSlot);
attributes.put(attribute, Map.of(modifier.getUniqueId(), modifier));
}
}

return attributes;
}

public static List<Map<String, Map<String, Object>>> serializeAttributes(Map<Attribute, Map<UUID, AttributeModifier>> attributes) {
List<Map<String, Map<String, Object>>> attributeList = new ArrayList<>();

for (Map.Entry<Attribute, Map<UUID, AttributeModifier>> attributeEntry : attributes.entrySet()) {
for (Map.Entry<UUID, AttributeModifier> modifierEntry : attributeEntry.getValue().entrySet()) {
AttributeModifier modifier = modifierEntry.getValue();

Map<String, Object> attributeParams = new HashMap<>();
attributeParams.put("name", modifier.getName());
attributeParams.put("value", modifier.getAmount());
attributeParams.put("equipmentSlot", modifier.getSlot().name());
attributeParams.put("operation", modifier.getOperation().name());

attributeList.add(Map.of(attributeEntry.getKey().name(), attributeParams));
}
}

return attributeList;
}
}

0 comments on commit efadb00

Please sign in to comment.