From e7a1065bfc13b83dc252a44b405415c08a172e0c Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Fri, 29 Sep 2023 21:09:23 +0100 Subject: [PATCH] Move file transfer API to the code library This is useful for emulators, which might want to emulate the event. --- .../computer/menu/ServerInputState.java | 16 +++++++++++--- .../core/apis/transfer}/TransferredFile.java | 2 +- .../core/apis/transfer}/TransferredFiles.java | 21 +++++++------------ 3 files changed, 21 insertions(+), 18 deletions(-) rename projects/{common/src/main/java/dan200/computercraft/shared/computer/upload => core/src/main/java/dan200/computercraft/core/apis/transfer}/TransferredFile.java (96%) rename projects/{common/src/main/java/dan200/computercraft/shared/computer/upload => core/src/main/java/dan200/computercraft/core/apis/transfer}/TransferredFiles.java (54%) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/menu/ServerInputState.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/menu/ServerInputState.java index 18234530a..a1cee206a 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/menu/ServerInputState.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/menu/ServerInputState.java @@ -4,7 +4,11 @@ package dan200.computercraft.shared.computer.menu; -import dan200.computercraft.shared.computer.upload.*; +import dan200.computercraft.core.apis.transfer.TransferredFile; +import dan200.computercraft.core.apis.transfer.TransferredFiles; +import dan200.computercraft.shared.computer.upload.FileSlice; +import dan200.computercraft.shared.computer.upload.FileUpload; +import dan200.computercraft.shared.computer.upload.UploadResult; import dan200.computercraft.shared.network.client.UploadResultMessage; import dan200.computercraft.shared.platform.PlatformHelper; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; @@ -150,8 +154,14 @@ public class ServerInputState im } } - computer.queueEvent("file_transfer", new Object[]{ - new TransferredFiles(player, owner, toUpload.stream().map(x -> new TransferredFile(x.getName(), x.getBytes())).collect(Collectors.toList())), + computer.queueEvent(TransferredFiles.EVENT, new Object[]{ + new TransferredFiles( + toUpload.stream().map(x -> new TransferredFile(x.getName(), x.getBytes())).collect(Collectors.toList()), + () -> { + if (player.isAlive() && player.containerMenu == owner) { + PlatformHelper.get().sendToPlayer(UploadResultMessage.consumed(owner), player); + } + }), }); return UploadResultMessage.queued(owner); } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/upload/TransferredFile.java b/projects/core/src/main/java/dan200/computercraft/core/apis/transfer/TransferredFile.java similarity index 96% rename from projects/common/src/main/java/dan200/computercraft/shared/computer/upload/TransferredFile.java rename to projects/core/src/main/java/dan200/computercraft/core/apis/transfer/TransferredFile.java index c059496f2..51565f32f 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/upload/TransferredFile.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/transfer/TransferredFile.java @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: MPL-2.0 -package dan200.computercraft.shared.computer.upload; +package dan200.computercraft.core.apis.transfer; import dan200.computercraft.api.lua.LuaFunction; import dan200.computercraft.core.apis.handles.BinaryReadableHandle; diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/upload/TransferredFiles.java b/projects/core/src/main/java/dan200/computercraft/core/apis/transfer/TransferredFiles.java similarity index 54% rename from projects/common/src/main/java/dan200/computercraft/shared/computer/upload/TransferredFiles.java rename to projects/core/src/main/java/dan200/computercraft/core/apis/transfer/TransferredFiles.java index c46841160..82dcc89c5 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/upload/TransferredFiles.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/transfer/TransferredFiles.java @@ -2,13 +2,9 @@ // // SPDX-License-Identifier: MPL-2.0 -package dan200.computercraft.shared.computer.upload; +package dan200.computercraft.core.apis.transfer; import dan200.computercraft.api.lua.LuaFunction; -import dan200.computercraft.shared.network.client.UploadResultMessage; -import dan200.computercraft.shared.platform.PlatformHelper; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.inventory.AbstractContainerMenu; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; @@ -19,16 +15,16 @@ import java.util.concurrent.atomic.AtomicBoolean; * @cc.module [kind=event] file_transfer.TransferredFiles */ public class TransferredFiles { - private final ServerPlayer player; - private final AbstractContainerMenu container; + public static final String EVENT = "file_transfer"; + private final AtomicBoolean consumed = new AtomicBoolean(false); + private final Runnable onConsumed; private final List files; - public TransferredFiles(ServerPlayer player, AbstractContainerMenu container, List files) { - this.player = player; - this.container = container; + public TransferredFiles(List files, Runnable onConsumed) { this.files = files; + this.onConsumed = onConsumed; } /** @@ -44,9 +40,6 @@ public class TransferredFiles { private void consumed() { if (consumed.getAndSet(true)) return; - - if (player.isAlive() && player.containerMenu == container) { - PlatformHelper.get().sendToPlayer(UploadResultMessage.consumed(container), player); - } + onConsumed.run(); } }