Skip to content

Commit

Permalink
Merge pull request #6 from Iteranya/1.20.1_Built_In_Script
Browse files Browse the repository at this point in the history
1.20.1 built in script
  • Loading branch information
Iteranya authored Nov 5, 2024
2 parents d6535b4 + 27ca00f commit 1e46ee9
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 239 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
if (context.getSource().getEntity() instanceof ServerPlayer player) {
Minecraft.getInstance().execute(() -> {
try {
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadDemo())));
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadDemo(),"demo.json")));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -38,7 +38,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
Minecraft.getInstance().execute(() -> {
try {
player.sendSystemMessage(Component.literal("Trying to load the file config/mobtalkerredux/"+name));
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadScript(name))));
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadScript(name),"demo.json")));

} catch (IOException e) {
player.sendSystemMessage(Component.literal("Failed to find the file config/mobtalkerredux/"+name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public InteractionResult useOn(UseOnContext context) {

Minecraft.getInstance().execute(() -> {
try {
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadDemo())));
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadDemo(),"demo.json")));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.arsparadox.mobtalkerredux.vn.data.DialogueState;
import org.arsparadox.mobtalkerredux.vn.data.SpriteState;
import org.arsparadox.mobtalkerredux.vn.model.ScriptLoader;

import java.util.*;

Expand All @@ -14,11 +15,26 @@ public class VisualNovelEngine {
public DialogueState state;
public boolean isEngineRunning = false;

public VisualNovelEngine(List<Map<String, Object>> gameData) {
public String scriptName;

public VisualNovelEngine(List<Map<String, Object>> 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(!"variable".equals(this.gameData.get(this.gameData.size() - 1).get("type"))){
System.out.println(this.gameData.get(this.gameData.size() - 1).get("type"));
this.variables = new HashMap<>();
this.variables.put("type", "variable");
this.gameData.add(variables);
System.out.println("Initialize Variable");
}else{
this.variables = this.gameData.get(this.gameData.size() - 1);
}
}

private Long findLabelId(String var) {
Expand Down Expand Up @@ -57,8 +73,6 @@ private void updateSprite(Map<String, Object> 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(),
Expand All @@ -70,8 +84,6 @@ private void updateSprite(Map<String, Object> 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());
Expand All @@ -84,7 +96,6 @@ private void updateSprite(Map<String, Object> sprite) {
this.currentState++;
}
public void removeSpriteByFolder(List<SpriteState> sprites, String folderName) {
//System.out.println("Remove: "+folderName);
sprites.removeIf(sprite -> sprite.getSprite().equals(folderName));
}
private void updateDialogue(String label, String content) {
Expand All @@ -110,6 +121,7 @@ private Object processCommand(Map<String, Object> 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";
Expand Down Expand Up @@ -140,19 +152,22 @@ 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<String, Object> 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")
private void processConditional(Map<String, Object> condition) {
System.out.println("trying to get:"+condition.get("var"));
Object var = this.variables.get(condition.get("var"));
Object value = condition.get("value");
System.out.println(var);
System.out.println(value);
long end = (long) condition.get("end");

if (value instanceof Map) {
Expand All @@ -161,27 +176,33 @@ private void processConditional(Map<String, Object> condition) {

String conditionType = (String) condition.get("condition");
boolean result = false;
if(var!=null){
switch (conditionType) {
case "equal":
result = var.equals(value);
break;
case "not_equal":
result = !var.equals(value);
break;
case "less_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() < ((Number) value).doubleValue();
}
break;
case "greater_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() > ((Number) value).doubleValue();
}
break;
}

switch (conditionType) {
case "equal":
result = var.equals(value);
break;
case "not_equal":
result = !var.equals(value);
break;
case "less_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() < ((Number) value).doubleValue();
}
break;
case "greater_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() > ((Number) value).doubleValue();
}
break;
this.currentState = result ? this.currentState + 1 : end;
}
else{
this.currentState = end;
}


this.currentState = result ? this.currentState + 1 : end;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -248,18 +269,24 @@ private boolean processAction(Map<String, Object> action) {
this.currentState++;
break;
case "finish_dialogue":
isEngineRunning=false;
shutdown = true;
processFinishing();
default:
this.currentState++;
break;
}
return false;
}

private void processFinishing() {
isEngineRunning=false;
ScriptLoader.saveState(gameData,scriptName);
shutdown = true;
}

public void runEngine() {
while (isEngineRunning) { // Infinite loop
// Check if engine is running
System.out.println(this.currentState);
Map<String, Object> action = getDictById(this.currentState);
if(action == null){
shutdown = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,33 @@ public static List<Map<String, Object>> loadScript(String filePath) {
}

public static List<Map<String, Object>> loadDemo() throws IOException {
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
ResourceLocation resourceLocation = new ResourceLocation("mobtalkerredux", "demo.json");
String configPath = FMLPaths.CONFIGDIR.get() + "\\" + MobTalkerRedux.MODID + "\\" + "demo.json";
File configFile = new File(configPath);

try (InputStream inputStream = resourceManager.getResource(resourceLocation).get().open()) {
return loadJson(inputStream);
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Resource file demo.json could not be found.");
if (configFile.exists()) {
// Load from the config directory if it exists
System.out.println("Loading Save File");
return loadJson(configPath);
} else {
System.out.println("Making New Save File");
// Otherwise, try loading from the resource manager
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
ResourceLocation resourceLocation = new ResourceLocation("mobtalkerredux", "demo.json");

try (InputStream inputStream = resourceManager.getResource(resourceLocation).get().open()) {
return loadJson(inputStream);
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Resource file demo.json could not be found.");
}
}
}

public static void saveState(List<Map<String, Object>> 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 gamestate to " + filePath, e);
}
}
}
Loading

0 comments on commit 1e46ee9

Please sign in to comment.