-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemColor.java
131 lines (114 loc) · 4.66 KB
/
ItemColor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
public class ItemColor implements Property {
public static boolean describes(ObjectTag item) {
// Leather armor and potions
return item instanceof ItemTag
&& (((ItemTag) item).getItemStack().getType() == Material.LEATHER_BOOTS
|| ((ItemTag) item).getItemStack().getType() == Material.LEATHER_CHESTPLATE
|| ((ItemTag) item).getItemStack().getType() == Material.LEATHER_HELMET
|| ((ItemTag) item).getItemStack().getType() == Material.LEATHER_LEGGINGS
|| ((ItemTag) item).getItemStack().getType() == Material.POTION
|| ((ItemTag) item).getItemStack().getType() == Material.SPLASH_POTION
|| ((ItemTag) item).getItemStack().getType() == Material.LINGERING_POTION);
}
public static ItemColor getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemColor((ItemTag) _item);
}
}
public static final String[] handledTags = new String[] {
"color", "dye_color"
};
public static final String[] handledMechs = new String[] {
"color", "dye_color", "dye"
};
private ItemColor(ItemTag _item) {
item = _item;
}
ItemTag item;
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <ItemTag.color>
// @returns ColorTag
// @mechanism ItemTag.color
// @group properties
// @description
// Returns the color of the leather armor item or potion item.
// -->
if (attribute.startsWith("color") || attribute.startsWith("dye_color")) {
Material mat = item.getItemStack().getType();
if (mat == Material.POTION
|| mat == Material.LINGERING_POTION
|| mat == Material.SPLASH_POTION) {
PotionMeta pm = (PotionMeta) item.getItemMeta();
if (!pm.hasColor()) {
return new ColorTag(Color.WHITE).getObjectAttribute(attribute.fulfill((1)));
}
return new ColorTag(pm.getColor()).getObjectAttribute(attribute.fulfill((1)));
}
return new ColorTag(((LeatherArmorMeta) item.getItemMeta()).getColor()).getObjectAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public String getPropertyString() {
Material mat = item.getItemStack().getType();
if (mat == Material.POTION
|| mat == Material.LINGERING_POTION
|| mat == Material.SPLASH_POTION) {
PotionMeta pm = (PotionMeta) item.getItemMeta();
if (!pm.hasColor()) {
return null;
}
return new ColorTag(pm.getColor()).identify();
}
return new ColorTag(((LeatherArmorMeta) item.getItemMeta()).getColor()).identify();
}
@Override
public String getPropertyId() {
return "color";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name color
// @input ColorTag
// @description
// Sets the leather armor item's dye color or the potion item's color.
// @tags
// <ItemTag.color>
// -->
if ((mechanism.matches("dye") || mechanism.matches("dye_color")
|| mechanism.matches("color")) && (mechanism.requireObject(ColorTag.class))) {
ColorTag color = mechanism.valueAsType(ColorTag.class);
Material mat = item.getItemStack().getType();
if (mat == Material.POTION || mat == Material.LINGERING_POTION || mat == Material.SPLASH_POTION) {
PotionMeta meta = (PotionMeta) item.getItemMeta();
meta.setColor(color.getColor());
item.setItemMeta(meta);
return;
}
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
meta.setColor(color.getColor());
item.setItemMeta(meta);
}
}
}