Skip to content

Commit

Permalink
Added A Function For Demo Script
Browse files Browse the repository at this point in the history
Will fix item next
  • Loading branch information
Iteranya committed Nov 2, 2024
1 parent 9e6a448 commit 563d539
Show file tree
Hide file tree
Showing 17 changed files with 1,163 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.arsparadox.mobtalkerredux.vn.view.DialogueScreen;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;

public class HelloWorldItem extends Item {
Expand All @@ -28,16 +29,16 @@ public InteractionResult useOn(UseOnContext context) {
);
}
else {
// ScriptLoader scriptLoader = new ScriptLoader();
// DialogueScreenVM dialogue = scriptLoader.loadDialogue("debug.dialogue.lua");

Minecraft.getInstance().execute(() -> {
try {
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadScript("demo.json"))));
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadDemo())));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
);

WaifuManager waifuManager = new WaifuManager(context.getPlayer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraftforge.fml.loading.FMLPaths;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class ScriptLoader{
Expand All @@ -29,6 +33,22 @@ public static ArrayNode loadJson(String filePath) {
}
}

public static ArrayNode loadJson(InputStream content) {
ObjectMapper objectMapper = new ObjectMapper();

try {
// Parse the JSON file and return it as an ArrayNode
JsonNode jsonNode = objectMapper.readTree(content);
if (jsonNode.isArray()) {
return (ArrayNode) jsonNode;
} else {
throw new RuntimeException("JSON is not an array at Demo");
}
} catch (IOException e) {
throw new RuntimeException("Failed to load file at Demo", e);
}
}

// public static DialogueList loadDialogue(String filePath) throws FileNotFoundException {
// return DialogueParser.parseDialogue(loadJson(filePath).toString());
// }
Expand All @@ -39,6 +59,20 @@ public static List<Map<String, Object>> loadScript(String filePath) throws FileN
return convertJsonArrayToList(jsonArray);
}

public static List<Map<String, Object>> loadDemo() throws IOException {
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
ResourceLocation resourceLocation = new ResourceLocation("mobtalkerredux", "demo.json");

// Try to retrieve the resource as an input stream
try (InputStream inputStream = resourceManager.getResource(resourceLocation).get().open()) {
ArrayNode jsonArray = loadJson(inputStream);
return convertJsonArrayToList(jsonArray);
} catch (FileNotFoundException e) {
// Handle the case where the file truly doesn't exist or isn't accessible
throw new FileNotFoundException("Resource file demo.json could not be found.");
}
}

private static List<Map<String, Object>> convertJsonArrayToList(ArrayNode arrayNode) {
List<Map<String, Object>> result = new ArrayList<>();

Expand Down
Loading

0 comments on commit 563d539

Please sign in to comment.