21
21
import java .lang .reflect .Method ;
22
22
import java .util .*;
23
23
24
+ /**
25
+ * A handle representation of a script containing its metadata and defined classes.
26
+ * <p>
27
+ * This can be used to find insights about a script or find a particular function handle.
28
+ * <p>
29
+ * These should not be stored - they can be arbitrarily graveyarded without any notification
30
+ * by the unloader, in which case this will become a dead spot in memory.
31
+ */
24
32
public final class Script {
25
33
private final File sourceFile ;
26
34
private final Class <?>[] classes ;
27
35
private final String name ;
28
36
private final Map <String , Member > functions ;
29
- private final List <Member > events ;
30
37
private final Collection <SourceData > data ;
31
38
private final Structure [] members ;
39
+ private final Skript skript ;
32
40
33
- public Script (Skript skript , File sourceFile , Class <?>... classes ) {
41
+ Script (Skript skript , File sourceFile , Class <?>... classes ) {
34
42
this (true , skript , sourceFile , classes );
35
43
}
36
44
37
- public Script (boolean init , Skript skript , File sourceFile , Class <?>... classes ) {
45
+ Script (boolean init , Skript skript , File sourceFile , Class <?>... classes ) {
46
+ this .skript = skript ;
38
47
this .sourceFile = sourceFile ;
39
48
this .classes = classes ;
40
49
this .name = mainClass ().getName ();
41
50
this .functions = new HashMap <>();
42
- this .events = new ArrayList <>();
43
51
this .data = new ArrayList <>();
44
52
final List <Structure > structures = new ArrayList <>();
45
53
for (final Class <?> type : classes ) {
@@ -59,20 +67,19 @@ public Script(boolean init, Skript skript, File sourceFile, Class<?>... classes)
59
67
} else if (method .isAnnotationPresent (EventData .class )) {
60
68
final EventData event = method .getAnnotation (EventData .class );
61
69
final Member member = new Member (this , method , event .async ());
62
- this .events .add (member );
63
70
skript .registerEventHandler ((Class <? extends Event >) skript .getClass (event .event ()), new InvokingScriptRunner (mainClass (), member ));
64
71
}
65
72
}
66
73
this .members = structures .toArray (new Structure [0 ]);
67
74
if (init ) {
68
- verify ();
69
- load (skript );
75
+ this . verify ();
76
+ this . load (skript );
70
77
}
71
78
}
72
79
73
80
void verify () {
74
- forceLoad (mainClass ());
75
- for (Map .Entry <String , Member > entry : functions .entrySet ()) {
81
+ this . forceLoad (mainClass ());
82
+ for (final Map .Entry <String , Member > entry : functions .entrySet ()) {
76
83
final Member value = entry .getValue ();
77
84
final String name = entry .getKey ();
78
85
try {
@@ -88,34 +95,81 @@ void load(Skript skript) {
88
95
skript .runEvent (new Load (this ));
89
96
}
90
97
98
+ /**
99
+ * The simple name of the main class for this script.
100
+ * This will be something in the format `script`
101
+ *
102
+ * @return the simple name
103
+ */
91
104
public String getSimpleName () {
92
105
return mainClass ().getSimpleName ();
93
106
}
94
107
108
+ /**
109
+ * The path of the main class for this script.
110
+ * This will be something in the format `skript.path.to.script`
111
+ *
112
+ * @return the path
113
+ */
95
114
public String getPath () {
96
115
return mainClass ().getName ();
97
116
}
98
117
118
+ /**
119
+ * Gets the main class of this script, which root-level members occupy.
120
+ * Custom types and other members may be moved to other classes, depending on the compiler used.
121
+ * This should not be stored - it will prevent the script unloading safely.
122
+ *
123
+ * @return the main class
124
+ */
99
125
public Class <? extends CompiledScript > mainClass () {
100
126
return (Class <? extends CompiledScript >) classes [0 ];
101
127
}
102
128
129
+ /**
130
+ * Whether this script has a known source file.
131
+ *
132
+ * @return the source file
133
+ */
103
134
public boolean hasSourceFile () {
104
135
return sourceFile != null && sourceFile .exists () && sourceFile .isFile ();
105
136
}
106
137
138
+ /**
139
+ * Returns a handle for the function with this name.
140
+ * Multiple functions may have the same name, this will return an arbitrary one.
141
+ *
142
+ * @param name the function name
143
+ * @return the function
144
+ */
107
145
public Member getFunction (String name ) {
108
146
return functions .get (name );
109
147
}
110
148
149
+ /**
150
+ * Finds source data annotations for members in this script.
151
+ *
152
+ * @return member data
153
+ */
111
154
public Collection <SourceData > getMemberData () {
112
155
return data ;
113
156
}
114
157
158
+ /**
159
+ * Returns the known source file for this script.
160
+ *
161
+ * @return potentially null source file
162
+ */
115
163
public File sourceFile () {
116
164
return sourceFile ;
117
165
}
118
166
167
+ /**
168
+ * Returns the known classes for this script.
169
+ * This will include the main class and any custom types, etc.
170
+ *
171
+ * @return the classes
172
+ */
119
173
public Class <?>[] classes () {
120
174
return classes ;
121
175
}
@@ -126,6 +180,11 @@ public String toString() {
126
180
"name=" + name + ']' ;
127
181
}
128
182
183
+ /**
184
+ * Returns the structures for all members, used for data collection.
185
+ *
186
+ * @return all found structures
187
+ */
129
188
public Structure [] getMembers () {
130
189
return members ;
131
190
}
@@ -137,5 +196,12 @@ private void forceLoad(Class<?> cls) {
137
196
}
138
197
}
139
198
140
-
199
+ /**
200
+ * Returns the Skript runtime that created this script.
201
+ *
202
+ * @return the runtime
203
+ */
204
+ public Skript skriptInstance () {
205
+ return skript ;
206
+ }
141
207
}
0 commit comments