2121import java .lang .reflect .Method ;
2222import java .util .*;
2323
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+ */
2432public final class Script {
2533 private final File sourceFile ;
2634 private final Class <?>[] classes ;
2735 private final String name ;
2836 private final Map <String , Member > functions ;
29- private final List <Member > events ;
3037 private final Collection <SourceData > data ;
3138 private final Structure [] members ;
39+ private final Skript skript ;
3240
33- public Script (Skript skript , File sourceFile , Class <?>... classes ) {
41+ Script (Skript skript , File sourceFile , Class <?>... classes ) {
3442 this (true , skript , sourceFile , classes );
3543 }
3644
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 ;
3847 this .sourceFile = sourceFile ;
3948 this .classes = classes ;
4049 this .name = mainClass ().getName ();
4150 this .functions = new HashMap <>();
42- this .events = new ArrayList <>();
4351 this .data = new ArrayList <>();
4452 final List <Structure > structures = new ArrayList <>();
4553 for (final Class <?> type : classes ) {
@@ -59,20 +67,19 @@ public Script(boolean init, Skript skript, File sourceFile, Class<?>... classes)
5967 } else if (method .isAnnotationPresent (EventData .class )) {
6068 final EventData event = method .getAnnotation (EventData .class );
6169 final Member member = new Member (this , method , event .async ());
62- this .events .add (member );
6370 skript .registerEventHandler ((Class <? extends Event >) skript .getClass (event .event ()), new InvokingScriptRunner (mainClass (), member ));
6471 }
6572 }
6673 this .members = structures .toArray (new Structure [0 ]);
6774 if (init ) {
68- verify ();
69- load (skript );
75+ this . verify ();
76+ this . load (skript );
7077 }
7178 }
7279
7380 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 ()) {
7683 final Member value = entry .getValue ();
7784 final String name = entry .getKey ();
7885 try {
@@ -88,34 +95,81 @@ void load(Skript skript) {
8895 skript .runEvent (new Load (this ));
8996 }
9097
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+ */
91104 public String getSimpleName () {
92105 return mainClass ().getSimpleName ();
93106 }
94107
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+ */
95114 public String getPath () {
96115 return mainClass ().getName ();
97116 }
98117
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+ */
99125 public Class <? extends CompiledScript > mainClass () {
100126 return (Class <? extends CompiledScript >) classes [0 ];
101127 }
102128
129+ /**
130+ * Whether this script has a known source file.
131+ *
132+ * @return the source file
133+ */
103134 public boolean hasSourceFile () {
104135 return sourceFile != null && sourceFile .exists () && sourceFile .isFile ();
105136 }
106137
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+ */
107145 public Member getFunction (String name ) {
108146 return functions .get (name );
109147 }
110148
149+ /**
150+ * Finds source data annotations for members in this script.
151+ *
152+ * @return member data
153+ */
111154 public Collection <SourceData > getMemberData () {
112155 return data ;
113156 }
114157
158+ /**
159+ * Returns the known source file for this script.
160+ *
161+ * @return potentially null source file
162+ */
115163 public File sourceFile () {
116164 return sourceFile ;
117165 }
118166
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+ */
119173 public Class <?>[] classes () {
120174 return classes ;
121175 }
@@ -126,6 +180,11 @@ public String toString() {
126180 "name=" + name + ']' ;
127181 }
128182
183+ /**
184+ * Returns the structures for all members, used for data collection.
185+ *
186+ * @return all found structures
187+ */
129188 public Structure [] getMembers () {
130189 return members ;
131190 }
@@ -137,5 +196,12 @@ private void forceLoad(Class<?> cls) {
137196 }
138197 }
139198
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+ }
141207}
0 commit comments