Skip to content

Commit c4a47e0

Browse files
committed
Add mclo.gs / hotfix command
1 parent f80af49 commit c4a47e0

9 files changed

Lines changed: 115 additions & 27 deletions

File tree

Core/src/main/kotlin/io/github/grassproject/framework/core/GPPlugin.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ abstract class GPPlugin : JavaPlugin(), GPFramework {
1212
lateinit var logger: GPLogger
1313

1414
override fun onLoad() {
15+
logger = GPLogger(this)
1516
load()
1617
}
1718

@@ -22,13 +23,12 @@ abstract class GPPlugin : JavaPlugin(), GPFramework {
2223
return
2324
}
2425

25-
enable()
2626
GPFrameworkEngine.register(this)
27-
logger=GPLogger(this)
27+
enable()
2828
}
2929

3030
override fun onDisable() {
31-
disable()
3231
GPFrameworkEngine.unregister(this)
32+
disable()
3333
}
3434
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.github.grassproject.framework.util
2+
3+
import java.io.File
4+
import java.net.URLEncoder
5+
import java.net.http.HttpClient
6+
import java.net.http.HttpRequest
7+
import java.net.http.HttpResponse
8+
import java.net.URI
9+
10+
object McLogsUtil {
11+
12+
private val logFile = File("logs/latest.log")
13+
private val client = HttpClient.newHttpClient()
14+
15+
fun readLogs(): List<String> = logFile.takeIf { it.exists() }?.readLines() ?: emptyList()
16+
17+
fun getLastLogs(limit: Int = 10): List<String> = readLogs().takeLast(limit)
18+
19+
fun findLogs(keyword: String): List<String> =
20+
readLogs().filter { it.contains(keyword, ignoreCase = true) }
21+
22+
fun uploadLogs(): String? {
23+
val logText = readTextSafe().takeIf { it.isNotBlank() } ?: return null
24+
25+
return runCatching {
26+
val body = "content=${URLEncoder.encode(logText, "UTF-8")}"
27+
val request = HttpRequest.newBuilder()
28+
.uri(URI.create("https://api.mclo.gs/1/log"))
29+
.header("Content-Type", "application/x-www-form-urlencoded")
30+
.POST(HttpRequest.BodyPublishers.ofString(body))
31+
.build()
32+
33+
val response = client.send(request, HttpResponse.BodyHandlers.ofString()).body()
34+
"\"id\":\"".let { id ->
35+
if (response.contains(id)) "https://mclo.gs/${response.substringAfter(id).substringBefore("\"")}"
36+
else null
37+
}
38+
}.getOrNull()
39+
}
40+
41+
private fun readTextSafe(): String = runCatching { logFile.readText() }.getOrDefault("")
42+
}

