Skip to content

Commit f2f5dfa

Browse files
committed
/jsscripts update and others
- jspm plain response logging - increase gradle memory - remove outdated global.d.ts
1 parent d25d7e0 commit f2f5dfa

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Done to increase the memory available to gradle.
2-
org.gradle.jvmargs=-Xmx2G
2+
org.gradle.jvmargs=-Xmx4G
33
# Fabric Properties
44
# check these on https://modmuss50.me/fabric.html
55
minecraft_version=1.20.1

src/main/java/de/blazemcworld/jsscripts/JSPM.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public static void upload(String name, byte[] zip) throws Exception {
3535
.POST(HttpRequest.BodyPublishers.ofByteArray(zip))
3636
.build();
3737

38-
JsonObject res = JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject();
38+
String strRes = client.send(req, HttpResponse.BodyHandlers.ofString()).body();
39+
JsScripts.LOGGER.info(strRes);
40+
JsonObject res = JsonParser.parseString(strRes).getAsJsonObject();
3941
if (!res.get("success").getAsBoolean()) {
4042
throw new Exception(res.get("error").getAsString());
4143
}
@@ -52,7 +54,10 @@ private static String getToken() throws Exception {
5254
.uri(URI.create(HOST + "/auth/getnonce/" + JsScripts.MC.getSession().getProfile().getId().toString()))
5355
.build();
5456

55-
JsonObject nonceRes = JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject();
57+
58+
String nonceStrRes = client.send(req, HttpResponse.BodyHandlers.ofString()).body();
59+
JsScripts.LOGGER.info(nonceStrRes);
60+
JsonObject nonceRes = JsonParser.parseString(nonceStrRes).getAsJsonObject();
5661
if (!nonceRes.get("success").getAsBoolean()) {
5762
throw new Exception(nonceRes.get("error").getAsString());
5863
}
@@ -90,12 +95,14 @@ private static String getToken() throws Exception {
9095
""".formatted(clientNonce)))
9196
.build();
9297

93-
JsonObject tokenRes = JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject();
98+
String tokenStrRes = client.send(req, HttpResponse.BodyHandlers.ofString()).body();
99+
JsScripts.LOGGER.info(tokenStrRes);
100+
JsonObject tokenRes = JsonParser.parseString(tokenStrRes).getAsJsonObject();
94101
if (!tokenRes.get("success").getAsBoolean()) {
95102
throw new Exception(nonceRes.get("error").getAsString());
96103
}
97104
token = tokenRes.get("token").getAsString();
98-
expires = System.currentTimeMillis() + 50 * 60 * 1000;
105+
expires = tokenRes.get("expireIn").getAsLong();
99106

100107
return token;
101108
}
@@ -140,4 +147,14 @@ public static boolean has(String name) throws Exception {
140147

141148
return client.send(req, HttpResponse.BodyHandlers.discarding()).statusCode() != 404;
142149
}
150+
151+
public static String getVersion(String name) throws Exception {
152+
HttpClient client = HttpClient.newHttpClient();
153+
154+
HttpRequest req = HttpRequest.newBuilder()
155+
.uri(URI.create("https://raw.githubusercontent.com/McJsScripts/JSPMRegistry/master/packages/" + name + "/jspm.json"))
156+
.build();
157+
158+
return JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject().getAsJsonObject("version").get("pkg").getAsString();
159+
}
143160
}

src/main/java/de/blazemcworld/jsscripts/JsScriptsCmd.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.*;
44
import com.mojang.brigadier.arguments.StringArgumentType;
5+
import com.sun.jna.platform.FileUtils;
56
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
67
import net.minecraft.MinecraftVersion;
78
import net.minecraft.text.ClickEvent;
@@ -35,6 +36,7 @@ public void register() {
3536
JsScripts.displayChat(Text.literal("/jsscripts disable - Remove a script from the auto-enable list.").formatted(Formatting.AQUA));
3637
JsScripts.displayChat(Text.literal("/jsscripts upload - Upload a script to jspm.").formatted(Formatting.AQUA));
3738
JsScripts.displayChat(Text.literal("/jsscripts download - Download a script from jspm.").formatted(Formatting.AQUA));
39+
JsScripts.displayChat(Text.literal("/jsscripts update - Check for script updates from jspm.").formatted(Formatting.AQUA));
3840
return 1;
3941
})
4042
.then(literal("reload")
@@ -219,6 +221,7 @@ public void register() {
219221
MinecraftVersion.CURRENT.getName()
220222
));
221223
JsScripts.displayChat(Text.literal("Please update the newly created jspm.json file in the script if necessary, then retry.").formatted(Formatting.AQUA));
224+
JsScripts.displayChat(Text.literal("It is also recommended to have a README.md and LICENSE file, as the code will be made public.").formatted(Formatting.AQUA));
222225
return;
223226
}
224227

@@ -272,8 +275,14 @@ public void register() {
272275
}
273276

274277
if (root.toFile().exists()) {
275-
JsScripts.displayChat(Text.literal("Script already found locally! Delete it to re-download.").formatted(Formatting.RED));
276-
return;
278+
FileUtils fu = FileUtils.getInstance();
279+
if (fu.hasTrash()) {
280+
fu.moveToTrash(root.toFile());
281+
JsScripts.displayChat(Text.literal("Moved previous script version to the trash.").formatted(Formatting.AQUA));
282+
} else {
283+
JsScripts.displayChat(Text.literal("Script already found locally! Delete it to re-download.").formatted(Formatting.RED));
284+
return;
285+
}
277286
}
278287

279288
JsScripts.displayChat(Text.literal("Downloading script from JSPM...").formatted(Formatting.AQUA));
@@ -290,6 +299,42 @@ public void register() {
290299
})
291300
)
292301
)
302+
.then(literal("update")
303+
.executes((e) -> {
304+
new Thread(() -> {
305+
int updates = 0;
306+
for (File f : ScriptManager.modDir.resolve("scripts").toFile().listFiles()) {
307+
try {
308+
Path p = f.toPath().resolve("jspm.json");
309+
if (!Files.exists(p)) {
310+
JsScripts.displayChat(Text.literal(f.getName() + ": No jspm.json").formatted(Formatting.AQUA));
311+
continue;
312+
}
313+
String ver = JsonParser.parseString(Files.readString(p)).getAsJsonObject().getAsJsonObject("version").get("pkg").getAsString();
314+
315+
try {
316+
String remoteVer = JSPM.getVersion(f.getName());
317+
if (ver.equals(remoteVer)) {
318+
JsScripts.displayChat(Text.literal(f.getName() + ": Up to date. (" + ver + ")").formatted(Formatting.AQUA));
319+
} else {
320+
JsScripts.displayChat(Text.literal(f.getName() + ": Update available. (" + ver + " => " + remoteVer + ")").formatted(Formatting.GREEN)
321+
.styled(s -> s.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/jsscripts download " + f.getName()))));
322+
updates++;
323+
}
324+
} catch (Exception unknown) {
325+
JsScripts.displayChat(Text.literal(f.getName() + ": No remote found.").formatted(Formatting.RED));
326+
}
327+
} catch (Exception err) {
328+
JsScripts.displayChat(Text.literal(f.getName() + ": Unknown Error").formatted(Formatting.RED));
329+
}
330+
}
331+
if (updates > 0) {
332+
JsScripts.displayChat(Text.literal(updates + " updates found. Click them to update.").formatted(Formatting.GREEN));
333+
}
334+
}).start();
335+
return 1;
336+
})
337+
)
293338
));
294339
}
295340
}

src/main/java/de/blazemcworld/jsscripts/TypingGen.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public static void genTypesIn(String targets) {
4242
Files.writeString(out.resolve("jsconfig.json"), """
4343
{
4444
"compilerOptions": {
45-
"typeRoots": ["types/global.d.ts"],
4645
"paths": {
4746
"$*": ["./types/*"],
4847
"#*": ["./scripts/*"]
@@ -57,10 +56,6 @@ public static void genTypesIn(String targets) {
5756
out.toFile().mkdirs();
5857
}
5958

60-
Files.writeString(out.resolve("global.d.ts"), """
61-
declare const script: import("../types/de/blazemcworld/jsscripts/Script").default;
62-
""");
63-
6459
JsScripts.displayChat(Text.literal("Scanning available classes...").formatted(Formatting.AQUA));
6560

6661
ClassLoader cl = JsScripts.class.getClassLoader();

0 commit comments

Comments
 (0)