Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/main/java/studio/magemonkey/divinity/modules/ModuleItem.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package studio.magemonkey.divinity.modules;

import org.apache.commons.lang3.ArrayUtils;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.*;
import org.jetbrains.annotations.NotNull;
import studio.magemonkey.codex.Codex;
import studio.magemonkey.codex.CodexEngine;
import studio.magemonkey.codex.api.items.ItemType;
import studio.magemonkey.codex.api.items.exception.MissingItemException;
import studio.magemonkey.codex.api.items.exception.MissingProviderException;
import studio.magemonkey.codex.api.items.providers.VanillaProvider;
import studio.magemonkey.codex.api.meta.NBTAttribute;
import studio.magemonkey.codex.compat.VersionManager;
import studio.magemonkey.codex.config.api.JYML;
import studio.magemonkey.codex.manager.LoadableItem;
import studio.magemonkey.codex.util.ItemUT;
Expand All @@ -33,6 +36,7 @@
import studio.magemonkey.divinity.stats.items.ItemTags;
import studio.magemonkey.divinity.stats.items.requirements.ItemRequirements;
import studio.magemonkey.divinity.stats.items.requirements.user.UntradeableRequirement;
import studio.magemonkey.divinity.utils.ItemUtils;
import studio.magemonkey.divinity.utils.LoreUT;

import java.util.*;
Expand All @@ -51,6 +55,8 @@ public abstract class ModuleItem extends LoadableItem {
protected Set<ItemFlag> flags;
protected boolean isUnbreakable;
protected Map<Enchantment, Integer> enchants;
protected String armorTrim;
protected Map<Attribute, AttributeModifier> attributes;

// Creating new config
@Deprecated
Expand Down Expand Up @@ -99,6 +105,7 @@ public ModuleItem(@NotNull Divinity plugin, @NotNull JYML cfg, @NotNull QModuleD

this.enchanted = cfg.getBoolean("enchanted");
this.hash = cfg.getString("skull-hash");
this.armorTrim = cfg.getString("armor-trim");

this.flags = new HashSet<>();
for (String flag : cfg.getStringList("item-flags")) {
Expand Down Expand Up @@ -128,6 +135,21 @@ public ModuleItem(@NotNull Divinity plugin, @NotNull JYML cfg, @NotNull QModuleD
this.enchants.put(en, level);
}

this.attributes = new HashMap<>();
for (String attr : cfg.getSection("attributes")) {
String[] attrData = cfg.getString("attributes." + attr, "").split(":");
double value = Double.parseDouble(attrData[0]);
String operation = attrData.length > 1 ? attrData[1] : "ADD_NUMBER";
AttributeModifier attrModifier = VersionManager.getCompat().createAttributeModifier(NBTAttribute.valueOf(attr.toUpperCase()), value, AttributeModifier.Operation.valueOf(operation));
if(attrModifier == null) {
Codex.warn("Invalid attribute provided: " + attr + " (" + cfg.getFile().getName() + ")");
continue;
}
// Todo find a robust solution one day
Attribute attribute = ItemUtils.resolveAttribute(attrModifier.getName());
this.attributes.put(attribute, attrModifier);
}

cfg.saveChanges();
}

Expand Down Expand Up @@ -234,9 +256,18 @@ protected ItemStack build(@NotNull ItemStack item) {
if (meta instanceof LeatherArmorMeta) {
LeatherArmorMeta lm = (LeatherArmorMeta) meta;
lm.setColor(Color.fromRGB(r, g, b));
if(this.armorTrim != null) {
String[] trimData = this.armorTrim.split(":");
VersionManager.getArmorUtil().addTrim(meta, trimData[0].toLowerCase(), trimData[1].toLowerCase());
}
} else if (meta instanceof PotionMeta) {
PotionMeta pm = (PotionMeta) meta;
pm.setColor(Color.fromRGB(r, g, b));
} else if(meta instanceof ArmorMeta) {
if(this.armorTrim != null) {
String[] trimData = this.armorTrim.split(":");
VersionManager.getArmorUtil().addTrim(meta, trimData[0].toLowerCase(), trimData[1].toLowerCase());
}
}
}

Expand All @@ -246,6 +277,11 @@ protected ItemStack build(@NotNull ItemStack item) {
meta.addEnchant(NamespaceResolver.getEnchantment("POWER", "ARROW_DAMAGE"), 1, true); // ARROW_DAMAGE/POWER
}

for(Map.Entry<Attribute, AttributeModifier> attribute : this.attributes.entrySet()) {
if (attribute != null) {
meta.addAttributeModifier(attribute.getKey(), attribute.getValue());
}
}
item.setItemMeta(meta);

for (Map.Entry<Enchantment, Integer> e : this.enchants.entrySet()) {
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/studio/magemonkey/divinity/utils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.apache.commons.lang3.ArrayUtils;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
Expand Down Expand Up @@ -397,4 +397,43 @@ public static void setColor(@NotNull ItemStack item, @NotNull Color c) {

item.setItemMeta(meta2);
}

// TODO might want to consider distributing this into CodexCore
public static Attribute resolveAttribute(String keyAttr) {
Registry<Attribute> reg = Bukkit.getRegistry(Attribute.class);
if (reg == null) return null;

// 1) Accept full namespaced keys, e.g. "minecraft:generic.max_health"
NamespacedKey nk = NamespacedKey.fromString(keyAttr);
if (nk != null) {
Attribute a = reg.get(nk);
if (a != null) return a;
}

// 2) Accept simple forms like "GENERIC_MAX_HEALTH" or "MAX_HEALTH"
String simple = keyAttr.toLowerCase();
// normalize known GENERIC_ prefix and dots
String normalized = simple
.replace("generic_", "generic.")
.replace('_', '.');

// try minecraft: prefix
NamespacedKey guess = NamespacedKey.fromString("minecraft:" + normalized);
if (guess != null) {
Attribute a = reg.get(guess);
if (a != null) return a;
}

// 3) Last resort: scan registry and match by end segment / legacy names
for (Attribute a : reg) {
String s = a.getKey().getKey(); // e.g. "minecraft:generic.max_health"
if (s.equalsIgnoreCase(keyAttr) ||
s.equalsIgnoreCase(normalized) ||
s.endsWith("." + normalized) ||
s.toUpperCase().endsWith("_" + keyAttr.toUpperCase()) ) {
return a;
}
}
return null;
}
}