-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of attributes
- Loading branch information
Showing
3 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/io/github/btarg/origami/util/parsers/AttributeParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.btarg.origami.util.parsers; | ||
|
||
import org.bukkit.attribute.Attribute; | ||
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; | ||
|
||
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); | ||
} | ||
} | ||
|
||
return attributeModifiers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters