Skip to content

Commit 47e2f64

Browse files
MaksyKunTravja
andauthored
Add attribute and armor trim support for customitems module (#300)
* added attribute and armor trim support for customitems * lightweighed attribute storage * optimized imports * Formatting --------- Co-authored-by: Travja <the.only.t.craft@gmail.com>
1 parent ecbac3b commit 47e2f64

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

src/main/java/studio/magemonkey/divinity/hooks/external/FabledHook.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.jetbrains.annotations.NotNull;
1515
import studio.magemonkey.codex.hooks.HookState;
1616
import studio.magemonkey.codex.hooks.NHook;
17-
import studio.magemonkey.codex.util.DataUT;
1817
import studio.magemonkey.codex.util.StringUT;
1918
import studio.magemonkey.divinity.Divinity;
2019
import studio.magemonkey.divinity.config.EngineCfg;

src/main/java/studio/magemonkey/divinity/hooks/external/LevelledMobsHK.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package studio.magemonkey.divinity.hooks.external;
22

33
import org.bukkit.Bukkit;
4-
import org.bukkit.persistence.PersistentDataType;
5-
import org.bukkit.plugin.Plugin;
64
import org.bukkit.NamespacedKey;
75
import org.bukkit.entity.Entity;
86
import org.bukkit.entity.LivingEntity;
7+
import org.bukkit.persistence.PersistentDataType;
8+
import org.bukkit.plugin.Plugin;
99
import org.jetbrains.annotations.NotNull;
1010
import studio.magemonkey.codex.hooks.HookState;
1111
import studio.magemonkey.codex.hooks.NHook;

src/main/java/studio/magemonkey/divinity/modules/ModuleItem.java

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
import org.bukkit.Color;
55
import org.bukkit.Material;
66
import org.bukkit.NamespacedKey;
7+
import org.bukkit.attribute.Attribute;
8+
import org.bukkit.attribute.AttributeModifier;
79
import org.bukkit.configuration.InvalidConfigurationException;
810
import org.bukkit.enchantments.Enchantment;
911
import org.bukkit.inventory.ItemFlag;
1012
import org.bukkit.inventory.ItemStack;
11-
import org.bukkit.inventory.meta.Damageable;
12-
import org.bukkit.inventory.meta.ItemMeta;
13-
import org.bukkit.inventory.meta.LeatherArmorMeta;
14-
import org.bukkit.inventory.meta.PotionMeta;
13+
import org.bukkit.inventory.meta.*;
1514
import org.jetbrains.annotations.NotNull;
15+
import studio.magemonkey.codex.Codex;
1616
import studio.magemonkey.codex.CodexEngine;
1717
import studio.magemonkey.codex.api.items.ItemType;
1818
import studio.magemonkey.codex.api.items.exception.MissingItemException;
1919
import studio.magemonkey.codex.api.items.exception.MissingProviderException;
2020
import studio.magemonkey.codex.api.items.providers.VanillaProvider;
21+
import studio.magemonkey.codex.api.meta.NBTAttribute;
22+
import studio.magemonkey.codex.compat.VersionManager;
2123
import studio.magemonkey.codex.config.api.JYML;
2224
import studio.magemonkey.codex.manager.LoadableItem;
2325
import studio.magemonkey.codex.util.ItemUT;
@@ -38,19 +40,21 @@
3840
import java.util.*;
3941

4042
public abstract class ModuleItem extends LoadableItem {
41-
protected final QModuleDrop<?> module;
42-
protected Divinity plugin;
43-
protected String name;
44-
protected ItemType material;
45-
protected List<String> lore;
46-
protected int modelData;
47-
protected int durability;
48-
protected int[] color;
49-
protected boolean enchanted;
50-
protected String hash;
51-
protected Set<ItemFlag> flags;
52-
protected boolean isUnbreakable;
53-
protected Map<Enchantment, Integer> enchants;
43+
protected final QModuleDrop<?> module;
44+
protected Divinity plugin;
45+
protected String name;
46+
protected ItemType material;
47+
protected List<String> lore;
48+
protected int modelData;
49+
protected int durability;
50+
protected int[] color;
51+
protected boolean enchanted;
52+
protected String hash;
53+
protected Set<ItemFlag> flags;
54+
protected boolean isUnbreakable;
55+
protected Map<Enchantment, Integer> enchants;
56+
protected String armorTrim;
57+
protected Map<Attribute, AttributeModifier> attributes;
5458

5559
// Creating new config
5660
@Deprecated
@@ -99,6 +103,7 @@ public ModuleItem(@NotNull Divinity plugin, @NotNull JYML cfg, @NotNull QModuleD
99103

100104
this.enchanted = cfg.getBoolean("enchanted");
101105
this.hash = cfg.getString("skull-hash");
106+
this.armorTrim = cfg.getString("armor-trim");
102107

103108
this.flags = new HashSet<>();
104109
for (String flag : cfg.getStringList("item-flags")) {
@@ -128,6 +133,21 @@ public ModuleItem(@NotNull Divinity plugin, @NotNull JYML cfg, @NotNull QModuleD
128133
this.enchants.put(en, level);
129134
}
130135

136+
this.attributes = new HashMap<>();
137+
for (String attr : cfg.getSection("attributes")) {
138+
String[] attrData = cfg.getString("attributes." + attr, "").split(":");
139+
double value = Double.parseDouble(attrData[0]);
140+
String operation = attrData.length > 1 ? attrData[1] : "ADD_NUMBER";
141+
NBTAttribute nbtAttr = NBTAttribute.valueOf(attr.toUpperCase());
142+
AttributeModifier attrModifier = VersionManager.getCompat()
143+
.createAttributeModifier(nbtAttr, value, AttributeModifier.Operation.valueOf(operation));
144+
if (attrModifier == null) {
145+
Codex.warn("Invalid attribute provided: " + attr + " (" + cfg.getFile().getName() + ")");
146+
continue;
147+
}
148+
this.attributes.put(nbtAttr.getAttribute(), attrModifier);
149+
}
150+
131151
cfg.saveChanges();
132152
}
133153

@@ -234,9 +254,18 @@ protected ItemStack build(@NotNull ItemStack item) {
234254
if (meta instanceof LeatherArmorMeta) {
235255
LeatherArmorMeta lm = (LeatherArmorMeta) meta;
236256
lm.setColor(Color.fromRGB(r, g, b));
257+
if (this.armorTrim != null) {
258+
String[] trimData = this.armorTrim.split(":");
259+
VersionManager.getArmorUtil().addTrim(meta, trimData[0].toLowerCase(), trimData[1].toLowerCase());
260+
}
237261
} else if (meta instanceof PotionMeta) {
238262
PotionMeta pm = (PotionMeta) meta;
239263
pm.setColor(Color.fromRGB(r, g, b));
264+
} else if (meta instanceof ArmorMeta) {
265+
if (this.armorTrim != null) {
266+
String[] trimData = this.armorTrim.split(":");
267+
VersionManager.getArmorUtil().addTrim(meta, trimData[0].toLowerCase(), trimData[1].toLowerCase());
268+
}
240269
}
241270
}
242271

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

278+
for (Map.Entry<Attribute, AttributeModifier> attribute : this.attributes.entrySet()) {
279+
if (attribute != null) {
280+
meta.addAttributeModifier(attribute.getKey(), attribute.getValue());
281+
}
282+
}
249283
item.setItemMeta(meta);
250284

251285
for (Map.Entry<Enchantment, Integer> e : this.enchants.entrySet()) {

0 commit comments

Comments
 (0)