From ecf69aee333ca59c8c27c47fe1df50a6e9e71c52 Mon Sep 17 00:00:00 2001 From: nickid2018 Date: Sat, 22 Jun 2024 00:37:04 +0800 Subject: [PATCH] Add noSave feature --- .../nickid2018/genwiki/RemapSettings.java | 28 +++++++++++++++++++ .../genwiki/iso/ISOInjectionEntryPoints.java | 9 ++++++ .../nickid2018/genwiki/remap/SingleFile.java | 27 ++++++++++-------- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/github/nickid2018/genwiki/RemapSettings.java b/src/main/java/io/github/nickid2018/genwiki/RemapSettings.java index 2758ab9..a484748 100644 --- a/src/main/java/io/github/nickid2018/genwiki/RemapSettings.java +++ b/src/main/java/io/github/nickid2018/genwiki/RemapSettings.java @@ -327,6 +327,34 @@ public static void remapSettings(GenWikiMode mode, RemapProgram remapProgram) { } ) ); + remapProgram.addPostTransform( + "net.minecraft.server.MinecraftServer", + new MethodTransform( + "tickServer", + "(Ljava/util/function/BooleanSupplier;)V", + methodNode -> { + FieldInsnNode findAutoSaveIntervalPoint = null; + for (AbstractInsnNode node : methodNode.instructions) { + if (node instanceof FieldInsnNode fieldInsnNode) { + if (fieldInsnNode.getOpcode() == Opcodes.PUTFIELD && + fieldInsnNode.name.equals("ticksUntilAutosave")) { + findAutoSaveIntervalPoint = fieldInsnNode; + break; + } + } + } + if (findAutoSaveIntervalPoint != null) { + methodNode.instructions.insertBefore(findAutoSaveIntervalPoint, new MethodInsnNode( + Opcodes.INVOKESTATIC, + "io/github/nickid2018/genwiki/iso/ISOInjectionEntryPoints", + "handleAutoSaveInterval", + "(I)I", + false + )); + } + } + ) + ); remapProgram.addInjectEntries(new IncludeJarPackages("io.github.nickid2018.genwiki.iso")); remapProgram.addInjectEntries(new SingleFile( "transparency.fsh", diff --git a/src/main/java/io/github/nickid2018/genwiki/iso/ISOInjectionEntryPoints.java b/src/main/java/io/github/nickid2018/genwiki/iso/ISOInjectionEntryPoints.java index c535c19..f248557 100644 --- a/src/main/java/io/github/nickid2018/genwiki/iso/ISOInjectionEntryPoints.java +++ b/src/main/java/io/github/nickid2018/genwiki/iso/ISOInjectionEntryPoints.java @@ -30,6 +30,7 @@ public static void clampColorInjection(Vector3f vector3f) { private static boolean ortho = false; private static int invokeCount = 0; + private static boolean noSave = false; private static Matrix4f orthoMatrix = new Matrix4f().ortho(-2, 2, -2, 2, -0.1f, 1000); public static Matrix4f getProjectionMatrixInjection(Matrix4f source) { @@ -41,6 +42,12 @@ public static Matrix4f getProjectionMatrixInjection(Matrix4f source) { return source; } + public static int handleAutoSaveInterval(int source) { + if (noSave) + return 1; + return source; + } + public static void handleChat(String chat) { chat = chat.trim(); if (chat.toLowerCase().startsWith("run")) @@ -63,6 +70,8 @@ public static void doCommand(String chat) { switch (chat.toLowerCase()) { case "persp" -> ortho = false; case "ortho" -> ortho = true; + case "nosave" -> noSave = true; + case "save" -> noSave = false; default -> { String[] commands = chat.split(" ", 2); String commandHead = commands[0]; diff --git a/src/main/java/io/github/nickid2018/genwiki/remap/SingleFile.java b/src/main/java/io/github/nickid2018/genwiki/remap/SingleFile.java index ce7cd5b..45777f8 100644 --- a/src/main/java/io/github/nickid2018/genwiki/remap/SingleFile.java +++ b/src/main/java/io/github/nickid2018/genwiki/remap/SingleFile.java @@ -13,17 +13,20 @@ public record SingleFile(String file, String path) implements InjectEntries { @Override @SneakyThrows public Map getInjectEntries() { - JarFile thisJar = new JarFile( - IncludeJarPackages.class - .getProtectionDomain() - .getCodeSource() - .getLocation() - .getPath() - ); - Optional fileFind = thisJar.stream().filter(e -> e.getName().equals(file)).findFirst(); - if (fileFind.isEmpty()) - return Map.of(); - byte[] bytes = thisJar.getInputStream(fileFind.get()).readAllBytes(); - return Map.of(path, bytes); + try ( + JarFile thisJar = new JarFile( + IncludeJarPackages.class + .getProtectionDomain() + .getCodeSource() + .getLocation() + .getPath() + ) + ) { + Optional fileFind = thisJar.stream().filter(e -> e.getName().equals(file)).findFirst(); + if (fileFind.isEmpty()) + return Map.of(); + byte[] bytes = thisJar.getInputStream(fileFind.get()).readAllBytes(); + return Map.of(path, bytes); + } } }