Skip to content

Commit

Permalink
/jsscripts update and others
Browse files Browse the repository at this point in the history
- jspm plain response logging
- increase gradle memory
- remove outdated global.d.ts
  • Loading branch information
BlazeMCworld committed Jun 27, 2023
1 parent d25d7e0 commit f2f5dfa
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx2G
org.gradle.jvmargs=-Xmx4G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.20.1
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/de/blazemcworld/jsscripts/JSPM.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static void upload(String name, byte[] zip) throws Exception {
.POST(HttpRequest.BodyPublishers.ofByteArray(zip))
.build();

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

JsonObject nonceRes = JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject();

String nonceStrRes = client.send(req, HttpResponse.BodyHandlers.ofString()).body();
JsScripts.LOGGER.info(nonceStrRes);
JsonObject nonceRes = JsonParser.parseString(nonceStrRes).getAsJsonObject();
if (!nonceRes.get("success").getAsBoolean()) {
throw new Exception(nonceRes.get("error").getAsString());
}
Expand Down Expand Up @@ -90,12 +95,14 @@ private static String getToken() throws Exception {
""".formatted(clientNonce)))
.build();

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

return token;
}
Expand Down Expand Up @@ -140,4 +147,14 @@ public static boolean has(String name) throws Exception {

return client.send(req, HttpResponse.BodyHandlers.discarding()).statusCode() != 404;
}

public static String getVersion(String name) throws Exception {
HttpClient client = HttpClient.newHttpClient();

HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("https://raw.githubusercontent.com/McJsScripts/JSPMRegistry/master/packages/" + name + "/jspm.json"))
.build();

return JsonParser.parseString(client.send(req, HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject().getAsJsonObject("version").get("pkg").getAsString();
}
}
49 changes: 47 additions & 2 deletions src/main/java/de/blazemcworld/jsscripts/JsScriptsCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.*;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.sun.jna.platform.FileUtils;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.MinecraftVersion;
import net.minecraft.text.ClickEvent;
Expand Down Expand Up @@ -35,6 +36,7 @@ public void register() {
JsScripts.displayChat(Text.literal("/jsscripts disable - Remove a script from the auto-enable list.").formatted(Formatting.AQUA));
JsScripts.displayChat(Text.literal("/jsscripts upload - Upload a script to jspm.").formatted(Formatting.AQUA));
JsScripts.displayChat(Text.literal("/jsscripts download - Download a script from jspm.").formatted(Formatting.AQUA));
JsScripts.displayChat(Text.literal("/jsscripts update - Check for script updates from jspm.").formatted(Formatting.AQUA));
return 1;
})
.then(literal("reload")
Expand Down Expand Up @@ -219,6 +221,7 @@ public void register() {
MinecraftVersion.CURRENT.getName()
));
JsScripts.displayChat(Text.literal("Please update the newly created jspm.json file in the script if necessary, then retry.").formatted(Formatting.AQUA));
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));
return;
}

Expand Down Expand Up @@ -272,8 +275,14 @@ public void register() {
}

if (root.toFile().exists()) {
JsScripts.displayChat(Text.literal("Script already found locally! Delete it to re-download.").formatted(Formatting.RED));
return;
FileUtils fu = FileUtils.getInstance();
if (fu.hasTrash()) {
fu.moveToTrash(root.toFile());
JsScripts.displayChat(Text.literal("Moved previous script version to the trash.").formatted(Formatting.AQUA));
} else {
JsScripts.displayChat(Text.literal("Script already found locally! Delete it to re-download.").formatted(Formatting.RED));
return;
}
}

JsScripts.displayChat(Text.literal("Downloading script from JSPM...").formatted(Formatting.AQUA));
Expand All @@ -290,6 +299,42 @@ public void register() {
})
)
)
.then(literal("update")
.executes((e) -> {
new Thread(() -> {
int updates = 0;
for (File f : ScriptManager.modDir.resolve("scripts").toFile().listFiles()) {
try {
Path p = f.toPath().resolve("jspm.json");
if (!Files.exists(p)) {
JsScripts.displayChat(Text.literal(f.getName() + ": No jspm.json").formatted(Formatting.AQUA));
continue;
}
String ver = JsonParser.parseString(Files.readString(p)).getAsJsonObject().getAsJsonObject("version").get("pkg").getAsString();

try {
String remoteVer = JSPM.getVersion(f.getName());
if (ver.equals(remoteVer)) {
JsScripts.displayChat(Text.literal(f.getName() + ": Up to date. (" + ver + ")").formatted(Formatting.AQUA));
} else {
JsScripts.displayChat(Text.literal(f.getName() + ": Update available. (" + ver + " => " + remoteVer + ")").formatted(Formatting.GREEN)
.styled(s -> s.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/jsscripts download " + f.getName()))));
updates++;
}
} catch (Exception unknown) {
JsScripts.displayChat(Text.literal(f.getName() + ": No remote found.").formatted(Formatting.RED));
}
} catch (Exception err) {
JsScripts.displayChat(Text.literal(f.getName() + ": Unknown Error").formatted(Formatting.RED));
}
}
if (updates > 0) {
JsScripts.displayChat(Text.literal(updates + " updates found. Click them to update.").formatted(Formatting.GREEN));
}
}).start();
return 1;
})
)
));
}
}
5 changes: 0 additions & 5 deletions src/main/java/de/blazemcworld/jsscripts/TypingGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static void genTypesIn(String targets) {
Files.writeString(out.resolve("jsconfig.json"), """
{
"compilerOptions": {
"typeRoots": ["types/global.d.ts"],
"paths": {
"$*": ["./types/*"],
"#*": ["./scripts/*"]
Expand All @@ -57,10 +56,6 @@ public static void genTypesIn(String targets) {
out.toFile().mkdirs();
}

Files.writeString(out.resolve("global.d.ts"), """
declare const script: import("../types/de/blazemcworld/jsscripts/Script").default;
""");

JsScripts.displayChat(Text.literal("Scanning available classes...").formatted(Formatting.AQUA));

ClassLoader cl = JsScripts.class.getClassLoader();
Expand Down

0 comments on commit f2f5dfa

Please sign in to comment.