From 5b97c9992fdee9333dcff555acd290be5ac0e497 Mon Sep 17 00:00:00 2001 From: Ars Paradox <152275094+Iteranya@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:28:23 +0700 Subject: [PATCH] Made some form of var management system --- .../vn/controller/VisualNovelEngine.java | 24 ++++++++++++------- .../mobtalkerredux/vn/model/ScriptLoader.java | 23 ++++-------------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/arsparadox/mobtalkerredux/vn/controller/VisualNovelEngine.java b/src/main/java/org/arsparadox/mobtalkerredux/vn/controller/VisualNovelEngine.java index 639a18c..7b2bef7 100644 --- a/src/main/java/org/arsparadox/mobtalkerredux/vn/controller/VisualNovelEngine.java +++ b/src/main/java/org/arsparadox/mobtalkerredux/vn/controller/VisualNovelEngine.java @@ -20,9 +20,19 @@ public class VisualNovelEngine { public VisualNovelEngine(List> gameData,String scriptName) { this.gameData = gameData; this.currentState = 0; - this.variables = new HashMap<>(); this.state = new DialogueState(null,null,null); this.scriptName = scriptName; + initializeVariable(); + } + + private void initializeVariable() { + if(this.gameData.get(0).get("type")!="variable"){ + this.variables = new HashMap<>(); + this.variables.put("type", "variable"); + this.gameData.add(0, variables); + }else{ + this.variables = this.gameData.get(0); + } } private Long findLabelId(String var) { @@ -61,8 +71,6 @@ private void updateSprite(Map sprite) { )); if(Objects.equals((String) sprite.get("action"), "show")){ -// System.out.println("New Sprite: "+newSprite.getSprite()); -// System.out.println("Old Sprite: "+sprite.get("action")); if(sprite.get("wRatio")!=null){ newSprite.setPositioning( ((Long) sprite.get("wRatio")).intValue(), @@ -74,8 +82,6 @@ private void updateSprite(Map sprite) { ); } for (SpriteState oldSprite: this.state.getSprites()) { -// System.out.println("New Sprite: "+oldSprite.getSprite()); -// System.out.println("Old Sprite: "+newSprite.getSprite()); if(Objects.equals(oldSprite.getSprite(), newSprite.getSprite())){ removeSpriteByFolder(this.state.getSprites(), newSprite.getSprite()); @@ -88,7 +94,6 @@ private void updateSprite(Map sprite) { this.currentState++; } public void removeSpriteByFolder(List sprites, String folderName) { - //System.out.println("Remove: "+folderName); sprites.removeIf(sprite -> sprite.getSprite().equals(folderName)); } private void updateDialogue(String label, String content) { @@ -114,6 +119,7 @@ private Object processCommand(Map value) { if ("get_gamemode".equals(action)) { return "Survival"; } else if ("custom_command".equals(action)) { + // NOT IMPLEMENTED YET return "Nothing for now"; } return "Nothing for now"; @@ -144,13 +150,13 @@ private void modifyVariable(String variable, String operation, Object value) { } private void giveItem(String item, long amount) { - + // Not Implemented Yet this.currentState++; } private void processJump(Map action) { this.currentState = findLabelId((String) action.get("label")); - this.currentState++; //TODO: Figure out if this is necessary + this.currentState++; //TO-DO: Figure out if this is necessary (Update: Yes It Is) } @SuppressWarnings("unchecked") @@ -262,7 +268,7 @@ private boolean processAction(Map action) { private void processFinishing() { isEngineRunning=false; - ScriptLoader.saveState(variables,scriptName); + ScriptLoader.saveState(gameData,scriptName); shutdown = true; } diff --git a/src/main/java/org/arsparadox/mobtalkerredux/vn/model/ScriptLoader.java b/src/main/java/org/arsparadox/mobtalkerredux/vn/model/ScriptLoader.java index 1f2c274..71b45a5 100644 --- a/src/main/java/org/arsparadox/mobtalkerredux/vn/model/ScriptLoader.java +++ b/src/main/java/org/arsparadox/mobtalkerredux/vn/model/ScriptLoader.java @@ -10,7 +10,6 @@ import java.io.*; import java.lang.reflect.Type; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.List; import java.util.Map; @@ -74,24 +73,12 @@ public static List> loadDemo() throws IOException { } } - public static void saveState(Map variables, String filePath) { - try { - filePath = FMLPaths.CONFIGDIR.get() + "\\"+ MobTalkerRedux.MODID +"\\" + filePath; - // Load existing JSON content - List> list = loadJson(filePath); - - // Append the 'variables' map to the list - list.add(variables); - - // Convert the updated list back to a JSON string - String jsonContent = gson.toJson(list); - - // Save the JSON string back into the file - try (Writer writer = new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8)) { - writer.write(jsonContent); - } + public static void saveState(List> gamestate, String filePath) { + filePath = FMLPaths.CONFIGDIR.get() + "\\" + MobTalkerRedux.MODID + "\\" + filePath; + try (Writer writer = new FileWriter(filePath)) { + gson.toJson(gamestate, writer); } catch (IOException e) { - throw new RuntimeException("Failed to save state to file at " + filePath, e); + throw new RuntimeException("Failed to save gamestate to " + filePath, e); } } } \ No newline at end of file