-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
SupportManager.java
101 lines (92 loc) · 3.67 KB
/
SupportManager.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
package net.gnomeffinway.depenizen.support;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.ReplaceableTagEvent;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.gnomeffinway.depenizen.Depenizen;
import org.bukkit.event.Listener;
import java.util.*;
public class SupportManager {
private final Depenizen depenizen;
private final PropertyParser propertyParser;
public final Map<String, Support> supported;
private final Map<String, Support> additionalTags;
private boolean hasNewObjects = false;
public SupportManager(Depenizen depenizen) {
this.depenizen = depenizen;
this.propertyParser = DenizenAPI.getCurrentInstance().getPropertyParser();
this.supported = new HashMap<String, Support>();
this.additionalTags = new HashMap<String, Support>();
TagManager.registerTagEvents(this);
}
public void register(Support support) {
supported.put(support.getPlugin().getName().toUpperCase(), support);
if (support.hasObjects()) {
for (Class<? extends dObject> object : support.getObjects()) {
ObjectFetcher.registerWithObjectFetcher(object);
}
hasNewObjects = true;
}
if (support.hasProperties()) {
for (Map.Entry<Class<? extends Property>, Class<? extends dObject>[]> prop
: support.getProperties().entrySet()) {
for (Class<? extends dObject> obj : prop.getValue()) {
if (obj.equals(dNPC.class) && Depends.citizens == null) continue;
propertyParser.registerProperty(prop.getKey(), obj);
}
}
}
if (support.hasEvents()) {
for (Class<? extends Listener> event : support.getEvents()) {
try {
depenizen.getServer().getPluginManager().registerEvents(event.newInstance(), depenizen);
} catch (Exception e) {
dB.echoError(e);
}
}
}
if (support.hasAdditionalTags()) {
for (String tag : support.getAdditionalTags()) {
this.additionalTags.put(tag.toLowerCase(), support);
}
}
}
public void registerNewObjects() {
if (hasNewObjects) {
try { ObjectFetcher._initialize(); }
catch (Exception e) {
dB.echoError(e);
}
hasNewObjects = false;
}
}
private String findAdditionalTags(String name, Attribute attribute) {
for (String tag : additionalTags.keySet()) {
if (name.equalsIgnoreCase(tag)) {
return additionalTags.get(tag).additionalTags(attribute);
}
}
return null;
}
@TagManager.TagEvents
public void tagListener(ReplaceableTagEvent event) {
if (event.replaced()) return;
Attribute attribute = new Attribute(event.raw_tag, event.getScriptEntry());
String replaced = null;
String name = event.getName().toLowerCase();
for (String tag : additionalTags.keySet()) {
if (name.startsWith(tag)) {
replaced = additionalTags.get(tag).additionalTags(attribute);
return;
}
}
if (replaced != null) {
event.setReplaced(replaced);
}
}
}