/
BukkitListProperties.java
118 lines (107 loc) · 4.24 KB
/
BukkitListProperties.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
package com.denizenscript.denizen.objects.properties.bukkit;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.ChatColor;
public class BukkitListProperties implements Property {
public static boolean describes(ObjectTag list) {
return list instanceof ListTag;
}
public static BukkitListProperties getFrom(ObjectTag list) {
if (!describes(list)) {
return null;
}
else {
return new BukkitListProperties((ListTag) list);
}
}
private BukkitListProperties(ListTag list) {
this.list = list;
}
public static final String[] handledMechs = new String[] {
}; // None
ListTag list;
public static void registerTags() {
// <--[tag]
// @attribute <ListTag.formatted>
// @returns ElementTag
// @description
// Returns the list in a human-readable format.
// EG, a list of "<npc>|<player>|potato" will return "GuardNPC, bob, and potato".
// -->
PropertyParser.<BukkitListProperties>registerTag("formatted", (attribute, listObj) -> {
ListTag list = listObj.list;
if (list.isEmpty()) {
return new ElementTag("");
}
StringBuilder output = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
ObjectTag object = list.getObject(i);
String val = object.toString();
boolean handled = false;
if (val.startsWith("p@")) {
PlayerTag gotten = object.asType(PlayerTag.class, attribute.context);
if (gotten != null) {
output.append(gotten.getName());
handled = true;
}
}
if (val.startsWith("e@") || val.startsWith("n@")) {
EntityTag gotten = object.asType(EntityTag.class, attribute.context);
if (gotten != null) {
output.append(gotten.getName());
handled = true;
}
}
if (val.startsWith("i@")) {
ItemTag item = object.asType(ItemTag.class, attribute.context);
if (item != null) {
output.append(item.formattedName());
handled = true;
}
}
if (val.startsWith("m@")) {
MaterialTag material = object.asType(MaterialTag.class, attribute.context);
if (material != null) {
output.append(material.name());
handled = true;
}
}
if (!handled) {
if (object instanceof ElementTag) {
output.append(val.replaceAll("\\w+@", ""));
}
else {
output.append(ChatColor.stripColor(object.debuggable()));
}
}
if (i == list.size() - 2) {
output.append(i == 0 ? " and " : ", and ");
}
else {
output.append(", ");
}
}
return new ElementTag(output.toString().substring(0, output.length() - 2));
});
}
@Override
public String getPropertyString() {
return null;
}
@Override
public String getPropertyId() {
return "BukkitListProperties";
}
@Override
public void adjust(Mechanism mechanism) {
// None
}
}