/
ItemPatterns.java
125 lines (106 loc) · 4.15 KB
/
ItemPatterns.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
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.inventory.meta.BannerMeta;
import java.util.ArrayList;
import java.util.List;
public class ItemPatterns implements Property {
public static boolean describes(dObject item) {
if (item instanceof dItem) {
Material material = ((dItem) item).getItemStack().getType();
return material == Material.BANNER
|| material == Material.WALL_BANNER
|| material == Material.STANDING_BANNER;
}
return false;
}
public static ItemPatterns getFrom(dObject item) {
if (!describes(item)) return null;
else return new ItemPatterns((dItem) item);
}
private ItemPatterns(dItem item) {
this.item = item;
}
dItem item;
private dList listPatterns() {
dList list = new dList();
for (Pattern pattern : ((BannerMeta) item.getItemStack().getItemMeta()).getPatterns()) {
list.add(pattern.getColor().name() + "/" + pattern.getPattern().name());
}
return list;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
// <--[tag]
// @attribute <i@item.patterns>
// @returns dList
// @group properties
// @mechanism dItem.patterns
// @description
// Lists a banner's patterns in the form "li@COLOR/PATTERN|COLOR/PATTERN" etc.
// TODO: Local meta for these links
// For the list of possible colors, see <@link url http://bit.ly/1dydq12>.
// For the list of possible patterns, see <@link url http://bit.ly/1MqRn7T>.
// -->
if (attribute.startsWith("patterns")) {
return listPatterns().getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public String getPropertyString() {
dList list = listPatterns();
if (list.isEmpty()) {
return null;
}
return list.identify();
}
@Override
public String getPropertyId() {
return "patterns";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dItem
// @name patterns
// @input dList
// @description
// Changes the patterns of a banner. Input must be in the form
// "li@COLOR/PATTERN|COLOR/PATTERN" etc.
// For the list of possible colors, see <@link url http://bit.ly/1dydq12>.
// For the list of possible patterns, see <@link url http://bit.ly/1MqRn7T>.
// @tags
// <i@item.patterns>
// -->
if (mechanism.matches("patterns")) {
List<Pattern> patterns = new ArrayList<Pattern>();
dList list = mechanism.getValue().asType(dList.class);
List<String> split;
for (String string : list) {
try {
split = CoreUtilities.split(string, '/', 2);
patterns.add(new Pattern(DyeColor.valueOf(split.get(0).toUpperCase()),
PatternType.valueOf(split.get(1).toUpperCase())));
}
catch (Exception e) {
dB.echoError("Could not apply pattern to banner: " + string);
}
}
BannerMeta bannerMeta = (BannerMeta) item.getItemStack().getItemMeta();
bannerMeta.setPatterns(patterns);
item.getItemStack().setItemMeta(bannerMeta);
}
}
}