From 7ae183610978c5330eaa18377211829c88b8b089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E8=91=89=20Scarlet?= <93977077+mukjepscarlet@users.noreply.github.com> Date: Mon, 25 May 2026 11:07:47 +0800 Subject: [PATCH 1/2] fix unclosed IO resources, unify notebot folder creation --- .../arguments/NotebotSongArgumentType.java | 6 ++--- .../commands/commands/NotebotCommand.java | 2 +- .../gui/screens/NotebotSongsScreen.java | 5 ++-- .../gui/tabs/builtin/ProfilesTab.java | 5 +++- .../systems/modules/misc/Notebot.java | 24 +++++++++++-------- .../systems/modules/world/StashFinder.java | 5 +--- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/java/meteordevelopment/meteorclient/commands/arguments/NotebotSongArgumentType.java b/src/main/java/meteordevelopment/meteorclient/commands/arguments/NotebotSongArgumentType.java index 9d0852c315..05632d9e04 100644 --- a/src/main/java/meteordevelopment/meteorclient/commands/arguments/NotebotSongArgumentType.java +++ b/src/main/java/meteordevelopment/meteorclient/commands/arguments/NotebotSongArgumentType.java @@ -11,7 +11,7 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; -import meteordevelopment.meteorclient.MeteorClient; +import meteordevelopment.meteorclient.systems.modules.misc.Notebot; import meteordevelopment.meteorclient.utils.notebot.decoder.SongDecoders; import net.minecraft.commands.SharedSuggestionProvider; @@ -34,12 +34,12 @@ private NotebotSongArgumentType() { public Path parse(StringReader reader) throws CommandSyntaxException { final String text = reader.getRemaining(); reader.setCursor(reader.getTotalLength()); - return MeteorClient.FOLDER.toPath().resolve("notebot/" + text); + return Notebot.PATH.toPath().resolve(text); } @Override public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { - try (var suggestions = Files.list(MeteorClient.FOLDER.toPath().resolve("notebot"))) { + try (var suggestions = Files.list(Notebot.PATH.toPath())) { return SharedSuggestionProvider.suggest(suggestions .filter(SongDecoders::hasDecoder) .map(path -> path.getFileName().toString()), diff --git a/src/main/java/meteordevelopment/meteorclient/commands/commands/NotebotCommand.java b/src/main/java/meteordevelopment/meteorclient/commands/commands/NotebotCommand.java index 29db933dc0..0a1ffa2d0c 100644 --- a/src/main/java/meteordevelopment/meteorclient/commands/commands/NotebotCommand.java +++ b/src/main/java/meteordevelopment/meteorclient/commands/commands/NotebotCommand.java @@ -126,7 +126,7 @@ public void build(LiteralArgumentBuilder builder) { if (name == null || name.isEmpty()) { throw INVALID_PATH.create(name); } - Path notebotFolder = MeteorClient.FOLDER.toPath().resolve("notebot"); + Path notebotFolder = Notebot.PATH.toPath(); Path path = notebotFolder.resolve(String.format("%s.txt", name)).normalize(); if (!path.startsWith(notebotFolder)) { throw INVALID_PATH.create(path); diff --git a/src/main/java/meteordevelopment/meteorclient/gui/screens/NotebotSongsScreen.java b/src/main/java/meteordevelopment/meteorclient/gui/screens/NotebotSongsScreen.java index 1599e22dde..af15ffb5e0 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/screens/NotebotSongsScreen.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/screens/NotebotSongsScreen.java @@ -5,7 +5,6 @@ package meteordevelopment.meteorclient.gui.screens; -import meteordevelopment.meteorclient.MeteorClient; import meteordevelopment.meteorclient.gui.GuiTheme; import meteordevelopment.meteorclient.gui.WindowScreen; import meteordevelopment.meteorclient.gui.widgets.containers.WTable; @@ -57,8 +56,8 @@ public void initWidgets() { private void initSongsTable() { AtomicBoolean noSongsFound = new AtomicBoolean(true); - try { - Files.list(MeteorClient.FOLDER.toPath().resolve("notebot")).forEach(path -> { + try (var stream = Files.list(Notebot.PATH.toPath())) { + stream.forEach(path -> { if (SongDecoders.hasDecoder(path)) { String name = path.getFileName().toString(); diff --git a/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java b/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java index e4651b5637..5216495432 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java @@ -38,6 +38,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -166,7 +167,9 @@ private Profile importProfile() throws IOException { } File f = new File(p.getFile(), filename); - NbtIo.writeUnnamedTagWithFallback(entry.getValue(), new DataOutputStream(new FileOutputStream(f))); + try (DataOutputStream output = new DataOutputStream(new FileOutputStream(f))) { + NbtIo.writeUnnamedTagWithFallback(entry.getValue(), output); + } } Profiles.get().getAll().add(p); diff --git a/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/Notebot.java b/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/Notebot.java index 94bb755d12..99f1856f23 100644 --- a/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/Notebot.java +++ b/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/Notebot.java @@ -37,6 +37,7 @@ import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket; import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket; import net.minecraft.sounds.SoundEvents; +import net.minecraft.util.Util; import net.minecraft.world.InteractionHand; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.NoteBlock; @@ -60,6 +61,8 @@ public class Notebot extends Module { + public static final File PATH = new File(MeteorClient.FOLDER, "notebot"); + private final SettingGroup sgGeneral = settings.getDefaultGroup(); private final SettingGroup sgNoteMap = settings.createGroup("Note Map", false); private final SettingGroup sgRender = settings.createGroup("Render", true); @@ -650,7 +653,7 @@ public void onSongEnd() { } public void playRandomSong() { - File[] files = MeteorClient.FOLDER.toPath().resolve("notebot").toFile().listFiles(); + File[] files = PATH.listFiles(); if (files == null) return; File randomSong = files[ThreadLocalRandom.current().nextInt(files.length)]; @@ -718,7 +721,8 @@ public boolean loadFileToMap(File file, Runnable callback) { return false; } - info("Loading song \"%s\".", FilenameUtils.getBaseName(file.getName())); + String baseName = FilenameUtils.getBaseName(file.getName()); + info("Loading song \"%s\".", baseName); // Start loading song loadingSongFuture = CompletableFuture.supplyAsync(() -> { @@ -727,16 +731,16 @@ public boolean loadFileToMap(File file, Runnable callback) { } catch (Exception e) { throw new RuntimeException(e); } - }); + }, Util.nonCriticalIoPool()); loadingSongFuture.completeOnTimeout(null, 60, TimeUnit.SECONDS); stage = Stage.LoadingSong; long time1 = System.currentTimeMillis(); - loadingSongFuture.whenComplete((song, ex) -> { + loadingSongFuture.whenCompleteAsync((song, ex) -> { if (ex == null) { // Song is null only when it times out if (song == null) { - error("Loading song '" + FilenameUtils.getBaseName(file.getName()) + "' timed out."); + error("Loading song '" + baseName + "' timed out."); onSongEnd(); return; } @@ -745,18 +749,18 @@ public boolean loadFileToMap(File file, Runnable callback) { long time2 = System.currentTimeMillis(); long diff = time2 - time1; - info("Song '" + FilenameUtils.getBaseName(file.getName()) + "' has been loaded to the memory! Took " + diff + "ms"); + info("Song '" + baseName + "' has been loaded to the memory! Took " + diff + "ms"); callback.run(); } else { if (ex instanceof CancellationException) { - error("Loading song '" + FilenameUtils.getBaseName(file.getName()) + "' was cancelled."); + error("Loading song '" + baseName + "' was cancelled."); } else { - error("An error occurred while loading song '" + FilenameUtils.getBaseName(file.getName()) + "'. See the logs for more details"); - MeteorClient.LOG.error("An error occurred while loading song '{}'", FilenameUtils.getBaseName(file.getName()), ex); + error("An error occurred while loading song '" + baseName + "'. See the logs for more details"); + MeteorClient.LOG.error("An error occurred while loading song '{}'", baseName, ex); onSongEnd(); } } - }); + }, mc); return true; } diff --git a/src/main/java/meteordevelopment/meteorclient/systems/modules/world/StashFinder.java b/src/main/java/meteordevelopment/meteorclient/systems/modules/world/StashFinder.java index cb9f918ead..649f4ea626 100644 --- a/src/main/java/meteordevelopment/meteorclient/systems/modules/world/StashFinder.java +++ b/src/main/java/meteordevelopment/meteorclient/systems/modules/world/StashFinder.java @@ -346,8 +346,7 @@ private void load() { // Try to load csv file = getCsvFile(); if (!loaded && file.exists()) { - try { - BufferedReader reader = new BufferedReader(new FileReader(file)); + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { reader.readLine(); String line; @@ -364,8 +363,6 @@ private void load() { chunks.add(chunk); } - - reader.close(); } catch (Exception _) { if (chunks == null) chunks = new ArrayList<>(); } From ab6dea15f079fdc93f3da226431f831ebd79bad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E8=91=89=20Scarlet?= <93977077+MukjepScarlet@users.noreply.github.com> Date: Mon, 25 May 2026 11:26:00 +0800 Subject: [PATCH 2/2] Remove unused import --- .../meteorclient/gui/tabs/builtin/ProfilesTab.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java b/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java index 5216495432..1d2f85329f 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java @@ -38,7 +38,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List;