From e35444ebcb3de7d6593c27d9d7b6b06c95b74c0a Mon Sep 17 00:00:00 2001 From: Liyan Zhao Date: Sat, 26 Aug 2023 13:00:50 +0800 Subject: [PATCH] feat: requestFollow --- .../zly2006/reden/malilib/KeyCallbacks.kt | 5 +- .../zly2006/reden/malilib/MalilibSettings.kt | 4 - .../com/github/zly2006/reden/report/Report.kt | 82 +++++++++++++++---- 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/github/zly2006/reden/malilib/KeyCallbacks.kt b/src/main/java/com/github/zly2006/reden/malilib/KeyCallbacks.kt index 06674bab..45b9e7c8 100644 --- a/src/main/java/com/github/zly2006/reden/malilib/KeyCallbacks.kt +++ b/src/main/java/com/github/zly2006/reden/malilib/KeyCallbacks.kt @@ -3,6 +3,7 @@ package com.github.zly2006.reden.malilib import com.github.zly2006.reden.access.PlayerData.Companion.data import com.github.zly2006.reden.network.Rollback import com.github.zly2006.reden.render.BlockBorder +import com.github.zly2006.reden.report.onFunctionUsed import com.github.zly2006.reden.utils.sendMessage import com.github.zly2006.reden.utils.toBlockPos import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking @@ -11,16 +12,18 @@ import net.minecraft.world.GameMode fun configureKeyCallbacks(mc: MinecraftClient) { REDEN_CONFIG_KEY.keybind.setCallback { _, _ -> - mc.setScreen(configScreen()) + mc.setScreen(GuiConfigs()) true } UNDO_KEY.keybind.setCallback { _, _ -> + onFunctionUsed("undo") if (mc.interactionManager?.currentGameMode == GameMode.CREATIVE) { ClientPlayNetworking.send(Rollback(0)) true } else false } REDO_KEY.keybind.setCallback { _, _ -> + onFunctionUsed("redo") if (mc.interactionManager?.currentGameMode == GameMode.CREATIVE) { ClientPlayNetworking.send(Rollback(1)) true diff --git a/src/main/java/com/github/zly2006/reden/malilib/MalilibSettings.kt b/src/main/java/com/github/zly2006/reden/malilib/MalilibSettings.kt index ff05c76c..9024396c 100644 --- a/src/main/java/com/github/zly2006/reden/malilib/MalilibSettings.kt +++ b/src/main/java/com/github/zly2006/reden/malilib/MalilibSettings.kt @@ -79,7 +79,3 @@ fun createCommandHotkey(index: Int) { private val commands = (1..20).map(::createCommandHotkey) fun getAllOptions() = GENERIC_TAB + RVC_TAB + MICRO_TICK_TAB + SUPER_RIGHT_TAB + DEBUG_TAB - -fun configScreen(): GuiConfigs { - return GuiConfigs() -} \ No newline at end of file diff --git a/src/main/java/com/github/zly2006/reden/report/Report.kt b/src/main/java/com/github/zly2006/reden/report/Report.kt index d43bd3a7..48486960 100644 --- a/src/main/java/com/github/zly2006/reden/report/Report.kt +++ b/src/main/java/com/github/zly2006/reden/report/Report.kt @@ -4,13 +4,17 @@ import com.google.gson.Gson import net.fabricmc.loader.api.FabricLoader import net.minecraft.MinecraftVersion import net.minecraft.client.MinecraftClient +import net.minecraft.client.network.MultiplayerServerListPinger import net.minecraft.client.option.ServerList -import java.net.URI +import net.minecraft.text.ClickEvent +import net.minecraft.text.Text +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody import java.net.http.HttpClient -import java.net.http.HttpRequest -import java.net.http.HttpRequest.BodyPublishers -import java.net.http.HttpResponse import java.util.* +import java.util.concurrent.CompletableFuture class ClientMetadataReq( val online_mode: Boolean, @@ -31,29 +35,73 @@ fun initReport() { val mc = MinecraftClient.getInstance() val servers = ServerList(mc) servers.loadFile() + val serverPinger = MultiplayerServerListPinger() val metadata = ClientMetadataReq( online_mode = mc.session.accessToken != "FabricMC", uuid = mc.session.uuidOrNull, name = mc.session.username, mcversion = mc.gameVersion + " " + MinecraftVersion.create().name, servers = (0 until servers.size()).map { servers[it] }.map { - ClientMetadataReq.Server( - name = it.name, - ip = it.address, - motd = "" - ) - } + val future = CompletableFuture() + serverPinger.add(it) { + future.complete( + ClientMetadataReq.Server( + name = it.name, + ip = it.address, + motd = "${it.label}, online=${it.online}, players=${it.playerCountLabel}" + ) + ) + } + future + }.map { it.join() } ) try { if (!FabricLoader.getInstance().isDevelopmentEnvironment) { - httpClient.send( - HttpRequest.newBuilder(URI("https://slv4.starlight.cool:4321/mcdata/client")) - .POST(BodyPublishers.ofString(Gson().toJson(metadata))) - .header("Content-Type", "application/json") - .build(), - HttpResponse.BodyHandlers.ofString() - ) + OkHttpClient().newCall(Request.Builder().apply { + url("https://slv4.starlight.cool:4321/mcdata/client") + post(Gson().toJson(metadata).toRequestBody("application/json".toMediaTypeOrNull())) + header("Content-Type", "application/json") + }.build()).execute() } } catch (_: Exception) { } } + +private var usedTimes = 0 + +private fun requestFollow() { + val mc = MinecraftClient.getInstance() + val text = Text.literal( + if (mc.languageManager.language == "zh_cn") + "你已经使用本mod的功能$usedTimes 次了,如果觉得好用的话,可以点击关注一下作者的B站哦!" + else + "You have used this mod $usedTimes times. If you like it, please click to follow the author's Youtube!" + ).styled { + it.withColor(0x00ff00).withClickEvent( + ClickEvent( + ClickEvent.Action.OPEN_URL, + if (mc.languageManager.language == "zh_cn") + "https://space.bilibili.com/1545239761" + else + "https://www.youtube.com/@guratory" + ) + ) + } + mc.player?.sendMessage(text) +} + +private fun requestDonate() { +} + +fun onFunctionUsed(name: String) { + Thread { + // usage report + }.start() + usedTimes++ + if (usedTimes % 50 == 0 || usedTimes == 10) { + requestFollow() + } + if (usedTimes % 100 == 0 || usedTimes == 20) { + requestDonate() + } +}