Skip to content

Commit

Permalink
feat: requestFollow
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Aug 26, 2023
1 parent d956599 commit e35444e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
82 changes: 65 additions & 17 deletions src/main/java/com/github/zly2006/reden/report/Report.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<ClientMetadataReq.Server>()
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()
}
}

0 comments on commit e35444e

Please sign in to comment.