Skip to content

Commit

Permalink
Merge pull request #3 from Iteranya/1.20.1
Browse files Browse the repository at this point in the history
1.20.1
  • Loading branch information
Iteranya authored Nov 2, 2024
2 parents 287e97a + 563d539 commit e667a12
Show file tree
Hide file tree
Showing 22 changed files with 1,187 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ run/

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
/gradle/wrapper/gradle-wrapper.jar
/gradlew
/gradlew.bat
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("story.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 @@ -3,6 +3,7 @@
import net.minecraft.resources.ResourceLocation;
import org.arsparadox.mobtalkerredux.vn.data.DialogueState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -40,7 +41,7 @@ private Map<String, Object> getDictById(int targetId) {

private void updateSprite(String spritePath) {
ResourceLocation location = new ResourceLocation(
"mobtalkerredux", "textures/characters/" + spritePath
"mobtalkerredux", "textures/" + spritePath
);
state.setSprite(location);
this.currentState++;
Expand Down Expand Up @@ -98,7 +99,7 @@ private void giveItem(String item, int amount) {
}

private void processJump(Map<String, Object> action) {
currentState = findLabelId((String) action.get("label"));
this.currentState = findLabelId((String) action.get("label"));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -159,7 +160,7 @@ private boolean processAction(Map<String, Object> action) {

switch (actionType) {
case "show_sprite":
updateSprite((String) action.get("sprite"));
updateSprite((String) action.get("location"));
return true;
case "dialogue":
updateDialogue((String) action.get("label"), (String) action.get("content"));
Expand All @@ -181,6 +182,7 @@ private boolean processAction(Map<String, Object> action) {
}
break;
case "choice":
System.out.println("Try to yoink choice"+ action);
updateChoices((List<Map<String, Object>>) action.get("choice"));
break;
case "command":
Expand All @@ -202,23 +204,16 @@ private boolean processAction(Map<String, Object> action) {
public void runEngine() {
while (isEngineRunning) { // Infinite loop
// Check if engine is running
System.out.println("Engine State = "+ (this.currentState));
if(isEngineRunning){
Map<String, Object> action = getDictById(this.currentState);
if(action == null){
shutdown = true;
isEngineRunning = false;
return;
}
if ("meta".equals(action.get("type"))) {
processMeta(action);
} else {
processAction(action);
}
}else{

return;

Map<String, Object> action = getDictById(this.currentState);
if(action == null){
shutdown = true;
isEngineRunning = false;
return;
}
if ("meta".equals(((Map<?, ?>) action).get("type"))) {
processMeta(action);
} else {
processAction(action);
}

}
Expand All @@ -237,5 +232,6 @@ public DialogueState getNext() {

public void buttonPress(String choice) {
changeStateByLabel(choice);
this.state.setChoices(new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ public List<Map<String, Object>> getChoices() {
}
public void setChoices(List<Map<String, Object>> choices) { this.choices = choices; }

public void emptyChoices() { this.choices.clear(); }
}
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,16 +33,46 @@ 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());
// }

public static List<Map<String, Object>> loadScript(String filePath) throws FileNotFoundException {
filePath = FMLPaths.CONFIGDIR.get() +"\\" +filePath;
filePath = FMLPaths.CONFIGDIR.get() +"\\mobtalkerredux\\" +filePath;
ArrayNode jsonArray = loadJson(filePath);
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ private void startScene() {
private void onPress(String choice) { // BUTTON press, btw
vn.buttonPress(choice);//Tell engine to update their globals then update this class's global
//After clicking, it tries to load the next dialogue...
vn.state.emptyChoices();
vn.isEngineRunning=true;
vn.runEngine();
update();
Expand All @@ -101,15 +100,17 @@ public void render(GuiGraphics poseStack, int mouseX, int mouseY, float partialT
}

public void renderCharacterSprite(GuiGraphics poseStack) {
int spriteX = this.width;
int spriteY = this.height;
if(sprite!=null){
RenderSystem.setShaderTexture(0, sprite);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);

// Calculate sprite position to center it
int spriteX = (this.width - DISPLAYED_SPRITE_WIDTH) / 2;
int spriteY = (this.height - DISPLAYED_SPRITE_HEIGHT) / 3; // Position it in upper third
spriteX = (this.width - DISPLAYED_SPRITE_WIDTH) / 2;
spriteY = (this.height - DISPLAYED_SPRITE_HEIGHT) / 3; // Position it in upper third

// Render the sprite with proper scaling

Expand Down
Loading

0 comments on commit e667a12

Please sign in to comment.