feat: wowmine: more abilites to debug version
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
package net.worldofsosal.clientinfo;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import net.neoforged.fml.loading.FMLPaths;
|
||||
|
||||
public record PackInfo(String name, String version, String minecraft, String neoforge) {
|
||||
private static final PackInfo UNKNOWN = new PackInfo("unknown pack", "unknown", "unknown", "unknown");
|
||||
|
||||
private static final class Loaded {
|
||||
private static final PackInfo INFO = read();
|
||||
}
|
||||
|
||||
public static PackInfo current() {
|
||||
return Loaded.INFO;
|
||||
}
|
||||
|
||||
private static PackInfo read() {
|
||||
Path path = FMLPaths.CONFIGDIR.get().resolve("worldofsosal-version.json");
|
||||
try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
|
||||
JsonReader jsonReader = new JsonReader(reader)) {
|
||||
JsonElement json = new Gson().getAdapter(JsonElement.class).read(jsonReader);
|
||||
if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
|
||||
throw new JsonParseException("Metadata must contain one JSON object");
|
||||
}
|
||||
if (!json.isJsonObject()) {
|
||||
throw new JsonParseException("Metadata must be an object");
|
||||
}
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
return new PackInfo(
|
||||
field(object, "name"), field(object, "version"),
|
||||
field(object, "minecraft"), field(object, "neoforge"));
|
||||
} catch (IOException | JsonParseException exception) {
|
||||
LogUtils.getLogger().warn("Pack metadata missing or invalid at {}; displaying unknown", path, exception);
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
private static String field(JsonObject object, String key) {
|
||||
JsonElement value = object.get(key);
|
||||
if (value == null || !value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) {
|
||||
throw new JsonParseException("Metadata field must be a string: " + key);
|
||||
}
|
||||
String text = value.getAsString();
|
||||
if (text.isBlank() || text.codePoints().anyMatch(Character::isISOControl)) {
|
||||
throw new JsonParseException("Metadata field must be nonblank and single-line: " + key);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
public String display() {
|
||||
return name + " " + version + " | Minecraft " + minecraft + " | NeoForge " + neoforge;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package net.worldofsosal.clientinfo;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.neoforged.api.distmarker.Dist;
|
||||
import net.neoforged.fml.common.Mod;
|
||||
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
|
||||
@Mod(value = "worldofsosal_client_info", dist = Dist.CLIENT)
|
||||
public final class WorldOfSosalClientInfo {
|
||||
public WorldOfSosalClientInfo() {
|
||||
NeoForge.EVENT_BUS.addListener(WorldOfSosalClientInfo::onLogin);
|
||||
}
|
||||
|
||||
private static void onLogin(ClientPlayerNetworkEvent.LoggingIn event) {
|
||||
if (!event.getConnection().isMemoryConnection()) {
|
||||
Minecraft.getInstance().gui.getChat().addMessage(
|
||||
Component.literal("CLIENT: " + PackInfo.current().display()));
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package net.worldofsosal.clientinfo.mixin;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.worldofsosal.clientinfo.PackInfo;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(Minecraft.class)
|
||||
public abstract class MinecraftTitleMixin {
|
||||
@Inject(method = "createTitle()Ljava/lang/String;", at = @At("RETURN"), cancellable = true)
|
||||
private void worldofsosalClientInfo$title(CallbackInfoReturnable<String> callback) {
|
||||
callback.setReturnValue(PackInfo.current().display() + " | " + callback.getReturnValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
modLoader="javafml"
|
||||
loaderVersion="[4,)"
|
||||
license="All Rights Reserved"
|
||||
|
||||
[[mods]]
|
||||
modId="worldofsosal_client_info"
|
||||
version="1.0.0"
|
||||
displayName="WorldOfSosal Client Info"
|
||||
description='''Local pack information in the window title and client chat.'''
|
||||
|
||||
[[mixins]]
|
||||
config="worldofsosal-client-info.mixins.json"
|
||||
|
||||
[[dependencies.worldofsosal_client_info]]
|
||||
modId="neoforge"
|
||||
type="required"
|
||||
versionRange="[21.1.250]"
|
||||
ordering="NONE"
|
||||
side="CLIENT"
|
||||
|
||||
[[dependencies.worldofsosal_client_info]]
|
||||
modId="minecraft"
|
||||
type="required"
|
||||
versionRange="[1.21.1]"
|
||||
ordering="NONE"
|
||||
side="CLIENT"
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "net.worldofsosal.clientinfo.mixin",
|
||||
"compatibilityLevel": "JAVA_21",
|
||||
"client": [
|
||||
"MinecraftTitleMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user