/
PropertyParser.java
115 lines (87 loc) · 4.12 KB
/
PropertyParser.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
package net.aufdemrand.denizen.objects.properties;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.properties.Entity.*;
import net.aufdemrand.denizen.objects.properties.Item.*;
import net.aufdemrand.denizen.utilities.debugging.dB;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class PropertyParser {
// Keep track of which Property belongs to which dObject
static Map<Class<? extends dObject>, List<Class>> properties
= new HashMap<Class<? extends dObject>, List<Class>>();
// Keep track of the static 'describes' and 'getFrom' methods for each Property
static Map<Class, Method> describes = new WeakHashMap<Class, Method>();
static Map<Class, Method> getFrom = new WeakHashMap<Class, Method>();
public PropertyParser() {
properties.clear();
describes.clear();
getFrom.clear();
// register core dEntity properties
registerProperty(EntityProfession.class, dEntity.class);
registerProperty(EntityAge.class, dEntity.class);
registerProperty(EntityFramed.class, dEntity.class);
registerProperty(EntityInfected.class, dEntity.class);
// register core dItem properties
registerProperty(ItemEnchantments.class, dItem.class);
registerProperty(ItemDisplayname.class, dItem.class);
registerProperty(ItemLore.class, dItem.class);
registerProperty(ItemQuantity.class, dItem.class);
registerProperty(ItemDurability.class, dItem.class);
registerProperty(ItemSkullskin.class, dItem.class);
}
public void registerProperty(Class property, Class<? extends dObject> object) {
// Add property to the dObject's Properties list
List<Class> prop_list;
// Get current properties list, or make a new one
if (properties.containsKey(object))
prop_list = properties.get(object);
else prop_list = new ArrayList<Class>();
// Add this property to the list
prop_list.add(property);
// Put the list back into the Map
properties.put(object, prop_list);
// Cache methods used for fetching new properties
try {
describes.put(property, property.getMethod("describes", dObject.class));
getFrom.put(property, property.getMethod("getFrom", dObject.class));
} catch (NoSuchMethodException e) {
dB.echoError("Unable to register property '" + property.getSimpleName() + "'!");
}
}
public static String getPropertiesString(dObject object) {
StringBuilder prop_string = new StringBuilder();
// Iterate through each property associated with the dObject type, invoke 'describes'
// and if 'true', add property string from the property to the prop_string.
for (Property property: getProperties(object)) {
String description = property.getPropertyString();
if (description != null) {
prop_string.append(property.getPropertyId()).append('=')
.append(description.replace(';', (char)0x2011)).append(';');
}
}
// Return the list of properties
if (prop_string.length() > 0) // Remove final semicolon
return "[" + prop_string.substring(0, prop_string.length() - 1) + "]";
else
return "";
}
public static List<Property> getProperties(dObject object) {
List<Property> props = new ArrayList<Property>();
try {
if (properties.containsKey(object.getClass())) {
for (Class property : properties.get(object.getClass())) {
if ((Boolean) describes.get(property).invoke(null, object))
props.add((Property) getFrom.get(property).invoke(null, object));
}
}
} catch (IllegalAccessException e) {
dB.echoError(e);
} catch (InvocationTargetException e) {
dB.echoError(e);
}
return props;
}
}