Shell and folders
Some checks failed
build / build (push) Failing after 1m37s

This commit is contained in:
2026-10-07 19:13:25 +02:00
parent 7a115445ab
commit 5897870725
53 changed files with 6626 additions and 450 deletions

View File

@@ -1,13 +1,32 @@
package com.aranroig;
import com.aranroig.collab.DocumentManager;
import com.aranroig.editor.EditorFile;
import com.aranroig.editor.ProjectPath;
import com.aranroig.listeners.ScriptRuntime;
import com.aranroig.listeners.ScriptRuntimeManager;
import com.aranroig.payloads.ClientboundDocumentEditPayload;
import com.aranroig.payloads.ClientboundDocumentStatePayload;
import com.aranroig.payloads.ClientboundEditAckPayload;
import com.aranroig.payloads.ClientboundPresencePayload;
import com.aranroig.payloads.ClientboundProjectFilesPayload;
import com.aranroig.payloads.ClientboundRenameFilePayload;
import com.aranroig.payloads.ClientboundSaveCodePayload;
import com.aranroig.payloads.ClientboundShellClosedPayload;
import com.aranroig.payloads.ClientboundShellOutputPayload;
import com.aranroig.payloads.ServerboundCloseEditorPayload;
import com.aranroig.payloads.ServerboundCloseShellPayload;
import com.aranroig.payloads.ServerboundCursorPayload;
import com.aranroig.payloads.ServerboundEditPayload;
import com.aranroig.payloads.ServerboundOpenDocumentPayload;
import com.aranroig.payloads.ServerboundRenameFilePayload;
import com.aranroig.payloads.ServerboundRequestProjectPayload;
import com.aranroig.payloads.ServerboundRunCodePayload;
import com.aranroig.payloads.ServerboundSaveCodePayload;
import com.aranroig.wrapper.ChatLogger;
import com.aranroig.wrapper.WorldApi;
import com.aranroig.payloads.ServerboundShellInputPayload;
import com.aranroig.payloads.ServerboundShellResizePayload;
import com.aranroig.payloads.ServerboundSpawnShellPayload;
import com.aranroig.shell.ShellManager;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
@@ -19,8 +38,6 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import party.iroiro.luajava.Lua;
import party.iroiro.luajava.luaj.LuaJ;
import java.util.List;
@@ -42,8 +59,37 @@ public class Codecraft implements ModInitializer {
PayloadTypeRegistry.serverboundPlay().register(ServerboundSaveCodePayload.TYPE, ServerboundSaveCodePayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundRunCodePayload.TYPE, ServerboundRunCodePayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundRequestProjectPayload.TYPE, ServerboundRequestProjectPayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundOpenDocumentPayload.TYPE, ServerboundOpenDocumentPayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundCloseEditorPayload.TYPE, ServerboundCloseEditorPayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundEditPayload.TYPE, ServerboundEditPayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundCursorPayload.TYPE, ServerboundCursorPayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundRenameFilePayload.TYPE, ServerboundRenameFilePayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundSpawnShellPayload.TYPE, ServerboundSpawnShellPayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundShellInputPayload.TYPE, ServerboundShellInputPayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundShellResizePayload.TYPE, ServerboundShellResizePayload.CODEC);
PayloadTypeRegistry.serverboundPlay().register(ServerboundCloseShellPayload.TYPE, ServerboundCloseShellPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundSaveCodePayload.TYPE, ClientboundSaveCodePayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundProjectFilesPayload.TYPE, ClientboundProjectFilesPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundDocumentStatePayload.TYPE, ClientboundDocumentStatePayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundDocumentEditPayload.TYPE, ClientboundDocumentEditPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundEditAckPayload.TYPE, ClientboundEditAckPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundPresencePayload.TYPE, ClientboundPresencePayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundRenameFilePayload.TYPE, ClientboundRenameFilePayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundShellOutputPayload.TYPE, ClientboundShellOutputPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ClientboundShellClosedPayload.TYPE, ClientboundShellClosedPayload.CODEC);
// Bridge game activity (item use, block break, chat, join/leave, tick) to
// the Lua listeners scripts register. Registered once; dispatching to the
// currently running script happens per event.
ScriptRuntimeManager.install();
// Drop players from their live editing sessions when they leave, and
// throw the cached documents away when the world stops.
DocumentManager.install();
// Same for the shells the terminal pane spawns: nobody leaks when the
// player disconnects or the server stops.
ShellManager.install();
ServerPlayNetworking.registerGlobalReceiver(ServerboundRequestProjectPayload.TYPE, (payload, context) -> {
MinecraftServer server = context.player().level().getServer();
@@ -53,8 +99,9 @@ public class Codecraft implements ModInitializer {
List<EditorFile> files = savedData.getEditorFiles();
// Seed a default entrypoint so the client always has at least one file.
// Names carry no extension: the sidebar shows them as they are.
if(files.isEmpty()) {
files.add(new EditorFile("main.lua", ""));
files.add(new EditorFile(ProjectPath.ENTRYPOINT, ""));
savedData.setEditorFiles(files);
}
@@ -62,36 +109,56 @@ public class Codecraft implements ModInitializer {
});
ServerPlayNetworking.registerGlobalReceiver(ServerboundSaveCodePayload.TYPE, (payload, context) -> {
MinecraftServer server = context.player().level().getServer();
if(server == null) return;
SavedProjectData savedData = SavedProjectData.getSavedBlockData(server);
List<EditorFile> files = savedData.getEditorFiles();
EditorFile editorFile = payload.data();
boolean existing = false;
for(EditorFile file : files) {
if(file.getName().equals(editorFile.getName())) {
existing = true;
file.setText(editorFile.getText());
break;
}
}
if(!existing) {
files.add(editorFile);
}
savedData.setEditorFiles(files);
// The live document is what the other editors are looking at, so the
// save goes through it: the new text reaches them as one edit they
// can merge, instead of clobbering whatever they typed meanwhile —
// and it is from there that the rest of them hear the file exists.
DocumentManager.replaceDocument(context.player(), editorFile.getName(), editorFile.getText());
LOGGER.info("Successfully saved file {}", editorFile.getName());
// Push the change to every other client so their editors stay in sync.
for(ServerPlayer player : server.getPlayerList().getPlayers()) {
if(player != context.player()) {
ServerPlayNetworking.send(player, new ClientboundSaveCodePayload(new EditorFile(editorFile.getName(), editorFile.getText())));
}
}
});
// Renaming a file moves it in the saved project and its live document,
// and the other editors' lists follow.
ServerPlayNetworking.registerGlobalReceiver(ServerboundRenameFilePayload.TYPE, (payload, context) ->
DocumentManager.renameFile(context.player(), payload.oldName(), payload.newName()));
// ── Live editing ────────────────────────────────────────────────────
// A player opening the editor, or switching file, joins that document's
// session and gets its authoritative contents back.
ServerPlayNetworking.registerGlobalReceiver(ServerboundOpenDocumentPayload.TYPE, (payload, context) ->
DocumentManager.openDocument(context.player(), payload.file()));
// One keystroke's worth of changes, rebased and passed to the others.
ServerPlayNetworking.registerGlobalReceiver(ServerboundEditPayload.TYPE, (payload, context) ->
DocumentManager.applyEdit(context.player(), payload.file(), payload.baseVersion(), payload.edit()));
// Where the player's caret is, for the other editors to draw.
ServerPlayNetworking.registerGlobalReceiver(ServerboundCursorPayload.TYPE, (payload, context) ->
DocumentManager.updateCursor(context.player(), payload.file(), payload.cursor(), payload.anchor()));
// Closing the editor ends the session; disconnecting does it too.
ServerPlayNetworking.registerGlobalReceiver(ServerboundCloseEditorPayload.TYPE, (payload, context) ->
DocumentManager.closeEditor(context.player()));
// ── The terminal pane ───────────────────────────────────────────────
// The button spawns a bash on this machine, every line typed in the
// pane is a command for it, and closing the pane kills it.
ServerPlayNetworking.registerGlobalReceiver(ServerboundSpawnShellPayload.TYPE, (payload, context) ->
ShellManager.spawn(context.player(), payload.rows(), payload.cols()));
ServerPlayNetworking.registerGlobalReceiver(ServerboundShellInputPayload.TYPE, (payload, context) ->
ShellManager.submit(context.player(), payload.bytes()));
// A resize of the pane moves the pty's window on this side: the shell's
// input is not touched, and full-screen programs are signalled live.
ServerPlayNetworking.registerGlobalReceiver(ServerboundShellResizePayload.TYPE, (payload, context) ->
ShellManager.resize(context.player(), payload.rows(), payload.cols()));
ServerPlayNetworking.registerGlobalReceiver(ServerboundCloseShellPayload.TYPE, (payload, context) ->
ShellManager.close(context.player()));
ServerPlayNetworking.registerGlobalReceiver(ServerboundRunCodePayload.TYPE, (payload, context) -> {
MinecraftServer server = context.player().level().getServer();
if(server == null) return;
@@ -104,12 +171,10 @@ public class Codecraft implements ModInitializer {
String entrypoint = payload.data().getEntrypoint();
LOGGER.info("Running entrypoint {}", entrypoint);
EditorFile target = null;
for(EditorFile file : files) {
if(file.getName().equals(entrypoint)) {
target = file;
break;
}
EditorFile target = findByName(files, entrypoint);
if(target == null) {
// Worlds seeded while names still carried the extension.
target = findByName(files, entrypoint + ProjectPath.LEGACY_EXTENSION);
}
if(target == null) {
@@ -117,12 +182,12 @@ public class Codecraft implements ModInitializer {
return;
}
Lua lua = new LuaJ();
lua.push(ChatLogger.print(player));
lua.setGlobal("print");
WorldApi.register(lua, player);
// Fresh runtime per run: this drops the listeners the player
// registered last time (reset-on-rerun) and registers print/world/
// events on a brand-new Lua state.
ScriptRuntime runtime = ScriptRuntimeManager.startRun(player);
try {
lua.run(target.getText());
runtime.run(target.getText());
} catch (Exception e) {
LOGGER.warn("Error running " + entrypoint, e);
player.sendSystemMessage(Component.literal("§ccodecraft error: " + e.getMessage()));
@@ -130,6 +195,14 @@ public class Codecraft implements ModInitializer {
});
}
/** The project's file called {@code name}, or null when the project has none. */
private static EditorFile findByName(List<EditorFile> files, String name) {
for(EditorFile file : files) {
if(file.getName().equals(name)) return file;
}
return null;
}
public static Identifier id(String path) {
return Identifier.fromNamespaceAndPath(MOD_ID, path);
}