Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add neofetch in Minecraft #108

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions scripts/neofetch/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# generated by https://asciiart.club
import json

html = """

<span style="color:#808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#6e8160">a</span><span style="color:#6e825f">w</span><br><span style="color:#808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#6a8457">s</span><span style="color:#5e853f">#</span><span style="color:#58892d">#</span><span style="color:#528b25">S</span><span style="color:#4d8720">#$</span><span style="color:#49861e">#</span><span style="color:#578c2b">#</span><span style="color:#5d863d">#</span><span style="color:#678353">w</span><br><span style="color:#718464">a</span><span style="color:#65874d">#</span><span style="color:#60893a">#</span><span style="color:#548926">$</span><span style="color:#4b8820">###S#</span><span style="color:#4b8820">#</span><span style="color:#5d8d2d">G</span><span style="color:#518924">#</span><span style="color:#4e8921">###</span><span style="color:#4b851f">#8</span><span style="color:#5f8938">#</span><span style="color:#67854c">N</span><span style="color:#718062">-</span><br><span style="color:#4f8e24">#</span><span style="color:#528c25">#</span><span style="color:#4a861f">#</span><span style="color:#4d8821">#</span><span style="color:#528924">#</span><span style="color:#558826">S</span><span style="color:#568c28">#</span><span style="color:#588e2a">G</span><span style="color:#518a24">#Q</span><span style="color:#518a25">##</span><span style="color:#4b8720">#GS</span><span style="color:#568e28">#$</span><span style="color:#3e6f1a">#</span><span style="color:#335c15">#</span><span style="color:#2c5212">#</span><br><span style="color:#4f5f22">#</span><span style="color:#597e2b">G</span><span style="color:#558926">Q</span><span style="color:#485c1c">#</span><span style="color:#48881e">N</span><span style="color:#548a26">##</span><span style="color:#578b29">G##</span><span style="color:#47861d">#</span><span style="color:#47841c">#</span><span style="color:#578c29">S</span><span style="color:#4b8122">Q</span><span style="color:#436f1f">#</span><span style="color:#365c18">#</span><span style="color:#305515">#</span><span style="color:#2e4b13">##</span><span style="color:#2e2d0f">#</span><br><span style="color:#5c3c20">#</span><span style="color:#4f361d">#</span><span style="color:#4a5c1c">#</span><span style="color:#443618">##</span><span style="color:#4d8921">#</span><span style="color:#578c29">###</span><span style="color:#548a26">#</span><span style="color:#487b20">#</span><span style="color:#3b691a">#</span><span style="color:#2e5513">#</span><span style="color:#2d3810">#</span><span style="color:#2b4510">#</span><span style="color:#305315">#</span><span style="color:#325316">#</span><span style="color:#332910">#</span><span style="color:#32210f">##</span><br><span style="color:#684122">3</span><span style="color:#613e20">##</span><span style="color:#5c3d1f">#</span><span style="color:#4f3119">#</span><span style="color:#466a1b">#</span><span style="color:#588129">S</span><span style="color:#506422">#</span><span style="color:#497d1d">#</span><span style="color:#47851d">#</span><span style="color:#325417">#</span><span style="color:#315216">#</span><span style="color:#2e5213">#</span><span style="color:#2c1e0d">#</span><span style="color:#2d220e">#</span><span style="color:#2a310e">##</span><span style="color:#3b2412">#</span><span style="color:#3e2713">#</span><span style="color:#452d16">#</span><br><span style="color:#764a26">#</span><span style="color:#6c4423">#S</span><span style="color:#5f4020">#</span><span style="color:#4f331a">#</span><span style="color:#472d17">####</span><span style="color:#493f18">#</span><span style="color:#313512">#</span><span style="color:#342510">##</span><span style="color:#3d2713">###</span><span style="color:#4a2d16">####</span><br><span style="color:#6d4424">#</span><span style="color:#57361c">###</span><span style="color:#623d1f">#</span><span style="color:#744926">#</span><span style="color:#6f4624">#</span><span style="color:#5c3a1e">#</span><span style="color:#5e3b1e">##</span><span style="color:#3d2613">#</span><span style="color:#402815">#</span><span style="color:#3f2a18">########</span><br><span style="color:#6d4424">#</span><span style="color:#5a381d">#3</span><span style="color:#4f351b">#</span><span style="color:#4e3119">#</span><span style="color:#5e3b1e">#</span><span style="color:#6a4323">###</span><span style="color:#6b4222">#</span><span style="color:#482c16">#</span><span style="color:#432914">###</span><span style="color:#352010">####</span><span style="color:#462f18">#</span><span style="color:#342315">#</span><br><span style="color:#57361c">#</span><span style="color:#5f3c1f">#</span><span style="color:#6c4423">#</span><span style="color:#6f4d25">##</span><span style="color:#56361c">#</span><span style="color:#664021">#</span><span style="color:#643f20">##</span><span style="color:#5d3b1e">#</span><span style="color:#4b2e18">#</span><span style="color:#3c2412">#</span><span style="color:#3f2613">#</span><span style="color:#331f0f">###</span><span style="color:#4d3119">##</span><span style="color:#3f2a14">##</span><br><span style="color:#684122">#</span><span style="color:#56361c">#</span><span style="color:#593a1d">#</span><span style="color:#644121">#</span><span style="color:#6e4523">#</span><span style="color:#4c3724">#</span><span style="color:#764a27">#</span><span style="color:#5f3b1e">##</span><span style="color:#664221">#</span><span style="color:#452c17">#</span><span style="color:#3a2312">#</span><span style="color:#51331a">#</span><span style="color:#4c3017">#</span><span style="color:#432a15">#</span><span style="color:#3f2613">##</span><span style="color:#372010">#</span><span style="color:#4a2f17">#</span><span style="color:#432a16">#</span><br><span style="color:#756354">7</span><span style="color:#745336">%</span><span style="color:#6f4825">#</span><span style="color:#4c3419">#</span><span style="color:#603c1f">##</span><span style="color:#654021">#</span><span style="color:#55351b">##</span><span style="color:#59391d">#</span><span style="color:#422915">#</span><span style="color:#3e2613">####</span><span style="color:#3b2311">###</span><span style="color:#4b3926">M</span><span style="color:#595047">7</span><br><span style="color:#808080">&nbsp;&nbsp;&nbsp;</span><span style="color:#71675a">j</span><span style="color:#785a3f">%</span><span style="color:#664022">#</span><span style="color:#603b1e">#</span><span style="color:#4f3119">#</span><span style="color:#6e4925">#</span><span style="color:#7b4e28">N</span><span style="color:#3c2412">#</span><span style="color:#3a2312">##</span><span style="color:#321f10">#</span><span style="color:#392717">#</span><span style="color:#524030">M</span><span style="color:#655c53">7</span><br><span style="color:#808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#756a61">j</span><span style="color:#6d5641">7</span><span style="color:#654424">#</span><span style="color:#654021">#</span><span style="color:#422915">#</span><span style="color:#482d18">#</span><span style="color:#584838">C</span><span style="color:#69635c">h</span><br><span style="color:#808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#746e67">^</span><span style="color:#706b65">\</span>



""".replace('\n', '').replace("</span>", "").replace("<span", "").replace(" ", "")
lines = html.split("<br>")


