Skip to content

Commit

Permalink
Parse attributes differently
Browse files Browse the repository at this point in the history
attributes are now parsed like this instead of in strings:

attributes:
  - GENERIC_KNOCKBACK_RESISTANCE:
      name: Knockback resistance
      value: 0.5
      equipmentSlot: FEET
      operation: MULTIPLY_SCALAR_1
  - GENERIC_MOVEMENT_SPEED:
      name: Speed boost
      value: 0.2
      equipmentSlot: LEGS
      operation: ADD_SCALAR
  - GENERIC_ARMOR:
      name: Extra armor
      value: 3
      equipmentSlot: CHEST
  - GENERIC_ARMOR_TOUGHNESS:
      name: Armor toughness
      value: 2
      equipmentSlot: HEAD
      operation: ADD_NUMBER
  • Loading branch information
btarg committed Dec 15, 2023
1 parent 3bba9bb commit 9122ec7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
build
.idea/
*/.idea/
run
run
bin
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public CustomItemDefinition(Map<String, Object> map) {
Bukkit.getLogger().severe("Custom Items need a base material! Defaulting to a Command Block...");
this.baseMaterial = Material.COMMAND_BLOCK;
}
this.attributes = AttributeParser.parseAttributes(Objects.requireNonNullElse((List<String>) map.get("attributes"), new ArrayList<>()));
this.durability = Objects.requireNonNullElse((Integer) map.get("durability"), (int) this.baseMaterial.getMaxDurability());
this.attributes = AttributeParser.parseAttributes(Objects.requireNonNullElse((List<Map<String, Map<String, Object>>>) map.get("attributes"), new ArrayList<>())); this.durability = Objects.requireNonNullElse((Integer) map.get("durability"), (int) this.baseMaterial.getMaxDurability());
this.enchantments = EnchantmentParser.parseEnchantments(Objects.requireNonNullElse((List<String>) map.get("enchantments"), new ArrayList<>()));
this.flags = deserializeFlags(map);
this.potionEffects = PotionEffectParser.parsePotionEffects(Objects.requireNonNullElse((List<String>) map.get("potionEffects"), new ArrayList<>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,29 @@
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.inventory.EquipmentSlot;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;

public class AttributeParser {

private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile("([A-Za-z_]+)\\(([^)]+)\\)");

public static Map<Attribute, Map<UUID, AttributeModifier>> parseAttributes(List<String> attributeStrings) {
if (attributeStrings == null) return new HashMap<>();

Map<Attribute, Map<UUID, AttributeModifier>> parsedAttributes = new HashMap<>();

for (String attributeString : attributeStrings) {
Matcher matcher = ATTRIBUTE_PATTERN.matcher(attributeString.trim());
if (matcher.matches()) {
String attributeName = matcher.group(1);
String attributeParams = matcher.group(2);

Attribute attribute = Attribute.valueOf(attributeName.toUpperCase());
Map<UUID, AttributeModifier> attributeModifiers = parseAttributeModifiers(attributeParams);

parsedAttributes.put(attribute, attributeModifiers);
} else {
throw new IllegalArgumentException("Invalid attribute format: " + attributeString);
}
}

return parsedAttributes;
}

private static Map<UUID, AttributeModifier> parseAttributeModifiers(String attributeParams) {
Map<UUID, AttributeModifier> attributeModifiers = new HashMap<>();

String[] modifierParts = attributeParams.split(",");
for (String modifierPart : modifierParts) {
String[] parts = modifierPart.trim().split(":");
if (parts.length == 2 || parts.length == 3) {
String modifierName = parts[0].trim();
double value = Double.parseDouble(parts[1].trim());

EquipmentSlot equipmentSlot = EquipmentSlot.HAND; // Default to HAND
if (parts.length == 3) {
equipmentSlot = EquipmentSlot.valueOf(parts[2].trim().toUpperCase());
}

AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), modifierName, value, AttributeModifier.Operation.ADD_NUMBER, equipmentSlot);
attributeModifiers.put(modifier.getUniqueId(), modifier);
} else {
throw new IllegalArgumentException("Invalid attribute modifier format: " + modifierPart);
public static Map<Attribute, Map<UUID, AttributeModifier>> parseAttributes(List<Map<String, Map<String, Object>>> attributeList) {
Map<Attribute, Map<UUID, AttributeModifier>> attributes = new HashMap<>();

for (Map<String, Map<String, Object>> attributeMap : attributeList) {
for (Map.Entry<String, Map<String, Object>> entry : attributeMap.entrySet()) {
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());

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

return attributeModifiers;
return attributes;
}
}
}

0 comments on commit 9122ec7

Please sign in to comment.