11package rml .layer .compat .kubejs ;
22
3+ import com .google .common .io .ByteSource ;
34import dev .latvian .kubejs .KubeJS ;
45import dev .latvian .kubejs .script .BindingsEvent ;
56import dev .latvian .kubejs .script .ScriptFile ;
67import dev .latvian .kubejs .script .ScriptManager ;
78import dev .latvian .kubejs .script .ScriptPack ;
89import net .minecraft .util .ResourceLocation ;
910import net .minecraftforge .fml .common .eventhandler .SubscribeEvent ;
10- import org .apache .commons .io .FilenameUtils ;
11- import org .apache .commons .io .IOUtils ;
1211import rml .loader .api .annotations .PrivateAPI ;
1312import rml .loader .api .reflection .jvm .MethodAccessor ;
1413import rml .loader .api .reflection .jvm .ReflectionHelper ;
1514import rml .loader .ResourceModLoader ;
1615import rml .loader .api .mods .module .ModuleType ;
1716import rml .loader .core .RMLFMLLoadingPlugin ;
1817
19- import java .io .BufferedReader ;
2018import java .io .IOException ;
21- import java .nio .file . Files ;
19+ import java .nio .charset . StandardCharsets ;
2220import java .util .Map ;
2321
2422/**
@@ -32,42 +30,32 @@ public class RMKKubeJs {
3230 @ SubscribeEvent
3331 public static void onJSLoad (BindingsEvent event ){
3432 RMLFMLLoadingPlugin .Container .LOGGER .info ("Inject KubeJS" );
35- ResourceModLoader .loadModuleFindAssets (ModuleType .valueOf (new ResourceLocation ("rml" , "mod_kubejs" )),
36- (module , containerHolder ) -> {
37- if (!packs .containsKey (containerHolder .getContainer ().getModId ())) {
38- packs .put (containerHolder .getContainer ().getModId (), newPack .invoke (ScriptManager .instance , containerHolder .getContainer ().getModId ()));
39- }
40- },
41- (containerHolder , module , root , file ) -> {
42- String relative = root .relativize (file ).toString ();
43- if (!"js" .equals (FilenameUtils .getExtension (file .toString ())) || relative .startsWith ("_" ))
44- return ;
45- BufferedReader bufferedReader =null ;
46- try {
47- char [] fileBytes ;
48- bufferedReader =Files .newBufferedReader (file );
49- fileBytes =IOUtils .toCharArray (bufferedReader );
50- load (ScriptManager .instance , file .toUri ().toString (), fileBytes , containerHolder .getContainer ().getModId ());
51-
52- } catch (IOException e ) {
53- throw new RuntimeException (e );
54- }finally {
55- IOUtils .closeQuietly (bufferedReader );
56- }
33+ ResourceModLoader .loadContainerHolders (ModuleType .valueOf (new ResourceLocation ("rml" , "mod_kubejs" )), containerHolder -> {
34+ if (!packs .containsKey (containerHolder .getContainer ().getModId ())) {
35+ packs .put (containerHolder .getContainer ().getModId (), newPack .invoke (ScriptManager .instance , containerHolder .getContainer ().getModId ()));
36+ }
37+ });
38+ ResourceModLoader .loadModule (ModuleType .valueOf (new ResourceLocation ("rml" , "mod_kubejs" )), (context ) -> {
39+ if (context .isExtension ("js" )) {
40+ try {
41+ char [] fileBytes = ByteSource .wrap (context .getBytes ()).asCharSource (StandardCharsets .UTF_8 ).read ().toCharArray ();
42+ load (ScriptManager .instance , context .getFile ().toUri ().toString (), fileBytes , context .getContainerHolder ().getContainer ().getModId ());
43+ } catch (IOException e ) {
44+ context .error (e , "Error at Loading kubejs file for {}" , context .getFile ());
5745 }
58- );
46+ }}, RMKKubeJs . class );
5947 }
6048 @ PrivateAPI
6149 private static void load (ScriptManager manager , String name , char [] file , String modid ) {
62- KubeJS .LOGGER .info ("Found script at " + name );
50+ KubeJS .LOGGER .debug ("Found script at {}" , name );
6351 int weight ;
6452 weight = 0 ;
6553 if (name .endsWith ("/init.js" )) {
6654 weight = -100 ;
6755 }
6856 ScriptFile scriptFile = new BuffedJSFile (packs .get (modid ), name , weight , file );
6957 manager .scripts .put (name , scriptFile );
70- KubeJS .LOGGER .info ("Load script at " + name );
58+ KubeJS .LOGGER .debug ("Load script at {}" , name );
7159 }
7260 @ PrivateAPI public static final MethodAccessor <ScriptPack , ScriptManager > newPack = ReflectionHelper .getMethodAccessor (ScriptManager .class , "newPack" , null , String .class );
7361 @ PrivateAPI public static final Map <String , ScriptPack > packs ;
0 commit comments