def proc_line(s: str) -> list[dict]:
index = 0
result = []
length = 0
while index < len(s):
color_index = s.find("style=\"color:", index)
if color_index == -1:
result.append({
'text': s[index:],
})
break
else:
color = s[color_index + 13: color_index + 20]
str_start = s.find("\">", color_index) + 2
str_end = s.find("style=", str_start)
if str_end == -1:
str_end = len(s)
# in MC, the width of space is 4, but for chars, it's 6
text = s[str_start:str_end].replace("&nbsp;&nbsp;", " ").replace("&nbsp;", " ")
for ch in text:
if ch == ' ':
length += 4
else:
length += 6
result.append({
'text': text,
'color': color
})
index = str_end
result.append({
'text': " " * ((120 - length) // 4),
'color': "#000000"
})
return result


lines = [proc_line(line) for line in lines]
with open("../../src/main/resources/assets/reden/minefetch/.json", "w") as f:
f.write(json.dumps(lines))
88 changes: 84 additions & 4 deletions src/main/java/com/github/zly2006/reden/Reden.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,42 @@ import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper
import com.github.zly2006.reden.network.registerChannels
import com.github.zly2006.reden.rvc.registerRvc
import com.github.zly2006.reden.transformers.ThisIsReden
import com.github.zly2006.reden.utils.ResourceLoader
import com.github.zly2006.reden.utils.ResourceLoader.loadLang
import com.github.zly2006.reden.utils.TaskScheduler
import com.github.zly2006.reden.utils.isClient
import com.github.zly2006.reden.utils.server
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.mojang.blaze3d.platform.GlDebugInfo
import com.mojang.brigadier.arguments.StringArgumentType
import com.redenmc.bragadier.ktdsl.register
import com.redenmc.bragadier.ktdsl.then
import fi.dy.masa.litematica.render.LitematicaRenderer
import fi.dy.masa.litematica.world.SchematicWorldHandler
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
import net.fabricmc.loader.api.FabricLoader
import net.fabricmc.loader.api.Version
import net.minecraft.SharedConstants
import net.minecraft.client.MinecraftClient
import net.minecraft.command.argument.BlockPosArgumentType
import net.minecraft.command.argument.BlockStateArgumentType
import net.minecraft.command.argument.ItemStackArgumentType
import net.minecraft.entity.EntityStatuses
import net.minecraft.network.packet.s2c.play.EntityStatusS2CPacket
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import org.jetbrains.annotations.Contract
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.time.Duration.Companion.milliseconds


class Reden : ModInitializer, CarpetExtension {
Expand All @@ -69,6 +74,8 @@ class Reden : ModInitializer, CarpetExtension {
}
}

private var serverStartTime: Long = 0

override fun version(): String {
return "reden"
}
Expand All @@ -86,6 +93,7 @@ class Reden : ModInitializer, CarpetExtension {
ServerLifecycleEvents.SERVER_STARTING.register {
UpdateMonitorHelper.cleanup()
server = it
serverStartTime = System.currentTimeMillis()
}
registerChannels()
CarpetServer.manageExtension(this)
Expand All @@ -99,6 +107,78 @@ class Reden : ModInitializer, CarpetExtension {
1
}
}
literal("minefetch").executes { context ->
val logo =
try {
context.getArgument("logo", String::class.java)
} catch (_: IllegalArgumentException) {
null
} ?: "grass_block"
val ja = Gson().fromJson(
ResourceLoader.loadString("assets/reden/minefetch/$logo.json"),
JsonArray::class.java
)
fun color(s: String) = s.map {
Text.literal("█").formatted(Formatting.byCode(it))
}.fold(Text.empty()) { acc, text -> acc.append(text) }

val lineSeq = buildList<Text> {
// @formatter:off
add(Text.literal(context.source.name))
add(Text.literal("-".repeat(context.source.name.length)))
add(Text.literal("Minecraft ${SharedConstants.getGameVersion().name} (${server.serverModName})"))
add(Text.literal("Reden $MOD_VERSION"))
add(Text.literal("OS: ${System.getProperty("os.name")} ${System.getProperty("os.version")} ${System.getProperty("os.arch")}"))
add(Text.literal("Java: " + System.getProperty("java.vm.name")
.replace("vm", "", true)
.replace("server", "", true)
.trim() + " " + System.getProperty("java.vm.version")))
add(Text.literal("MC Memory: ${(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024}/${Runtime.getRuntime().maxMemory() / 1024 / 1024} MiB"))
add(Text.literal("Server Uptime: ${(System.currentTimeMillis() - serverStartTime).milliseconds}"))
// @formatter:on
if (isClient) {
val mc = MinecraftClient.getInstance()
runBlocking(mc.asCoroutineDispatcher()) {
buildList {
add(Text.literal("CPU: ${GlDebugInfo.getCpuInfo()}"))
add(Text.literal("Display: ${mc.window.framebufferWidth}x${mc.window.framebufferHeight} (${GlDebugInfo.getVendor()})"))
add(Text.literal("Driver: ${GlDebugInfo.getRenderer()}"))
add(Text.literal("OpenGL: ${GlDebugInfo.getVersion()}"))
}
}.let { addAll(it) }
}
add(Text.empty())
add(color("01234567"))
add(color("89abcdef"))
}.iterator()
var textLength = 0
ja.asSequence().forEach {
val text = Text.empty()
for (t in it.asJsonArray) {
text.append(Text.Serialization.fromJsonTree(t))
}
if (lineSeq.hasNext()) {
text.append(" ").apply { }
textLength = text.string.length
text.append(lineSeq.next())
}
context.source.sendMessage(text)
}
lineSeq.forEachRemaining {
context.source.sendMessage(Text.literal(" ".repeat(textLength)).append(it))
}
1
}
}
// forks etc
dispatcher.register {
literal("minefetch") {
literal("--logo") {
// todo: brigadier bug
argument("logo", StringArgumentType.word())
.redirect(dispatcher.root.getChild("minefetch"))
}
}
}
// Debug command
if (isRedenDev) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/github/zly2006/reden/RedenClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ class RedenClient : ClientModInitializer {
relaunch(null)
1
}
literal("neofetch") {
literal("chars").executes {
val renderer = MinecraftClient.getInstance().textRenderer
val width = renderer.getWidth(" ")
(0x20..0x7E)
.map { it.toChar() }
.groupBy { renderer.getWidth(it.toString()) }
.forEach {
println("${it.key}: ${it.value.joinToString("")}")
}
1
}
}
Comment on lines +163 to +174
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this command used for?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get chars that have the same width to generate ascii art

}
}
})
Expand Down
Loading
Loading