Skip to content

Commit

Permalink
Initial implementation of attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
btarg committed Dec 12, 2023
1 parent b230051 commit 7254fa7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import io.github.btarg.origami.resourcepack.ResourcePackGenerator;
import io.github.btarg.origami.util.ComponentHelper;
import io.github.btarg.origami.util.NamespacedKeyHelper;
import io.github.btarg.origami.util.parsers.AttributeParser;
import io.github.btarg.origami.util.parsers.EnchantmentParser;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
Expand All @@ -22,6 +25,7 @@
@SuppressWarnings("unchecked")
public class CustomItemDefinition extends BaseCustomDefinition {
public Map<Enchantment, Integer> enchantments;
public Map<Attribute, Map<UUID, AttributeModifier>> attributes;
public List<ItemFlag> flags = new ArrayList<>();
public List<PotionEffectType> potionEffects = new ArrayList<>();
public Integer durability;
Expand All @@ -34,7 +38,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.enchantments = EnchantmentParser.parseEnchantments(Objects.requireNonNullElse((List<String>) map.get("enchantments"), new ArrayList<>()));
this.flags = deserializeFlags(map);
Expand Down Expand Up @@ -86,8 +90,16 @@ protected ItemMeta getItemMeta(ItemStack itemStack) {
meta.addEnchant(key, value, true);
}
}
//TODO: add attributes here
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();

for (AttributeModifier modifier : attributeModifiers.values()) {
meta.addAttributeModifier(attribute, modifier);
}
}

return meta;
}
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.bukkit.inventory.ItemStack;

public class ItemParser {
public static ItemStack parseItemStack(String input) {
public static ItemStack parseItemStack(String input) throws IllegalArgumentException {
// Split the input string by "(" and ")"
String[] parts = input.trim().split("\\(");

Expand Down

0 comments on commit 7254fa7

Please sign in to comment.