1
1
package de .blazemcworld .jsscripts ;
2
2
3
+ import com .google .gson .*;
3
4
import com .mojang .brigadier .arguments .StringArgumentType ;
4
5
import net .fabricmc .fabric .api .client .command .v2 .ClientCommandRegistrationCallback ;
5
- import net .minecraft .text .Text ;
6
+ import net .minecraft .text .* ;
6
7
import net .minecraft .util .Formatting ;
7
8
9
+ import java .io .File ;
10
+ import java .io .IOException ;
8
11
import java .nio .file .Files ;
9
12
import java .nio .file .Path ;
10
13
@@ -21,6 +24,8 @@ public void register() {
21
24
JsScripts .displayChat (Text .literal ("/jsscripts gen_types - Generate .d.ts files" ).formatted (Formatting .AQUA ));
22
25
JsScripts .displayChat (Text .literal ("/jsscripts list - List all currently enabled scripts." ).formatted (Formatting .AQUA ));
23
26
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 ));
24
29
return 1 ;
25
30
})
26
31
.then (literal ("reload" )
@@ -59,9 +64,42 @@ public void register() {
59
64
)
60
65
.then (literal ("list" )
61
66
.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 );
65
103
}
66
104
return 1 ;
67
105
})
@@ -72,7 +110,7 @@ public void register() {
72
110
JsScripts .displayChat (Text .literal ("/jsscripts sign <script>" ).formatted (Formatting .AQUA ));
73
111
return 1 ;
74
112
})
75
- .then (argument ("script" , StringArgumentType .string ())
113
+ .then (argument ("script" , StringArgumentType .greedyString ())
76
114
.executes ((e ) -> {
77
115
try {
78
116
Path p = ScriptManager .scriptDir .toPath ().resolve (e .getArgument ("script" , String .class ));
@@ -88,6 +126,103 @@ public void register() {
88
126
})
89
127
)
90
128
)
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
+ )
91
226
));
92
227
}
93
228
0 commit comments