@@ -141,11 +141,35 @@ public void reload(JsonObject json, boolean init) {
141141
142142 String regex = File .separatorChar == '\\' ? "/" : "\\ \\ " ;
143143 String replacement = getSeparator ();
144+
145+ JsonElement el = json .get ("loaders" );
146+ JsonObject jsonLoaders ;
147+ if (el == null || !el .isJsonObject ()) {
148+ GroovyLog .msg ("No loaders are defined (or is not a json object). This means no scripts will be executed" )
149+ .add ("Please see https://cleanroommc.com/groovy-script/getting_started/run_config#loaders for help." )
150+ .add ("Alternatively delete runConfig.json to let GroovyScript generate default values." )
151+ .warn ()
152+ .logToMc ()
153+ .post ();
154+ jsonLoaders = new JsonObject ();
155+ el = jsonLoaders ;
156+ json .add ("loaders" , jsonLoaders );
157+ } else {
158+ jsonLoaders = el .getAsJsonObject ();
159+ }
160+
144161 if (json .has ("classes" )) {
145- throw new IllegalStateException ("GroovyScript classes definition in runConfig is deprecated! Classes are now treated as normal scripts." );
162+ GroovyLog .msg ("GroovyScript classes definition in runConfig is deprecated! Classes are now treated as normal scripts" )
163+ .add ("GroovyScript will try to add the defined paths to the loaders automatically. This may result in unexpected behaviour." )
164+ .add ("Visit https://cleanroommc.com/groovy-script/getting_started/run_config#classes to find out how to migrate." )
165+ .add ("Ask on the discord if you need more help." )
166+ .error ()
167+ .logToMc ()
168+ .post ();
169+ // adds properties of classes into the appropriate loader json element
170+ migrateClassesToLoaders (json .get ("classes" ), jsonLoaders );
146171 }
147172
148- JsonObject jsonLoaders = JsonHelper .getJsonObject (json , "loaders" );
149173 List <Pair <String , String >> pathsList = new ArrayList <>();
150174
151175 GroovyLog .Msg errorMsg = GroovyLog .msg ("Fatal while parsing runConfig.json" )
@@ -158,11 +182,8 @@ public void reload(JsonObject json, boolean init) {
158182 List <String > paths = new ArrayList <>();
159183
160184 for (JsonElement element : loader ) {
161- String path = element .getAsString ().replaceAll (regex , replacement );
162- while (path .endsWith ("/" ) || path .endsWith ("\\ " )) {
163- path = path .substring (0 , path .length () - 1 );
164- }
165- if (!checkValid (errorMsg , pathsList , entry .getKey (), path )) continue ;
185+ String path = sanitizePath (element .getAsString ().replaceAll (regex , replacement ));
186+ if (paths .contains (path ) || !checkValid (errorMsg , pathsList , entry .getKey (), path )) continue ;
166187 paths .add (path );
167188 }
168189
@@ -208,6 +229,41 @@ public void reload(JsonObject json, boolean init) {
208229 }
209230 }
210231
232+ private static JsonArray getLoaderJsonArray (JsonObject loaders , String loader ) {
233+ JsonElement loadersElement = loaders .get (loader );
234+ if (loadersElement == null || !loadersElement .isJsonArray ()) {
235+ loadersElement = new JsonArray ();
236+ loaders .add (loader , loadersElement );
237+ }
238+ return loadersElement .getAsJsonArray ();
239+ }
240+
241+ private void migrateClassesToLoaders (JsonElement classesJson , JsonObject jsonLoaders ) {
242+ JsonArray loaderPaths ;
243+ if (classesJson .isJsonArray ()) {
244+ loaderPaths = getLoaderJsonArray (jsonLoaders , LoadStage .PRE_INIT .getName ());
245+ for (JsonElement el1 : classesJson .getAsJsonArray ()) {
246+ if (el1 .isJsonPrimitive ()) {
247+ loaderPaths .add (el1 );
248+ }
249+ }
250+ } else if (classesJson .isJsonObject ()) {
251+ JsonObject classes = classesJson .getAsJsonObject ();
252+ for (Map .Entry <String , JsonElement > entry : classes .entrySet ()) {
253+ loaderPaths = getLoaderJsonArray (jsonLoaders , entry .getKey ());
254+ if (entry .getValue ().isJsonPrimitive ()) {
255+ loaderPaths .add (entry .getValue ().getAsString ());
256+ } else if (entry .getValue ().isJsonArray ()) {
257+ for (JsonElement el1 : entry .getValue ().getAsJsonArray ()) {
258+ if (el1 .isJsonPrimitive ()) {
259+ loaderPaths .add (el1 );
260+ }
261+ }
262+ }
263+ }
264+ }
265+ }
266+
211267 public String getPackName () {
212268 return packName ;
213269 }
0 commit comments