Core/src/main/kotlin/io/github/grassproject/framework/util/bukkit/MinecraftVersion.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ enum class MinecraftVersion(val versionString: String) {
1010
V1_20_5("1.20.5"),
1111
V1_20_6("1.20.6"),
1212
V1_21("1.21.0"),
13-
V1_21_2("1.21.2"),
1413
V1_21_1("1.21.1"),
14+
V1_21_2("1.21.2"),
1515
V1_21_4("1.21.4"),
1616
V1_21_5("1.21.5"),
1717
V1_21_6("1.21.6"),
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.grassproject.framework.util.item
22

3+
import io.github.grassproject.framework.item.ItemBuilder
34
import org.bukkit.inventory.ItemStack
45

56
fun ItemStack.encode(): String {
@@ -8,4 +9,6 @@ fun ItemStack.encode(): String {
89

910
fun String.decodeToItemStack(): ItemStack {
1011
return ItemEncoder.decode(this)
11-
}
12+
}
13+
14+
fun ItemStack.toItemBuilder(): ItemBuilder = ItemBuilder(this).clone()

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "io.github.grassproject"
10-
version = "0.2-RC4"
10+
version = "0.2-RC5"
1111

1212
val exposed = "1.0.0-beta-5"
1313

src/main/kotlin/io/github/grassproject/framework/GPFrameworkPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class GPFrameworkPlugin : GPPlugin() {
2121

2222
override fun enable() {
2323
if (GithubAPI.isLatest(this)) {
24-
val data=GithubAPI.getLatest()
24+
val data = GithubAPI.getLatest()
2525
logger.bug("New Version Released! ${data["published"]}")
2626
logger.bug("New: ${data["ver"]}, Now: $version")
2727
}

src/main/kotlin/io/github/grassproject/framework/commands/GPFCommand.kt

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package io.github.grassproject.framework.commands
33
import io.github.grassproject.framework.GPFrameworkPlugin
44
import io.github.grassproject.framework.core.GPFrameworkEngine
55
import io.github.grassproject.framework.command.GPCommand
6+
import io.github.grassproject.framework.message.GPFTranslate
7+
import io.github.grassproject.framework.util.McLogsUtil
68
import io.github.grassproject.framework.util.component.toMiniMessage
7-
import io.github.grassproject.framework.utils.translate
89
import org.bukkit.command.CommandSender
910

1011
class GPFCommand: GPCommand<GPFrameworkPlugin>(
@@ -14,34 +15,70 @@ class GPFCommand: GPCommand<GPFrameworkPlugin>(
1415
) {
1516
private val prefix = "<#96f19c>GPF</#96f19c><#989c99> | </#989c99>".toMiniMessage()
1617
override fun execute(sender: CommandSender, args: Array<out String>): Boolean {
17-
if (args.size == 1) {
18-
if (args[0]=="info") {
19-
translate.fromList(
20-
"command.gpf.info", mapOf(
21-
"server" to sender.server.name,
22-
"version" to sender.server.version,
23-
"nms" to sender.server.bukkitVersion,
24-
"name" to plugin.name,
25-
"ver" to plugin.version,
26-
"list" to GPFrameworkEngine.listPlugins().joinToString(", ")
27-
)
28-
).forEach { sender.sendMessage(prefix.append { it.toMiniMessage() }) }
29-
}
30-
31-
if (args[0]=="") {
32-
33-
}
18+
if (args.isEmpty()) return true
19+
20+
when (args[0].lowercase()) {
21+
"info" -> info(sender)
22+
"dump" -> dump(sender)
3423
}
24+
25+
// if (args.size == 1) {
26+
// if (args[0]=="info") {
27+
// GPFTranslate.fromList(
28+
// "command.gpf.info", mapOf(
29+
// "server" to sender.server.name,
30+
// "version" to sender.server.version,
31+
// "nms" to sender.server.bukkitVersion,
32+
// "name" to plugin.name,
33+
// "ver" to plugin.version,
34+
// "list" to GPFrameworkEngine.listPlugins().joinToString(", ")
35+
// )
36+
// ).forEach { sender.sendMessage(prefix.append { it.toMiniMessage() }) }
37+
// }
38+
//
39+
// if (args[0]=="dump") {
40+
// val logFile = File("logs/latest.log")
41+
// if (!logFile.exists()) {
42+
// sender.sendMessage(prefix.append { "<red>로그 파일을 찾을 수 없습니다.</red>".toMiniMessage() })
43+
// return true
44+
// }
45+
// }
46+
//
47+
// if (args[0]=="") {}
48+
// }
3549
return true
3650
}
3751

3852
override fun tabComplete(sender: CommandSender, args: Array<out String>): List<String> {
3953
val tab = mutableListOf<String>()
40-
if (args.size==1) {
41-
tab.addAll(arrayOf("info"))
54+
if (args.size == 1) {
55+
tab.addAll(arrayOf("info", "dump"))
4256
}
4357
return tab
4458
}
59+
60+
private fun info(sender: CommandSender) {
61+
GPFTranslate.fromList(
62+
"command.gpf.info",
63+
mapOf(
64+
"server" to sender.server.name,
65+
"version" to sender.server.version,
66+
"nms" to sender.server.bukkitVersion,
67+
"name" to plugin.name,
68+
"ver" to plugin.version,
69+
"list" to GPFrameworkEngine.listPlugins().joinToString(", ")
70+
)
71+
).forEach { sender.sendMessage(prefix.append { it.toMiniMessage() }) }
72+
}
73+
74+
private fun dump(sender: CommandSender) {
75+
val link = McLogsUtil.uploadLogs()
76+
val message = if (link != null)
77+
"<green>Log dump successful</green> <gray>$link</gray>"
78+
else "<red>Log dump failed</red>"
79+
80+
sender.sendMessage(prefix.append { message.toMiniMessage() })
81+
}
4582
//
4683
// sorted()
4784
// fun listPluginsColored(): List<String> {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.grassproject.framework.message
2+
3+
import io.github.grassproject.framework.GPFrameworkPlugin
4+
5+
object GPFTranslate : GTranslate<GPFrameworkPlugin>(GPFrameworkPlugin.instance)

src/main/kotlin/io/github/grassproject/framework/utils/translate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.google.gson.JsonObject
55
import io.github.grassproject.framework.GPFrameworkPlugin
66
import java.io.File
77

8+
@Deprecated("GPFTranslate")
89
object translate {
910
private val plugin = GPFrameworkPlugin.instance
1011

0 commit comments

Comments
 (0)