diff --git a/src/main/java/io/github/homchom/recode/game/GameTime.kt b/src/main/java/io/github/homchom/recode/game/GameTime.kt index f33f540c..779cd656 100644 --- a/src/main/java/io/github/homchom/recode/game/GameTime.kt +++ b/src/main/java/io/github/homchom/recode/game/GameTime.kt @@ -3,17 +3,11 @@ package io.github.homchom.recode.game import io.github.homchom.recode.Power -import io.github.homchom.recode.RecodeDispatcher import io.github.homchom.recode.event.listen import io.github.homchom.recode.event.listenEach -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.take import kotlinx.coroutines.flow.takeWhile -import kotlinx.coroutines.future.asCompletableFuture -import kotlinx.coroutines.launch -import java.util.concurrent.CompletionStage /** * The current client tick. @@ -29,18 +23,6 @@ val currentTick get() = TickRecorder.currentTick */ suspend fun waitTicks(ticks: Int) = AfterClientTickEvent.notifications.take(ticks).collect() -/** - * @return A [CompletionStage] of a future that completes after the given number of [ticks]. - * - * @see waitTicks - */ -@Deprecated("Only for use in Java code", - ReplaceWith("launch { waitTicks(ticks) }", "kotlinx.coroutines.launch") -) -@DelicateCoroutinesApi -fun waitTicksAsync(ticks: Int): CompletionStage = - GlobalScope.launch(RecodeDispatcher) { waitTicks(ticks) }.asCompletableFuture() - private object TickRecorder { var currentTick = 0L diff --git a/src/main/java/io/github/homchom/recode/mod/commands/impl/item/ImportFileCommand.java b/src/main/java/io/github/homchom/recode/mod/commands/impl/item/ImportFileCommand.java index 9018c1ad..b76c1de1 100644 --- a/src/main/java/io/github/homchom/recode/mod/commands/impl/item/ImportFileCommand.java +++ b/src/main/java/io/github/homchom/recode/mod/commands/impl/item/ImportFileCommand.java @@ -4,8 +4,8 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.mojang.brigadier.CommandDispatcher; +import io.github.homchom.recode.LegacyRecode; import io.github.homchom.recode.ModConstants; -import io.github.homchom.recode.game.GameTime; import io.github.homchom.recode.io.NativeIO; import io.github.homchom.recode.mod.commands.Command; import io.github.homchom.recode.mod.commands.arguments.ArgBuilder; @@ -24,6 +24,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -37,94 +38,98 @@ public void register(Minecraft mc, CommandDispatcher cd.register(ArgBuilder.literal("importfile") .executes(ctx -> { if (!isCreative(mc)) return -1; - ChatUtil.sendMessage("Opening File Picker", ChatType.INFO_BLUE); - Minecraft.getInstance().tell(this::openFilePicker); - + Minecraft.getInstance().tell(this::openFilePickerScreen); return 1; }) ); } - private void openFilePicker() { + private void openFilePickerScreen() { var screen = new DummyScreen(Component.text(TITLE), true); Minecraft.getInstance().setScreen(screen); - GameTime.waitTicksAsync(1).thenRun(() -> { + LegacyRecode.executor.execute(() -> { var paths = NativeIO.pickMultipleFiles(TITLE); - Minecraft.getInstance().setScreen(null); - if (paths == null || paths.isEmpty()) { - ChatUtil.sendMessage("You didnt choose a file!", ChatType.FAIL); - return; + Minecraft.getInstance().execute(() -> { + Minecraft.getInstance().setScreen(null); + importFiles(paths); + }); + }); + } + + private void importFiles(List paths) { + if (paths == null || paths.isEmpty()) { + ChatUtil.sendMessage("You didnt choose a file!", ChatType.FAIL); + return; + } + + int valid = 0; + files: for (var path : paths) { + if (paths.size() != 1) { + ChatUtil.sendMessage("Loading file: " + path.getFileName(), ChatType.INFO_BLUE); + } + Scanner sc; + try { + sc = new Scanner(path, StandardCharsets.UTF_8); + } catch (IOException e) { + ChatUtil.sendMessage("Failed to load file: " + path.getFileName(), ChatType.FAIL); + continue; } - int valid = 0; - files: for (var path : paths) { - if (paths.size() != 1) { - ChatUtil.sendMessage("Loading file: " + path.getFileName(), ChatType.INFO_BLUE); + List lines = new ArrayList<>(); + + while (sc.hasNextLine()) { + String line = sc.nextLine(); + if (line.length() > 10000) { + ChatUtil.sendMessage("Line " + (lines.size() + 1) + " is too long! (" + line.length() + " > 10000)", ChatType.FAIL); + continue files; } - Scanner sc; - try { - sc = new Scanner(path, StandardCharsets.UTF_8); - } catch (IOException e) { - ChatUtil.sendMessage("Failed to load file: " + path.getFileName(), ChatType.FAIL); - continue; + lines.add(line); + if (lines.size() > 10000) { + ChatUtil.sendMessage("File contains contains too many lines! (Max: 10,000)", ChatType.FAIL); + continue files; } + } - List lines = new ArrayList<>(); - - while (sc.hasNextLine()) { - String line = sc.nextLine(); - if (line.length() > 10000) { - ChatUtil.sendMessage("Line " + (lines.size() + 1) + " is too long! (" + line.length() + " > 10000)", ChatType.FAIL); - continue files; - } - lines.add(line); - if (lines.size() > 10000) { - ChatUtil.sendMessage("File contains contains too many lines! (Max: 10,000)", ChatType.FAIL); - continue files; - } - } + List blocks = new ArrayList<>(); + List current = new ArrayList<>(); - List blocks = new ArrayList<>(); - List current = new ArrayList<>(); - - boolean first = true; - for (String line : lines) { - current.add(line); - if (current.size() >= 26) { - blocks.add(block(current, first)); - first = false; - current = new ArrayList<>(); - } - } - if (!current.isEmpty()) blocks.add(block(current, first)); - - String template; - try { - template = template(blocks); - } catch (IOException e) { - ChatUtil.sendMessage("Failed to generate template for file: " + path.getFileName(), ChatType.FAIL); - continue; + boolean first = true; + for (String line : lines) { + current.add(line); + if (current.size() >= 26) { + blocks.add(block(current, first)); + first = false; + current = new ArrayList<>(); } - if (template.getBytes().length > 65536) { // i have no idea what the actual limit is it just seems to be close to this TODO: nice - ChatUtil.sendMessage("Your file is too large!", ChatType.FAIL); - } else { - ItemStack item = new ItemStack(Items.ENDER_CHEST); - TemplateUtil.applyRawTemplateNBT(item, path.getFileName().toString(), ModConstants.MOD_NAME, template); - ItemUtil.giveCreativeItem(item, paths.size() == 1); - if (paths.size() != 1) try { - Thread.sleep(500); - } catch (InterruptedException e) { - // temporary TODO: what - throw new RuntimeException(e); - } - valid++; + } + if (!current.isEmpty()) blocks.add(block(current, first)); + + String template; + try { + template = template(blocks); + } catch (IOException e) { + ChatUtil.sendMessage("Failed to generate template for file: " + path.getFileName(), ChatType.FAIL); + continue; + } + if (template.getBytes().length > 65536) { // i have no idea what the actual limit is it just seems to be close to this TODO: nice + ChatUtil.sendMessage("Your file is too large!", ChatType.FAIL); + } else { + ItemStack item = new ItemStack(Items.ENDER_CHEST); + TemplateUtil.applyRawTemplateNBT(item, path.getFileName().toString(), ModConstants.MOD_NAME, template); + ItemUtil.giveCreativeItem(item, paths.size() == 1); + if (paths.size() != 1) try { + Thread.sleep(500); + } catch (InterruptedException e) { + // temporary TODO: what + throw new RuntimeException(e); } + valid++; } - if (paths.size() != 1 && valid > 0) - ChatUtil.sendMessage("Loaded " + valid + " files!", ChatType.SUCCESS); - }); + } + if (paths.size() != 1 && valid > 0) + ChatUtil.sendMessage("Loaded " + valid + " files!", ChatType.SUCCESS); } private String template(List iblocks) throws IOException {