This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
DataKeys.java
206 lines (195 loc) · 9.47 KB
/
DataKeys.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.denizenscript.denizen2sponge.utilities;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.*;
import com.denizenscript.denizen2core.utilities.Action;
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2core.utilities.ErrorInducedException;
import com.denizenscript.denizen2core.utilities.debugging.Debug;
import com.denizenscript.denizen2sponge.tags.objects.FormattedTextTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.flags.FlagHelper;
import com.denizenscript.denizen2sponge.utilities.flags.FlagMap;
import com.denizenscript.denizen2sponge.utilities.flags.FlagMapDataImpl;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.CatalogType;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.ImmutableDataHolder;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.value.BaseValue;
import org.spongepowered.api.text.Text;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
public class DataKeys {
private static Collection<Key> keys;
private static int lastUpdate;
private static void updateKeys() {
int currentTime = Sponge.getServer().getRunningTimeTicks();
if (currentTime != lastUpdate) {
lastUpdate = currentTime;
keys = new HashSet<>(Sponge.getRegistry().getAllOf(Key.class));
keys.add(FlagHelper.FLAGMAP); // TODO: Make this not needed! GetAllOf shouldn't require manual key registration!
}
}
public static Key getKeyForName(String name) {
updateKeys();
name = CoreUtilities.toLowerCase(name);
for (Key key : keys) {
if (name.equals(CoreUtilities.after(key.getId(), ":"))
|| name.equals(key.getId())) {
return key;
}
}
return null;
}
public static MapTag getAllKeys(DataHolder dataHolder) {
MapTag temp = new MapTag();
for (Key key : dataHolder.getKeys()) {
if (!dataHolder.supports(key)) { // Just in case
continue;
}
if (dataHolder.getOrNull(key) == null) {
// Nope nope nope!
continue;
}
AbstractTagObject ato = getValue(dataHolder, key, (s) -> {
Debug.error("Failed to read key '" + key.getId() + "': " + s);
});
if (ato != null && !(ato instanceof NullTag)) {
temp.getInternal().put(key.getId(), ato);
}
}
return temp;
}
// TODO: List support!
public static AbstractTagObject getValue(DataHolder dataHolder, Key key, Action<String> error) {
Class clazz = key.getElementToken().getRawType();
if (!dataHolder.supports(key)) {
if (FlagMap.class.isAssignableFrom(clazz)) {
return new MapTag();
}
error.run("This data holder does not support the key '" + key.getId() + "'!");
return new NullTag();
}
if (Boolean.class.isAssignableFrom(clazz)) {
return new BooleanTag(dataHolder.getOrElse((Key<BaseValue<Boolean>>) key, false));
}
else if (CatalogType.class.isAssignableFrom(clazz)) {
return new TextTag(dataHolder.getValue((Key<BaseValue<CatalogType>>) key).orElseThrow(() -> new ErrorInducedException("Value not present!")).get().toString());
}
else if (Double.class.isAssignableFrom(clazz)) {
return new NumberTag(dataHolder.getOrElse((Key<BaseValue<Double>>) key, 0.0));
}
else if (Enum.class.isAssignableFrom(clazz)) {
return new TextTag(dataHolder.getValue((Key<BaseValue<Enum>>) key).orElseThrow(() -> new ErrorInducedException("Empty enum value!")).get().name());
}
else if (Integer.class.isAssignableFrom(clazz)) {
return new IntegerTag(dataHolder.getOrElse((Key<BaseValue<Integer>>) key, 0));
}
else if (Vector3d.class.isAssignableFrom(clazz)) {
return new LocationTag(dataHolder.getOrElse((Key<BaseValue<Vector3d>>) key, new Vector3d(0, 0, 0)));
}
else if (Text.class.isAssignableFrom(clazz)) {
return new FormattedTextTag(dataHolder.getOrElse((Key<BaseValue<Text>>) key, Text.EMPTY));
}
else if (FlagMap.class.isAssignableFrom(clazz)) {
return new MapTag(dataHolder.getOrElse((Key<BaseValue<FlagMap>>) key, new FlagMap(new MapTag())).flags.getInternal());
}
else {
error.run("The value type '" + clazz.getName() + "' is not supported yet!");
return new NullTag();
}
}
public static void tryApply(DataHolder entity, Key key, AbstractTagObject value, Action<String> error) {
Class clazz = key.getElementToken().getRawType();
if (!entity.supports(key)) {
if (FlagMap.class.isAssignableFrom(clazz)) {
entity.offer(new FlagMapDataImpl(new FlagMap(new MapTag())));
}
else {
error.run("This data holder does not support the key '" + key.getId() + "'!");
return;
}
}
if (Boolean.class.isAssignableFrom(clazz)) {
entity.offer(key, BooleanTag.getFor(error, value).getInternal());
}
else if (CatalogType.class.isAssignableFrom(clazz)) {
String val = value.toString();
Optional optCatalogType = Sponge.getRegistry().getType(clazz, val);
if (!optCatalogType.isPresent()) {
error.run("Invalid value '" + val + "' for property '" + key.getId() + "'!");
return;
}
entity.offer(key, optCatalogType.get());
}
else if (Double.class.isAssignableFrom(clazz)) {
entity.offer(key, NumberTag.getFor(error, value).getInternal());
}
else if (Enum.class.isAssignableFrom(clazz)) {
entity.offer(key, Enum.valueOf(clazz, value.toString().toUpperCase()));
}
else if (Integer.class.isAssignableFrom(clazz)) {
entity.offer(key, (int) IntegerTag.getFor(error, value).getInternal());
}
else if (Vector3d.class.isAssignableFrom(clazz)) {
entity.offer(key, LocationTag.getFor(error, value).getInternal().toVector3d());
}
else if (Text.class.isAssignableFrom(clazz)) {
entity.offer(key, FormattedTextTag.getFor(error, value).getInternal());
}
else if (FlagMap.class.isAssignableFrom(clazz)) {
entity.offer(new FlagMapDataImpl(new FlagMap(MapTag.getFor(error, value))));
}
else {
error.run("The value type '" + clazz.getName() + "' is not supported yet!");
}
}
public static ImmutableDataHolder with(ImmutableDataHolder entity, Key key, AbstractTagObject value, Action<String> error) {
Class clazz = key.getElementToken().getRawType();
if (!entity.supports(key)) {
if (FlagMap.class.isAssignableFrom(clazz)) {
entity = (ImmutableDataHolder) entity.with(new FlagMapDataImpl(new FlagMap(new MapTag()))).get();
}
else {
error.run("This data holder does not support the key '" + key.getId() + "'!");
return null;
}
}
if (Boolean.class.isAssignableFrom(clazz)) {
return (ImmutableDataHolder) entity.with(key, BooleanTag.getFor(error, value).getInternal()).get();
}
else if (CatalogType.class.isAssignableFrom(clazz)) {
String val = value.toString();
Optional optCatalogType = Sponge.getRegistry().getType(clazz, val);
if (!optCatalogType.isPresent()) {
error.run("Invalid value '" + val + "' for property '" + key.getId() + "'!");
return null;
}
return (ImmutableDataHolder) entity.with(key, optCatalogType.get()).get();
}
else if (Double.class.isAssignableFrom(clazz)) {
return (ImmutableDataHolder) entity.with(key, NumberTag.getFor(error, value).getInternal()).get();
}
else if (Enum.class.isAssignableFrom(clazz)) {
return (ImmutableDataHolder) entity.with(key, Enum.valueOf(clazz, value.toString().toUpperCase())).get();
}
else if (Integer.class.isAssignableFrom(clazz)) {
return (ImmutableDataHolder) entity.with(key, (int) IntegerTag.getFor(error, value).getInternal()).get();
}
else if (Vector3d.class.isAssignableFrom(clazz)) {
return (ImmutableDataHolder) entity.with(key, LocationTag.getFor(error, value).getInternal().toVector3d()).get();
}
else if (Text.class.isAssignableFrom(clazz)) {
return (ImmutableDataHolder) entity.with(key, FormattedTextTag.getFor(error, value).getInternal()).get();
}
else if (FlagMap.class.isAssignableFrom(clazz)) {
return (ImmutableDataHolder) entity.with(new FlagMapDataImpl(new FlagMap(MapTag.getFor(error, value)))).get();
}
else {
error.run("The value type '" + clazz.getName() + "' is not supported yet!");
return null;
}
}
}