Thing
This commit is contained in:
@@ -1,18 +1,26 @@
|
||||
package com.aranroig;
|
||||
|
||||
import com.aranroig.editor.EditorFile;
|
||||
import com.aranroig.payloads.ClientboundProjectFilesPayload;
|
||||
import com.aranroig.payloads.ClientboundSaveCodePayload;
|
||||
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 net.fabricmc.api.ModInitializer;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
||||
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.world.entity.vehicle.minecart.Minecart;
|
||||
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;
|
||||
|
||||
@@ -24,8 +32,6 @@ public class Codecraft implements ModInitializer {
|
||||
// That way, it's clear which mod wrote info, warnings, and errors.
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||
|
||||
private List<EditorFile> editorFiles;
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
||||
@@ -35,6 +41,25 @@ 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.clientboundPlay().register(ClientboundSaveCodePayload.TYPE, ClientboundSaveCodePayload.CODEC);
|
||||
PayloadTypeRegistry.clientboundPlay().register(ClientboundProjectFilesPayload.TYPE, ClientboundProjectFilesPayload.CODEC);
|
||||
|
||||
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();
|
||||
|
||||
// Seed a default entrypoint so the client always has at least one file.
|
||||
if(files.isEmpty()) {
|
||||
files.add(new EditorFile("main.lua", ""));
|
||||
savedData.setEditorFiles(files);
|
||||
}
|
||||
|
||||
ServerPlayNetworking.send(context.player(), new ClientboundProjectFilesPayload(List.copyOf(files)));
|
||||
});
|
||||
|
||||
ServerPlayNetworking.registerGlobalReceiver(ServerboundSaveCodePayload.TYPE, (payload, context) -> {
|
||||
MinecraftServer server = context.player().level().getServer();
|
||||
@@ -57,26 +82,51 @@ public class Codecraft implements ModInitializer {
|
||||
}
|
||||
|
||||
savedData.setEditorFiles(files);
|
||||
System.out.println("Successfully saved files");
|
||||
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())));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ServerPlayNetworking.registerGlobalReceiver(ServerboundRunCodePayload.TYPE, (payload, context) -> {
|
||||
// HERE
|
||||
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();
|
||||
System.out.println("Want to run the code " + payload.data().getEntrypoint());
|
||||
LOGGER.info("Running entrypoint {}", entrypoint);
|
||||
|
||||
// TODO: Run the entrypoint, might change later
|
||||
EditorFile target = null;
|
||||
for(EditorFile file : files) {
|
||||
if(file.getName().equals(entrypoint)) {
|
||||
System.out.println(file.getText());
|
||||
target = file;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(target == null) {
|
||||
player.sendSystemMessage(Component.literal("§ccodecraft: entrypoint not found: " + entrypoint));
|
||||
return;
|
||||
}
|
||||
|
||||
Lua lua = new LuaJ();
|
||||
lua.push(ChatLogger.print(player));
|
||||
lua.setGlobal("print");
|
||||
WorldApi.register(lua, player);
|
||||
try {
|
||||
lua.run(target.getText());
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("Error running " + entrypoint, e);
|
||||
player.sendSystemMessage(Component.literal("§ccodecraft error: " + e.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user