From bb5d608b4cb3b87ab4ef0d31b04d0edded7f9929 Mon Sep 17 00:00:00 2001 From: Wzp-2008 Date: Tue, 8 Aug 2023 16:22:18 +0800 Subject: [PATCH] perf: remove testCode --- .../java/cn/wzpmc/DreamRunnerModClient.java | 166 +----------------- 1 file changed, 3 insertions(+), 163 deletions(-) diff --git a/src/client/java/cn/wzpmc/DreamRunnerModClient.java b/src/client/java/cn/wzpmc/DreamRunnerModClient.java index 3b30fa1..6bcb6c9 100644 --- a/src/client/java/cn/wzpmc/DreamRunnerModClient.java +++ b/src/client/java/cn/wzpmc/DreamRunnerModClient.java @@ -2,34 +2,20 @@ package cn.wzpmc; import cn.wzpmc.entities.SongObject; import cn.wzpmc.gui.OnWorldTickStart; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.mojang.brigadier.arguments.IntegerArgumentType; -import com.mojang.brigadier.builder.LiteralArgumentBuilder; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.minecraft.client.MinecraftClient; -import net.minecraft.server.command.CommandManager; -import net.minecraft.server.command.ServerCommandSource; import net.minecraft.util.Identifier; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; @Environment(EnvType.CLIENT) public class DreamRunnerModClient implements ClientModInitializer { @@ -37,157 +23,11 @@ public class DreamRunnerModClient implements ClientModInitializer { public static final Identifier packageChannelId = Objects.requireNonNull(Identifier.of("der", "music_channel")); public static OnWorldTickStart worldTickStart = new OnWorldTickStart(); public static List musics = new ArrayList<>(); - public boolean isLogin = false; - public String cookies = ""; @Override public void onInitializeClient() { ClientPlayNetworking.registerGlobalReceiver(packageChannelId, (client, handler, buf, responseSender) -> packetHandler(client, buf)); ClientTickEvents.START_CLIENT_TICK.register(worldTickStart); - CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(LiteralArgumentBuilder.literal("play").requires((e) -> isLogin).then(CommandManager.argument("id", IntegerArgumentType.integer()).executes((e) -> { - //https://music.163.com/song?id=1369828361&userid=347345537 - Long songId = Long.valueOf(e.getArgument("id", Integer.class)); - ByteBuf buffer = Unpooled.buffer(); - HttpClient.Builder builder = HttpClient.newBuilder(); - HttpClient build = builder.build(); - HttpRequest request = HttpRequest.newBuilder(URI.create("http://192.168.1.2:1200/song/url/v1?id=" + songId + "&level=exhigh")).setHeader("Cookie", cookies).build(); - try { - HttpResponse send = build.send(request, HttpResponse.BodyHandlers.ofString()); - JsonElement jsonElement = JsonParser.parseString(send.body()); - String url = jsonElement.getAsJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("url").getAsString(); - System.out.println(url); - HttpResponse music = build.send(HttpRequest.newBuilder(URI.create(url)).build(), HttpResponse.BodyHandlers.ofByteArray()); - HttpRequest.Builder builder1 = HttpRequest.newBuilder(URI.create("http://192.168.1.2:1200/song/detail?ids=" + songId)); - HttpResponse detailResponse = build.send(builder1.build(), HttpResponse.BodyHandlers.ofString()); - JsonObject asJsonObject = JsonParser.parseString(detailResponse.body()).getAsJsonObject().get("songs").getAsJsonArray().get(0).getAsJsonObject(); - String asString = asJsonObject.get("name").getAsString(); - int dt = asJsonObject.get("dt").getAsInt(); - buffer.writeInt(dt); - byte[] bytes1 = asString.getBytes(StandardCharsets.UTF_8); - buffer.writeInt(bytes1.length); - buffer.writeBytes(bytes1); - JsonArray arList = asJsonObject.get("ar").getAsJsonArray(); - int size = arList.size(); - buffer.writeInt(size); - for (JsonElement ar : arList) { - JsonObject jsonObject = ar.getAsJsonObject(); - String arName = jsonObject.get("name").getAsString(); - byte[] bytes = arName.getBytes(StandardCharsets.UTF_8); - buffer.writeInt(bytes.length); - buffer.writeBytes(bytes); - } - JsonObject alObject = asJsonObject.get("al").getAsJsonObject(); - String picURL = alObject.get("picUrl").getAsString(); - System.out.println(picURL); - HttpRequest.Builder builder3 = HttpRequest.newBuilder(URI.create("http://192.168.1.2:1200/lyric?id=" + songId)).setHeader("Cookie", this.cookies); - String lrc = JsonParser.parseString(build.send(builder3.build(), HttpResponse.BodyHandlers.ofString()).body()).getAsJsonObject().get("lrc").getAsJsonObject().get("lyric").getAsString(); - byte[] bytes = lrc.getBytes(StandardCharsets.UTF_8); - buffer.writeInt(bytes.length); - buffer.writeBytes(bytes); - HttpRequest.Builder builder2 = HttpRequest.newBuilder(URI.create(picURL)); - HttpResponse picResponse = build.send(builder2.build(), HttpResponse.BodyHandlers.ofByteArray()); - byte[] body1 = picResponse.body(); - buffer.writeInt(body1.length); - buffer.writeBytes(body1); - byte[] body = music.body(); - buffer.writeInt(body.length); - buffer.writeBytes(body); - } catch (IOException | InterruptedException ex) { - ex.printStackTrace(); - throw new RuntimeException(ex); - } - try { - packetHandler(MinecraftClient.getInstance(), buffer); - } catch (Throwable ea) { - ea.printStackTrace(); - } - return 0; - })))); - testLogin(); - - } - - private void testLogin() { - HttpClient httpClient = HttpClient.newHttpClient(); - try { - FileInputStream fileInputStream = new FileInputStream("Cookie"); - String cookie = new String(fileInputStream.readAllBytes()); - fileInputStream.close(); - HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create("http://192.168.1.2:1200/login/status?timestamp=" + new Date().getTime())).setHeader("Cookie", cookie); - HttpResponse send = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString()); - if (!JsonParser.parseString(send.body()).getAsJsonObject().get("data").getAsJsonObject().get("profile").isJsonNull()) { - this.cookies = cookie; - this.isLogin = true; - return; - } - } catch (IOException | InterruptedException e) { - e.printStackTrace(); - return; - } - HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create("http://192.168.1.2:1200/login/qr/key?timestamp=" + new Date().getTime())); - try { - HttpResponse send = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString()); - String body = send.body(); - JsonElement jsonElement = JsonParser.parseString(body); - String unikey = jsonElement.getAsJsonObject().get("data").getAsJsonObject().get("unikey").getAsString(); - HttpRequest.Builder createQr = HttpRequest.newBuilder(URI.create("http://192.168.1.2:1200/login/qr/create?qrimg=1&key=" + unikey)); - HttpResponse send1 = httpClient.send(createQr.build(), HttpResponse.BodyHandlers.ofString()); - String body1 = send1.body(); - JsonElement jsonElement1 = JsonParser.parseString(body1); - String imgB64 = jsonElement1.getAsJsonObject().get("data").getAsJsonObject().get("qrimg").getAsString().replace("data:image/png;base64,", ""); - byte[] decode = Base64.getDecoder().decode(imgB64); - FileOutputStream fileOutputStream = new FileOutputStream("qr.png"); - fileOutputStream.write(decode); - fileOutputStream.close(); - DreamRunnerMod.LOGGER.info(unikey); - new Thread(() -> { - w: - while (true) { - HttpRequest.Builder builder1 = HttpRequest.newBuilder(URI.create("http://192.168.1.2:1200/login/qr/check?key=" + unikey + "×tamp=" + new Date().getTime())); - try { - HttpResponse send2 = httpClient.send(builder1.build(), HttpResponse.BodyHandlers.ofString()); - JsonElement jsonElement2 = JsonParser.parseString(send2.body()); - int code = jsonElement2.getAsJsonObject().get("code").getAsInt(); - switch (code) { - case 801: - break; - case 802: - DreamRunnerMod.LOGGER.info("扫码成功等待确认"); - break; - case 803: - DreamRunnerMod.LOGGER.info("登录成功"); - this.loginSuccess(send2); - break w; - } - } catch (IOException | InterruptedException e) { - e.printStackTrace(); - } - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - }).start(); - } catch (IOException | InterruptedException e) { - e.printStackTrace(); - throw new RuntimeException(e); - } - } - - private void loginSuccess(HttpResponse send2) { - send2.headers().firstValue("set-cookie").ifPresent((e) -> { - this.isLogin = true; - this.cookies = e; - FileOutputStream fileOutputStream; - try { - fileOutputStream = new FileOutputStream("Cookie"); - fileOutputStream.write(e.getBytes(StandardCharsets.UTF_8)); - fileOutputStream.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - }); } // Packet Struct: