11package de .blazemcworld .jsscripts ;
22
3+ import com .google .gson .*;
34import com .mojang .brigadier .arguments .StringArgumentType ;
45import net .fabricmc .fabric .api .client .command .v2 .ClientCommandRegistrationCallback ;
5- import net .minecraft .text .Text ;
6+ import net .minecraft .text .* ;
67import net .minecraft .util .Formatting ;
78
9+ import java .io .File ;
10+ import java .io .IOException ;
811import java .nio .file .Files ;
912import java .nio .file .Path ;
1013
@@ -21,6 +24,8 @@ public void register() {
2124 JsScripts .displayChat (Text .literal ("/jsscripts gen_types - Generate .d.ts files" ).formatted (Formatting .AQUA ));
2225 JsScripts .displayChat (Text .literal ("/jsscripts list - List all currently enabled scripts." ).formatted (Formatting .AQUA ));
2326 JsScripts .displayChat (Text .literal ("/jsscripts sign - Adds your signature to a script." ).formatted (Formatting .AQUA ));
27+ JsScripts .displayChat (Text .literal ("/jsscripts enable - Enable a script." ).formatted (Formatting .AQUA ));
28+ JsScripts .displayChat (Text .literal ("/jsscripts disable - Disable a script." ).formatted (Formatting .AQUA ));
2429 return 1 ;
2530 })
2631 .then (literal ("reload" )
@@ -59,9 +64,42 @@ public void register() {
5964 )
6065 .then (literal ("list" )
6166 .executes ((e ) -> {
62- JsScripts .displayChat (Text .literal ("Current Scripts (" + ScriptManager .scripts .size () + ")" ).formatted (Formatting .AQUA ));
63- for (Script s : ScriptManager .scripts ) {
64- JsScripts .displayChat (Text .literal ("-" + s .getFile ().getName ()).formatted (Formatting .AQUA ));
67+ JsScripts .displayChat (Text .literal ("Scripts (Loaded " + ScriptManager .scripts .size () + ")" ).formatted (Formatting .AQUA ));
68+ try {
69+ for (File f : ScriptManager .availableScripts ()) {
70+ Script s = ScriptManager .scriptByFile (f );
71+
72+ MutableText msg = Text .literal ("-" + f .getName ()).copy ();
73+ Style style = msg .getStyle ();
74+ if (s == null ) {
75+ if (ScriptManager .errors .contains (f )) {
76+ style = style .withColor (Formatting .RED );
77+ msg .append (" - Error" );
78+ } else {
79+ style = style .withColor (Formatting .GRAY );
80+ msg .append (" - Disabled" );
81+ style = style .withClickEvent (new ClickEvent (ClickEvent .Action .RUN_COMMAND , "/jsscripts enable " + ScriptManager .scriptDir .toPath ().relativize (f .toPath ())));
82+ style = style .withHoverEvent (new HoverEvent (HoverEvent .Action .SHOW_TEXT , Text .literal ("Click to enable " + f .getName ()).formatted (Formatting .AQUA )));
83+ }
84+ } else {
85+ msg .append (" - " );
86+ switch (s .getCause ()) {
87+ case DIRECT -> {
88+ msg .append ("Directly" );
89+ style = style .withColor (Formatting .GREEN );
90+ style = style .withClickEvent (new ClickEvent (ClickEvent .Action .RUN_COMMAND , "/jsscripts disable " + ScriptManager .scriptDir .toPath ().relativize (f .toPath ())));
91+ style = style .withHoverEvent (new HoverEvent (HoverEvent .Action .SHOW_TEXT , Text .literal ("Click to disable " + f .getName ()).formatted (Formatting .AQUA )));
92+ }
93+ case DEPENDED_UPON -> {
94+ msg .append ("Dependency" );
95+ style = style .withColor (TextColor .fromRgb (4031824 ));
96+ }
97+ }
98+ }
99+ JsScripts .displayChat (msg .setStyle (style ));
100+ }
101+ } catch (IOException ex ) {
102+ throw new RuntimeException (ex );
65103 }
66104 return 1 ;
67105 })
@@ -72,7 +110,7 @@ public void register() {
72110 JsScripts .displayChat (Text .literal ("/jsscripts sign <script>" ).formatted (Formatting .AQUA ));
73111 return 1 ;
74112 })
75- .then (argument ("script" , StringArgumentType .string ())
113+ .then (argument ("script" , StringArgumentType .greedyString ())
76114 .executes ((e ) -> {
77115 try {
78116 Path p = ScriptManager .scriptDir .toPath ().resolve (e .getArgument ("script" , String .class ));
@@ -88,6 +126,103 @@ public void register() {
88126 })
89127 )
90128 )
129+ .then (literal ("enable" )
130+ .executes ((e ) -> {
131+ JsScripts .displayChat (Text .literal ("Invalid usage! Usage:" ).formatted (Formatting .AQUA ));
132+ JsScripts .displayChat (Text .literal ("/jsscripts enable <script>" ).formatted (Formatting .AQUA ));
133+ return 1 ;
134+ })
135+ .then (argument ("script" , StringArgumentType .greedyString ())
136+ .executes ((e ) -> {
137+ File f = ScriptManager .scriptDir .toPath ().resolve (e .getArgument ("script" , String .class )).toFile ();
138+
139+ if (ScriptManager .scriptByFile (f ) != null ) {
140+ JsScripts .displayChat (Text .literal ("Already enabled!" ).formatted (Formatting .AQUA ));
141+ return 1 ;
142+ }
143+
144+ if (!f .exists ()) {
145+ JsScripts .displayChat (Text .literal ("Unknown script!" ).formatted (Formatting .AQUA ));
146+ return 1 ;
147+ }
148+
149+ try {
150+ JsonObject obj = JsonParser .parseString (Files .readString (ScriptManager .config .toPath ())).getAsJsonObject ();
151+
152+ JsonArray loaded = obj .getAsJsonArray ("loaded_scripts" );
153+
154+ if (loaded .contains (new JsonPrimitive (e .getArgument ("script" , String .class )))) {
155+ JsScripts .displayChat (Text .literal ("Should be enabled! Check logs in case it's not." ).formatted (Formatting .AQUA ));
156+ return 1 ;
157+ }
158+
159+ loaded .add (e .getArgument ("script" , String .class ));
160+ obj .add ("loaded_scripts" , loaded );
161+ Files .writeString (ScriptManager .config .toPath (), obj .toString ());
162+ ScriptManager .reload ();
163+ JsScripts .displayChat (Text .literal ("Enabled script!" ).formatted (Formatting .AQUA ));
164+ } catch (Exception err ) {
165+ JsScripts .displayChat (Text .literal ("Error enabling script!" ).formatted (Formatting .AQUA ));
166+ err .printStackTrace ();
167+ }
168+ return 1 ;
169+ })
170+ )
171+ )
172+ .then (literal ("disable" )
173+ .executes ((e ) -> {
174+ JsScripts .displayChat (Text .literal ("Invalid usage! Usage:" ).formatted (Formatting .AQUA ));
175+ JsScripts .displayChat (Text .literal ("/jsscripts disable <script>" ).formatted (Formatting .AQUA ));
176+ return 1 ;
177+ })
178+ .then (argument ("script" , StringArgumentType .greedyString ())
179+ .executes ((e ) -> {
180+ try {
181+ JsonObject obj = JsonParser .parseString (Files .readString (ScriptManager .config .toPath ())).getAsJsonObject ();
182+
183+ File query = ScriptManager .scriptDir .toPath ().resolve (e .getArgument ("script" , String .class )).toFile ();
184+ String found = null ;
185+
186+ JsonArray loaded = obj .getAsJsonArray ("loaded_scripts" );
187+ for (JsonElement elm : loaded ) {
188+ if (ScriptManager .scriptDir .toPath ().resolve (elm .getAsString ()).toFile ().equals (query )) {
189+ found = elm .getAsString ();
190+ break ;
191+ }
192+ }
193+ if (found != null ) {
194+ loaded .remove (new JsonPrimitive (found ));
195+ }
196+ obj .add ("loaded_scripts" , loaded );
197+
198+ JsonArray devScripts = obj .getAsJsonArray ("dev_scripts" );
199+ for (JsonElement elm : devScripts ) {
200+ if (ScriptManager .scriptDir .toPath ().resolve (elm .getAsString ()).toFile ().equals (query )) {
201+ found = elm .getAsString ();
202+ break ;
203+ }
204+ }
205+ if (found != null ) {
206+ devScripts .remove (new JsonPrimitive (found ));
207+ }
208+ obj .add ("dev_scripts" , devScripts );
209+
210+ if (found == null ) {
211+ JsScripts .displayChat (Text .literal ("Script not found in config! Is it in a dev scripts folder?" ).formatted (Formatting .AQUA ));
212+ return 1 ;
213+ }
214+
215+ Files .writeString (ScriptManager .config .toPath (), obj .toString ());
216+ ScriptManager .reload ();
217+ JsScripts .displayChat (Text .literal ("Disabled script!" ).formatted (Formatting .AQUA ));
218+ } catch (Exception err ) {
219+ JsScripts .displayChat (Text .literal ("Error disabling script!" ).formatted (Formatting .AQUA ));
220+ err .printStackTrace ();
221+ }
222+ return 1 ;
223+ })
224+ )
225+ )
91226 ));
92227 }
93228
0 commit comments