@@ -1,5 +1,7 @@
|
||||
package com.aranroig;
|
||||
|
||||
import com.aranroig.editor.EditorFile;
|
||||
import com.aranroig.payloads.ServerboundRunCodePayload;
|
||||
import com.aranroig.payloads.ServerboundSaveCodePayload;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
|
||||
@@ -7,9 +9,13 @@ import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.entity.vehicle.minecart.Minecart;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Codecraft implements ModInitializer {
|
||||
public static final String MOD_ID = "codecraft";
|
||||
|
||||
@@ -18,6 +24,8 @@ 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.
|
||||
@@ -26,10 +34,49 @@ public class Codecraft implements ModInitializer {
|
||||
LOGGER.info("Hello Fabric world!");
|
||||
|
||||
PayloadTypeRegistry.serverboundPlay().register(ServerboundSaveCodePayload.TYPE, ServerboundSaveCodePayload.CODEC);
|
||||
PayloadTypeRegistry.serverboundPlay().register(ServerboundRunCodePayload.TYPE, ServerboundRunCodePayload.CODEC);
|
||||
|
||||
ServerPlayNetworking.registerGlobalReceiver(ServerboundSaveCodePayload.TYPE, (payload, context) -> {
|
||||
System.out.println(payload.data().getName());
|
||||
System.out.println(payload.data().getText());
|
||||
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);
|
||||
System.out.println("Successfully saved files");
|
||||
});
|
||||
|
||||
ServerPlayNetworking.registerGlobalReceiver(ServerboundRunCodePayload.TYPE, (payload, context) -> {
|
||||
// HERE
|
||||
MinecraftServer server = context.player().level().getServer();
|
||||
if(server == null) return;
|
||||
|
||||
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());
|
||||
|
||||
// TODO: Run the entrypoint, might change later
|
||||
for(EditorFile file : files) {
|
||||
if(file.getName().equals(entrypoint)) {
|
||||
System.out.println(file.getText());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
65
src/main/java/com/aranroig/SavedProjectData.java
Normal file
65
src/main/java/com/aranroig/SavedProjectData.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package com.aranroig;
|
||||
|
||||
import com.aranroig.editor.EditorFile;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.saveddata.SavedData;
|
||||
import net.minecraft.world.level.saveddata.SavedDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SavedProjectData extends SavedData {
|
||||
private List<EditorFile> editorFiles;
|
||||
|
||||
public SavedProjectData() {
|
||||
editorFiles = new ArrayList<EditorFile>();
|
||||
}
|
||||
|
||||
public SavedProjectData(List<EditorFile> editorFiles) {
|
||||
this.editorFiles = editorFiles;
|
||||
}
|
||||
|
||||
public List<EditorFile> getEditorFiles() {
|
||||
return editorFiles;
|
||||
}
|
||||
|
||||
public void setEditorFiles(List<EditorFile> editorFiles) {
|
||||
this.editorFiles = editorFiles;
|
||||
setDirty();
|
||||
}
|
||||
|
||||
|
||||
private static final Codec<SavedProjectData> CODEC = RecordCodecBuilder.create(instance ->
|
||||
instance.group(
|
||||
EditorFile.CODEC.listOf()
|
||||
.fieldOf("editor_files")
|
||||
.forGetter(SavedProjectData::getEditorFiles)
|
||||
).apply(instance, SavedProjectData::new)
|
||||
);
|
||||
|
||||
private static final SavedDataType<SavedProjectData> TYPE = new SavedDataType<SavedProjectData>(
|
||||
Identifier.fromNamespaceAndPath(Codecraft.MOD_ID, "saved_project_data"), // The unique name for this saved data.
|
||||
SavedProjectData::new, // If there's no 'SavedBlockData', yet create one and refresh fields.
|
||||
CODEC, // The codec used for serialization/deserialization.
|
||||
null // A data fixer, which is not needed here.
|
||||
);
|
||||
|
||||
public static SavedProjectData getSavedBlockData(MinecraftServer server) {
|
||||
// This could be either the overworld or another dimension.
|
||||
ServerLevel level = server.getLevel(ServerLevel.OVERWORLD);
|
||||
|
||||
if (level == null) {
|
||||
return new SavedProjectData(); // Return a new instance if the level is null.
|
||||
}
|
||||
|
||||
// The first time the following 'computeIfAbsent' function is called, it creates a new 'SavedBlockData'
|
||||
// instance and stores it inside the 'DimensionDataStorage'.
|
||||
// Subsequent calls to 'computeIfAbsent' returns the saved 'SavedBlockData' NBT on disk to the Codec in our type,
|
||||
// using the Codec to decode the NBT into our saved data.
|
||||
return level.getDataStorage().computeIfAbsent(TYPE);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.aranroig.editor;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf;
|
||||
import net.minecraft.network.codec.ByteBufCodecs;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
|
||||
public class EditorFile {
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, EditorFile> CODEC =
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, EditorFile> STREAM_CODEC =
|
||||
StreamCodec.composite(
|
||||
ByteBufCodecs.STRING_UTF8,
|
||||
EditorFile::getName,
|
||||
@@ -16,6 +18,13 @@ public class EditorFile {
|
||||
EditorFile::new
|
||||
);
|
||||
|
||||
public static final Codec<EditorFile> CODEC = RecordCodecBuilder.create(instance ->
|
||||
instance.group(
|
||||
Codec.STRING.fieldOf("name").forGetter(EditorFile::getName),
|
||||
Codec.STRING.fieldOf("text").forGetter(EditorFile::getText)
|
||||
).apply(instance, EditorFile::new)
|
||||
);
|
||||
|
||||
// Class with file info
|
||||
private String name;
|
||||
private String text;
|
||||
|
||||
9
src/main/java/com/aranroig/editor/Project.java
Normal file
9
src/main/java/com/aranroig/editor/Project.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.aranroig.editor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record Project(List<EditorFile> editorFiles) {
|
||||
/*
|
||||
TODO: Acabar esto. En principio hay que sustituir las listas de los archivos de proyecto por esta clase
|
||||
*/
|
||||
}
|
||||
30
src/main/java/com/aranroig/editor/RunInfo.java
Normal file
30
src/main/java/com/aranroig/editor/RunInfo.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.aranroig.editor;
|
||||
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf;
|
||||
import net.minecraft.network.codec.ByteBufCodecs;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
|
||||
public class RunInfo {
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, RunInfo> CODEC =
|
||||
StreamCodec.composite(
|
||||
ByteBufCodecs.STRING_UTF8,
|
||||
RunInfo::getEntrypoint,
|
||||
|
||||
RunInfo::new
|
||||
);
|
||||
|
||||
// Class with file info
|
||||
private String entrypoint;
|
||||
|
||||
public RunInfo(String entrypoint) {
|
||||
this.entrypoint = entrypoint;
|
||||
}
|
||||
|
||||
public String getEntrypoint() {
|
||||
return entrypoint;
|
||||
}
|
||||
|
||||
public void setEntrypoint(String entrypoint) {
|
||||
this.entrypoint = entrypoint;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public record ClientboundSaveCodePayload(EditorFile data) implements CustomPacke
|
||||
public static final CustomPacketPayload.Type<ClientboundSaveCodePayload> TYPE = new CustomPacketPayload.Type<>(SAVE_CODE_PAYLOAD_ID);
|
||||
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSaveCodePayload> CODEC =
|
||||
StreamCodec.composite(EditorFile.CODEC, ClientboundSaveCodePayload::data, ClientboundSaveCodePayload::new);
|
||||
StreamCodec.composite(EditorFile.STREAM_CODEC, ClientboundSaveCodePayload::data, ClientboundSaveCodePayload::new);
|
||||
|
||||
@Override
|
||||
public Type<? extends CustomPacketPayload> type() {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.aranroig.payloads;
|
||||
|
||||
import com.aranroig.Codecraft;
|
||||
import com.aranroig.editor.EditorFile;
|
||||
import com.aranroig.editor.RunInfo;
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
public record ServerboundRunCodePayload(RunInfo data) implements CustomPacketPayload {
|
||||
public static final Identifier RUN_CODE_PAYLOAD_ID = Identifier.fromNamespaceAndPath(Codecraft.MOD_ID, "run_code");
|
||||
|
||||
public static final CustomPacketPayload.Type<ServerboundRunCodePayload> TYPE = new CustomPacketPayload.Type<>(RUN_CODE_PAYLOAD_ID);
|
||||
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, ServerboundRunCodePayload> CODEC =
|
||||
StreamCodec.composite(RunInfo.CODEC, ServerboundRunCodePayload::data, ServerboundRunCodePayload::new);
|
||||
|
||||
|
||||
@Override
|
||||
public Type<? extends CustomPacketPayload> type() {
|
||||
return TYPE;
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public record ServerboundSaveCodePayload(EditorFile data) implements CustomPacke
|
||||
public static final CustomPacketPayload.Type<ServerboundSaveCodePayload> TYPE = new CustomPacketPayload.Type<>(SAVE_CODE_PAYLOAD_ID);
|
||||
|
||||
public static final StreamCodec<RegistryFriendlyByteBuf, ServerboundSaveCodePayload> CODEC =
|
||||
StreamCodec.composite(EditorFile.CODEC, ServerboundSaveCodePayload::data, ServerboundSaveCodePayload::new);
|
||||
StreamCodec.composite(EditorFile.STREAM_CODEC, ServerboundSaveCodePayload::data, ServerboundSaveCodePayload::new);
|
||||
|
||||
@Override
|
||||
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
|
||||
|
||||
Reference in New Issue
Block a user