From b1a65909b79abe3e4ec2e3269a4f6061df75a857 Mon Sep 17 00:00:00 2001 From: 3093FengMing <83573615+3093FengMing@users.noreply.github.com> Date: Sat, 21 Jan 2023 10:55:55 +0800 Subject: [PATCH] 1.2.6 clean up code --- .../fengming/vaultpatcher/VaultPatcher.java | 4 -- .../vaultpatcher/command/ExportCommand.java | 1 - .../vaultpatcher/config/DebugMode.java | 24 ++++++---- .../vaultpatcher/config/PatchInfo.java | 45 +++++++++++-------- .../vaultpatcher/config/TargetClassInfo.java | 16 +++++-- .../vaultpatcher/config/TranslationInfo.java | 1 - .../config/VaultPatcherConfig.java | 21 ++++++--- .../config/VaultPatcherPatch.java | 25 +++++------ .../mixin/BaseComponentMixin.java | 7 ++- .../vaultpatcher/mixin/FontMixin.java | 10 ----- .../mixin/TextComponentMixin.java | 6 +-- src/main/resources/mixins.vaultpatcher.json | 8 ++-- 12 files changed, 90 insertions(+), 78 deletions(-) diff --git a/src/main/java/me/fengming/vaultpatcher/VaultPatcher.java b/src/main/java/me/fengming/vaultpatcher/VaultPatcher.java index 97d6962..5cf825b 100644 --- a/src/main/java/me/fengming/vaultpatcher/VaultPatcher.java +++ b/src/main/java/me/fengming/vaultpatcher/VaultPatcher.java @@ -3,13 +3,9 @@ import com.mojang.logging.LogUtils; import me.fengming.vaultpatcher.config.VaultPatcherConfig; import me.fengming.vaultpatcher.config.VaultPatcherPatch; -import net.minecraft.network.chat.Component; -import net.minecraft.server.Bootstrap; import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; -import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent; import org.slf4j.Logger; diff --git a/src/main/java/me/fengming/vaultpatcher/command/ExportCommand.java b/src/main/java/me/fengming/vaultpatcher/command/ExportCommand.java index 91b392e..88d1ec4 100644 --- a/src/main/java/me/fengming/vaultpatcher/command/ExportCommand.java +++ b/src/main/java/me/fengming/vaultpatcher/command/ExportCommand.java @@ -4,7 +4,6 @@ import com.google.gson.reflect.TypeToken; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; -import net.minecraft.client.Minecraft; import net.minecraft.commands.CommandSourceStack; import net.minecraft.network.chat.TranslatableComponent; import net.minecraftforge.fml.loading.FMLPaths; diff --git a/src/main/java/me/fengming/vaultpatcher/config/DebugMode.java b/src/main/java/me/fengming/vaultpatcher/config/DebugMode.java index 4f0e887..62bb750 100644 --- a/src/main/java/me/fengming/vaultpatcher/config/DebugMode.java +++ b/src/main/java/me/fengming/vaultpatcher/config/DebugMode.java @@ -14,14 +14,14 @@ public class DebugMode { private String outputFormat = "%s -> %s"; - public void setEnable(boolean enable) { - isEnable = enable; - } - public boolean isEnable() { return isEnable; } + public void setEnable(boolean enable) { + isEnable = enable; + } + public int getOutputMode() { return outputMode; } @@ -42,10 +42,18 @@ public void readJson(JsonReader reader) throws IOException { reader.beginObject(); while (reader.peek() != JsonToken.END_OBJECT) { switch (reader.nextName()) { - case "is_enable" : setEnable(reader.nextBoolean()); break; - case "output_format" : setOutputFormat(reader.nextString()); break; - case "output_mode" : setOutputMode(reader.nextInt()); break; - default : reader.skipValue(); break; + case "is_enable": + setEnable(reader.nextBoolean()); + break; + case "output_format": + setOutputFormat(reader.nextString()); + break; + case "output_mode": + setOutputMode(reader.nextInt()); + break; + default: + reader.skipValue(); + break; } } reader.endObject(); diff --git a/src/main/java/me/fengming/vaultpatcher/config/PatchInfo.java b/src/main/java/me/fengming/vaultpatcher/config/PatchInfo.java index 96238f2..a7c2f8d 100644 --- a/src/main/java/me/fengming/vaultpatcher/config/PatchInfo.java +++ b/src/main/java/me/fengming/vaultpatcher/config/PatchInfo.java @@ -19,31 +19,30 @@ public String getName() { return name; } - public String getDesc() { - return desc; - } - - public String getMods() { - return mods; - } - - public String getAuthors() { - return authors; - } - - public void setName(String name) { this.name = name; } + public String getDesc() { + return desc; + } + public void setDesc(String desc) { this.desc = desc; } + public String getMods() { + return mods; + } + public void setMods(String mods) { this.mods = mods; } + public String getAuthors() { + return authors; + } + public void setAuthors(String authors) { this.authors = authors; } @@ -53,11 +52,21 @@ public void readJson(JsonReader reader) throws IOException { reader.beginObject(); while (reader.peek() != JsonToken.END_OBJECT) { switch (reader.nextName()) { - case "name" : setName(reader.nextString()); break; - case "desc" : setDesc(reader.nextString()); break; - case "mods" : setMods(reader.nextString()); break; - case "authors" : setAuthors(reader.nextString()); break; - default : reader.skipValue(); break; + case "name": + setName(reader.nextString()); + break; + case "desc": + setDesc(reader.nextString()); + break; + case "mods": + setMods(reader.nextString()); + break; + case "authors": + setAuthors(reader.nextString()); + break; + default: + reader.skipValue(); + break; } } reader.endObject(); diff --git a/src/main/java/me/fengming/vaultpatcher/config/TargetClassInfo.java b/src/main/java/me/fengming/vaultpatcher/config/TargetClassInfo.java index a3817ac..50a5922 100644 --- a/src/main/java/me/fengming/vaultpatcher/config/TargetClassInfo.java +++ b/src/main/java/me/fengming/vaultpatcher/config/TargetClassInfo.java @@ -19,10 +19,18 @@ public void readJson(JsonReader reader) throws IOException { reader.beginObject(); while (reader.peek() != JsonToken.END_OBJECT) { switch (reader.nextName()) { - case "name" : setName(reader.nextString()); break; - case "mapping" : setMapping(reader.nextString()); break; - case "stack_depth" : setStackDepth(reader.nextInt()); break; - default : reader.skipValue(); break; + case "name": + setName(reader.nextString()); + break; + case "mapping": + setMapping(reader.nextString()); + break; + case "stack_depth": + setStackDepth(reader.nextInt()); + break; + default: + reader.skipValue(); + break; } } reader.endObject(); diff --git a/src/main/java/me/fengming/vaultpatcher/config/TranslationInfo.java b/src/main/java/me/fengming/vaultpatcher/config/TranslationInfo.java index d3b4004..2cf9dd9 100644 --- a/src/main/java/me/fengming/vaultpatcher/config/TranslationInfo.java +++ b/src/main/java/me/fengming/vaultpatcher/config/TranslationInfo.java @@ -2,7 +2,6 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; diff --git a/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherConfig.java b/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherConfig.java index 910f6fa..7633579 100644 --- a/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherConfig.java +++ b/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherConfig.java @@ -7,16 +7,20 @@ import com.google.gson.stream.JsonWriter; import net.minecraftforge.fml.loading.FMLPaths; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.List; public class VaultPatcherConfig { private static final Gson GSON = new Gson(); private static final Path configFile = FMLPaths.CONFIGDIR.get().resolve("vaultpatcher").resolve("config.json"); private static List mods = new ArrayList<>(); - private static DebugMode debug = new DebugMode(); + private static final DebugMode debug = new DebugMode(); public static List getMods() { return mods; @@ -48,17 +52,20 @@ public static void readConfig() throws IOException { jr.beginObject(); while (jr.peek() != JsonToken.END_OBJECT) { switch (jr.nextName()) { - case "debug_mode" : + case "debug_mode": if (jr.peek() == JsonToken.BEGIN_OBJECT) { debug.readJson(jr); } break; - case "mods" : + case "mods": if (jr.peek() == JsonToken.BEGIN_ARRAY) { - mods = GSON.fromJson(jr, new TypeToken>() {}.getType()); + mods = GSON.fromJson(jr, new TypeToken>() { + }.getType()); } break; - default : jr.skipValue(); break; + default: + jr.skipValue(); + break; } } jr.endObject(); diff --git a/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherPatch.java b/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherPatch.java index 23f49ce..efa5c5b 100644 --- a/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherPatch.java +++ b/src/main/java/me/fengming/vaultpatcher/config/VaultPatcherPatch.java @@ -5,7 +5,6 @@ import com.google.gson.stream.JsonToken; import me.fengming.vaultpatcher.VaultPatcher; import net.minecraft.client.resources.language.I18n; -import net.minecraft.server.Bootstrap; import net.minecraftforge.fml.loading.FMLPaths; import java.io.FileInputStream; @@ -17,6 +16,12 @@ public class VaultPatcherPatch { private static final Gson GSON = new Gson(); + private static boolean isSemimatch = false; + private final Path patchFile; + + private Map> map = new HashMap<>(); + + private PatchInfo info = new PatchInfo(); public VaultPatcherPatch(String patchFile) { VaultPatcher.LOGGER.info("Load Module " + patchFile); @@ -30,12 +35,6 @@ public VaultPatcherPatch(String patchFile) { this.patchFile = p; } - private final Path patchFile; - - private Map> map = new HashMap<>(); - - private PatchInfo info = new PatchInfo(); - private static void addEntry(Map> p, K key, T val) { p.computeIfAbsent(key, k -> new ArrayList<>()).add(val); } @@ -66,7 +65,7 @@ public void readConfig() throws IOException { readConfig(jsonReader); } } - + private List getList(String str) { Set set = map.keySet(); for (String s : set) { @@ -77,8 +76,6 @@ private List getList(String str) { return null; } - private static boolean isSemimatch = false; - public String patch(String text, StackTraceElement[] stackTrace) { List list; if ((list = getList(text)) == null) return null; @@ -86,7 +83,8 @@ public String patch(String text, StackTraceElement[] stackTrace) { for (TranslationInfo info : list) { isSemimatch = info.getValue().startsWith("@"); if (!isSemimatch && !text.equals(info.getKey())) continue; - if (info.getValue() == null || info.getKey() == null || info.getKey().isEmpty() || info.getValue().isEmpty()) continue; + if (info.getValue() == null || info.getKey() == null || info.getKey().isEmpty() || info.getValue().isEmpty()) + continue; final TargetClassInfo targetClassInfo = info.getTargetClassInfo(); if (targetClassInfo.getName().isEmpty() || targetClassInfo.getStackDepth() <= 0 || matchStack(targetClassInfo.getName(), stackTrace)) { return patchText(info.getValue(), info.getKey(), text); @@ -103,9 +101,8 @@ public String patch(String text, StackTraceElement[] stackTrace) { private boolean matchStack(String str, StackTraceElement[] stack) { String s = str.toLowerCase(); - List stackTrace = Arrays.stream(stack).toList(); - stackTrace = stackTrace.subList(7, stackTrace.size() - 13); - for (StackTraceElement ste : stackTrace) { + stack = Arrays.copyOfRange(stack, 7, 13); + for (StackTraceElement ste : stack) { if (s.startsWith("#")) { return ste.getClassName().endsWith(s); } else if (s.startsWith("@")) { diff --git a/src/main/java/me/fengming/vaultpatcher/mixin/BaseComponentMixin.java b/src/main/java/me/fengming/vaultpatcher/mixin/BaseComponentMixin.java index 5403b4e..68c43c0 100644 --- a/src/main/java/me/fengming/vaultpatcher/mixin/BaseComponentMixin.java +++ b/src/main/java/me/fengming/vaultpatcher/mixin/BaseComponentMixin.java @@ -32,9 +32,12 @@ private void proxy_getSiblings(CallbackInfoReturnable> cir) { } */ - @Shadow public abstract MutableComponent copy(); + @Shadow + @Final + protected List siblings; - @Shadow @Final protected List siblings; + @Shadow + public abstract MutableComponent copy(); @ModifyArg( method = "getVisualOrderText", diff --git a/src/main/java/me/fengming/vaultpatcher/mixin/FontMixin.java b/src/main/java/me/fengming/vaultpatcher/mixin/FontMixin.java index 9be10c8..ecf0ebf 100644 --- a/src/main/java/me/fengming/vaultpatcher/mixin/FontMixin.java +++ b/src/main/java/me/fengming/vaultpatcher/mixin/FontMixin.java @@ -2,21 +2,11 @@ import me.fengming.vaultpatcher.ThePatcher; import net.minecraft.client.gui.Font; -import net.minecraft.network.chat.FormattedText; -import net.minecraft.network.chat.Style; -import net.minecraft.network.chat.TextComponent; -import net.minecraft.util.FormattedCharSequence; -import net.minecraft.util.FormattedCharSink; -import net.minecraft.util.StringDecomposer; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyArg; -import org.spongepowered.asm.mixin.injection.ModifyVariable; import java.util.Objects; -import java.util.Optional; - -import static me.fengming.vaultpatcher.VaultPatcher.exportList; @Mixin(value = Font.class, priority = Integer.MAX_VALUE) public class FontMixin { diff --git a/src/main/java/me/fengming/vaultpatcher/mixin/TextComponentMixin.java b/src/main/java/me/fengming/vaultpatcher/mixin/TextComponentMixin.java index 9ef747c..4e96420 100644 --- a/src/main/java/me/fengming/vaultpatcher/mixin/TextComponentMixin.java +++ b/src/main/java/me/fengming/vaultpatcher/mixin/TextComponentMixin.java @@ -2,17 +2,12 @@ import me.fengming.vaultpatcher.ThePatcher; import net.minecraft.network.chat.TextComponent; -import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import static me.fengming.vaultpatcher.VaultPatcher.exportList; - @Mixin(value = TextComponent.class, priority = Integer.MAX_VALUE) public abstract class TextComponentMixin { @@ -24,6 +19,7 @@ private void proxy_getContents(CallbackInfoReturnable cir) { String c = ThePatcher.patch(this.getText1()); if (c != null) cir.setReturnValue(c); } + @Inject(method = "getText", at = @At("HEAD"), cancellable = true) private void proxy_getText(CallbackInfoReturnable cir) { String c = ThePatcher.patch(this.getText1()); diff --git a/src/main/resources/mixins.vaultpatcher.json b/src/main/resources/mixins.vaultpatcher.json index 472df9f..12774e8 100644 --- a/src/main/resources/mixins.vaultpatcher.json +++ b/src/main/resources/mixins.vaultpatcher.json @@ -6,14 +6,14 @@ "package": "me.fengming.vaultpatcher.mixin", "refmap": "mixins.vaultpatcher.refmap.json", "mixins": [ - "TextComponentMixin", "BaseComponentMixin", - "FontMixin" + "FontMixin", + "TextComponentMixin" ], "client": [ "BaseComponentMixin", - "TextComponentMixin", - "FontMixin" + "FontMixin", + "TextComponentMixin" ], "server": [ "BaseComponentMixin",