/
Script.java
122 lines (101 loc) · 3.37 KB
/
Script.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
package net.aufdemrand.denizen.arguments;
import net.aufdemrand.denizen.interfaces.dScriptArgument;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Script implements dScriptArgument {
final public static Pattern matchesScriptPtrn = Pattern.compile("(?:.+:|)(.+)", Pattern.CASE_INSENSITIVE);
/**
* Gets a Script Argument Object from a dScript argument.
*
* @param string the dScript argument String
* @return a Script, or null if incorrectly formatted
*/
public static Script valueOf(String string) {
Matcher m = matchesScriptPtrn.matcher(string);
if (m.matches()) {
Script script = new Script(m.group(1));
// Make sure it's valid.
if (script.isValid()) return script;
}
return null;
}
private ScriptContainer container;
private String prefix = "Script";
private String name = null;
private boolean valid = false;
/**
* Creates a script object from a script name. If the script is valid, {@link #isValid()} will retrun true.
*
* @param scriptName
*/
public Script (String scriptName) {
// Required for tests
if (DenizenAPI.getCurrentInstance() == null) return;
if (ScriptRegistry.getScriptContainer(scriptName) != null) {
container = ScriptRegistry.getScriptContainer(scriptName);
name = scriptName.toUpperCase();
valid = true;
}
}
/**
* Confirms that the script references a valid name and type in current loaded ScriptsContainers.
*
* @return true if the script is valid, false if the script was not found, or the type is missing
*/
public boolean isValid() {
return valid;
}
/**
* Gets the type of the ScriptContainer, as defined by the TYPE: key.
*
* @return the type of the Script Container
*/
public String getType() {
return (container != null ? container.getType() : "invalid");
}
/**
* Gets the name of the ScriptContainer.
*
* @return script name
*/
public String getName() {
return name;
}
/**
* Gets the contents of the scriptContainer.
*
* @return ConfigurationSection of the script contents
*/
public ScriptContainer getContainer() {
return container;
}
@Override
public String getDefaultPrefix() {
return prefix;
}
@Override
public String debug() {
return "<G>" + prefix + "='<A>" + name + "<Y>(" + getType() + ")<G>' ";
}
@Override
public String as_dScriptArg() {
return prefix + ":" + name;
}
public String dScriptArgValue() {
return name;
}
@Override
public dScriptArgument setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return as_dScriptArg();
return new Element(identify()).getAttribute(attribute.fulfill(0));
}
}