This commit is contained in:
@@ -26,6 +26,7 @@ import com.aranroig.payloads.ServerboundSaveCodePayload;
|
||||
import com.aranroig.payloads.ServerboundShellInputPayload;
|
||||
import com.aranroig.payloads.ServerboundShellResizePayload;
|
||||
import com.aranroig.payloads.ServerboundSpawnShellPayload;
|
||||
import com.aranroig.project.ProjectStore;
|
||||
import com.aranroig.shell.ShellManager;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
@@ -34,7 +35,6 @@ import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -83,6 +83,12 @@ public class Codecraft implements ModInitializer {
|
||||
// currently running script happens per event.
|
||||
ScriptRuntimeManager.install();
|
||||
|
||||
// The project now lives as plain .lua files under config/codecraft:
|
||||
// move a world's scripts out of its saved data the first time a
|
||||
// server starts, and drain the queued keystroke writes to disk before
|
||||
// it stops.
|
||||
ProjectStore.install();
|
||||
|
||||
// Drop players from their live editing sessions when they leave, and
|
||||
// throw the cached documents away when the world stops.
|
||||
DocumentManager.install();
|
||||
@@ -92,17 +98,15 @@ public class Codecraft implements ModInitializer {
|
||||
ShellManager.install();
|
||||
|
||||
ServerPlayNetworking.registerGlobalReceiver(ServerboundRequestProjectPayload.TYPE, (payload, context) -> {
|
||||
MinecraftServer server = context.player().level().getServer();
|
||||
if(server == null) return;
|
||||
|
||||
SavedProjectData savedData = SavedProjectData.getSavedBlockData(server);
|
||||
List<EditorFile> files = savedData.getEditorFiles();
|
||||
// Read fresh from disk every time the editor opens: scripts edited
|
||||
// with an external editor in the meantime are in here too.
|
||||
List<EditorFile> files = ProjectStore.loadAll();
|
||||
|
||||
// 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(ProjectPath.ENTRYPOINT, ""));
|
||||
savedData.setEditorFiles(files);
|
||||
ProjectStore.write(ProjectPath.ENTRYPOINT, "");
|
||||
files = List.of(new EditorFile(ProjectPath.ENTRYPOINT, ""));
|
||||
}
|
||||
|
||||
ServerPlayNetworking.send(context.player(), new ClientboundProjectFilesPayload(List.copyOf(files)));
|
||||
@@ -160,24 +164,18 @@ public class Codecraft implements ModInitializer {
|
||||
ShellManager.close(context.player()));
|
||||
|
||||
ServerPlayNetworking.registerGlobalReceiver(ServerboundRunCodePayload.TYPE, (payload, context) -> {
|
||||
MinecraftServer server = context.player().level().getServer();
|
||||
if(server == null) return;
|
||||
|
||||
ServerPlayer player = context.player();
|
||||
|
||||
SavedProjectData savedData = SavedProjectData.getSavedBlockData(server);
|
||||
List<EditorFile> files = savedData.getEditorFiles();
|
||||
|
||||
String entrypoint = payload.data().getEntrypoint();
|
||||
LOGGER.info("Running entrypoint {}", entrypoint);
|
||||
|
||||
EditorFile target = findByName(files, entrypoint);
|
||||
if(target == null) {
|
||||
String code = ProjectStore.read(entrypoint);
|
||||
if(code == null) {
|
||||
// Worlds seeded while names still carried the extension.
|
||||
target = findByName(files, entrypoint + ProjectPath.LEGACY_EXTENSION);
|
||||
code = ProjectStore.read(entrypoint + ProjectPath.LEGACY_EXTENSION);
|
||||
}
|
||||
|
||||
if(target == null) {
|
||||
if(code == null) {
|
||||
player.sendSystemMessage(Component.literal("§ccodecraft: entrypoint not found: " + entrypoint));
|
||||
return;
|
||||
}
|
||||
@@ -187,7 +185,7 @@ public class Codecraft implements ModInitializer {
|
||||
// events on a brand-new Lua state.
|
||||
ScriptRuntime runtime = ScriptRuntimeManager.startRun(player);
|
||||
try {
|
||||
runtime.run(target.getText());
|
||||
runtime.run(code);
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("Error running " + entrypoint, e);
|
||||
player.sendSystemMessage(Component.literal("§ccodecraft error: " + e.getMessage()));
|
||||
@@ -195,14 +193,6 @@ 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user