-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemTrim.java
79 lines (70 loc) · 3.46 KB
/
ItemTrim.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import org.bukkit.Registry;
import org.bukkit.inventory.meta.ArmorMeta;
import org.bukkit.inventory.meta.trim.ArmorTrim;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.bukkit.inventory.meta.trim.TrimPattern;
public class ItemTrim extends ItemProperty<MapTag> {
// <--[property]
// @object ItemTag
// @name trim
// @input MapTag
// @description
// Controls an armor item's trim.
// Allowed keys: material, pattern.
// Valid material inputs can be found here: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/trim/TrimMaterial.html>
// Valid pattern inputs can be found here: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/trim/TrimPattern.html>
// Valid values also include ones added by datapacks, plugins, etc. as a namespaced key.
// For the mechanism, if an item already has a trim, you can omit either material or pattern to keep the original data while also changing the other option.
// For example, if you only want to change the pattern and not the material, you can omit the material, and it will use the already existing material.
// -->
public static boolean describes(ItemTag item) {
return item.getItemMeta() instanceof ArmorMeta;
}
@Override
public MapTag getPropertyValue() {
ArmorTrim currentTrim = ((ArmorMeta) getItemMeta()).getTrim();
if (currentTrim == null) {
return null;
}
MapTag map = new MapTag();
map.putObject("material", new ElementTag(Utilities.namespacedKeyToString(currentTrim.getMaterial().getKey()), true));
map.putObject("pattern", new ElementTag(Utilities.namespacedKeyToString(currentTrim.getPattern().getKey()), true));
return map;
}
@Override
public boolean isDefaultValue(MapTag map) {
return map.isEmpty();
}
@Override
public String getPropertyId() {
return "trim";
}
@Override
public void setPropertyValue(MapTag map, Mechanism mechanism) {
ElementTag mat = map.getElement("material");
ElementTag pat = map.getElement("pattern");
ArmorMeta meta = (ArmorMeta) getItemMeta();
ArmorTrim currentTrim = meta.getTrim();
if (mat == null && currentTrim == null) {
mechanism.echoError("The armor piece must have a material already if you want to omit it!");
return;
}
if (pat == null && currentTrim == null) {
mechanism.echoError("The armor piece must have a pattern already if you want to omit it!");
return;
}
TrimMaterial material = mat == null ? currentTrim.getMaterial() : Registry.TRIM_MATERIAL.get(Utilities.parseNamespacedKey(mat.asString()));
TrimPattern pattern = pat == null ? currentTrim.getPattern() : Registry.TRIM_PATTERN.get(Utilities.parseNamespacedKey(pat.asString()));
meta.setTrim(new ArmorTrim(material, pattern));
setItemMeta(meta);
}
public static void register() {
autoRegister("trim", ItemTrim.class, MapTag.class, false);
}
}