1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-24 17:10:31 +00:00

Move file transfer API to the code library

This is useful for emulators, which might want to emulate the event.
This commit is contained in:
Jonathan Coates 2023-09-29 21:09:23 +01:00
parent 663eecff0c
commit e7a1065bfc
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 21 additions and 18 deletions

View File

@ -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<T extends AbstractContainerMenu & ComputerMenu> 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);
}

View File

@ -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;

View File

@ -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<TransferredFile> files;
public TransferredFiles(ServerPlayer player, AbstractContainerMenu container, List<TransferredFile> files) {
this.player = player;
this.container = container;
public TransferredFiles(List<TransferredFile> 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();
}
}