From d9d97cd7c294abe9ec2da37d4cf554e3be00593c Mon Sep 17 00:00:00 2001 From: David Hermann Date: Tue, 7 Feb 2023 12:16:09 +0100 Subject: [PATCH] Hotfix for merge --- src/main/java/org/bitbiome/Boot.java | 3 +- .../org/bitbiome/classes/InteractionLoop.java | 4 +-- .../java/org/bitbiome/classes/JsonParser.java | 19 +----------- .../org/bitbiome/classes/TravelEngine.java | 11 +++---- .../bitbiome/commands/BlackJackCommand.java | 3 +- .../org/bitbiome/commands/QuizCommand.java | 12 ++++--- .../java/org/bitbiome/entities/Player.java | 5 ++- src/main/resources/playerconfig.json | 31 ++++++++++--------- src/main/resources/quiz.json | 2 +- 9 files changed, 36 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/bitbiome/Boot.java b/src/main/java/org/bitbiome/Boot.java index 0f5ebdd..4852196 100644 --- a/src/main/java/org/bitbiome/Boot.java +++ b/src/main/java/org/bitbiome/Boot.java @@ -26,8 +26,7 @@ public class Boot { private Player getPlayerSave() { String name; - JsonParser jp = new JsonParser(); - JSONObject playerconfig = jp.getJSONObject("playerconfig.json"); + JSONObject playerconfig = JsonParser.getJSONObject("src/main/resources/playerconfig.json"); name = playerconfig.getString("name"); return new Player(name); } diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index f8a6c84..4948f5c 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -40,10 +40,10 @@ public class InteractionLoop { + Colors.ANSI_RESET); String name = input.nextLine(); JsonParser jp = new JsonParser(); - JSONObject playerconf = jp.getJSONObject("playerconfig.json"); + JSONObject playerconf = JsonParser.getJSONObject("src/main/resources/playerconfig.json"); playerconf.put("name", name); travelEngine.getPlayer().setName(name); - jp.writeObject("playerconfig.json", playerconf); + JsonParser.writeObject("src/main/resources/playerconfig.json", playerconf); } } diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index e7ad5b7..d9657e1 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -1,13 +1,10 @@ package org.bitbiome.classes; import org.json.JSONObject; -import org.json.JSONTokener; import java.io.FileWriter; import java.io.IOException; import java.io.FileReader; -import java.io.InputStream; - public class JsonParser { @@ -26,7 +23,7 @@ public class JsonParser { https://github.com/stleary/JSON-java */ - public static JSONObject readJSONFile(String filePath) { + public static JSONObject getJSONObject(String filePath) { StringBuilder sb = null; try { FileReader reader = new FileReader(filePath); @@ -58,18 +55,4 @@ public class JsonParser { throw new RuntimeException(e); } } - - public void writeObject(String fileName, JSONObject object) { - - String resourceName = System.getProperty("user.dir") + "/src/main/resources/" + fileName; - try { - FileWriter fw = new FileWriter(resourceName, false); - fw.write(object.toString(1)); - fw.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - - - } } diff --git a/src/main/java/org/bitbiome/classes/TravelEngine.java b/src/main/java/org/bitbiome/classes/TravelEngine.java index a8d3213..0573cd9 100644 --- a/src/main/java/org/bitbiome/classes/TravelEngine.java +++ b/src/main/java/org/bitbiome/classes/TravelEngine.java @@ -10,21 +10,19 @@ import java.util.ArrayList; public class TravelEngine { private JSONArray locations; - private JsonParser jp; private Player player; public TravelEngine(Player player) { - jp = new JsonParser(); - locations = jp.getJSONObject("gameconfig.json").getJSONArray("locations"); + locations = JsonParser.getJSONObject("src/main/resources/gameconfig.json").getJSONArray("locations"); this.player = player; } public void travelTo(Location location) { player.setLocation(location); - JSONObject jObj = jp.getJSONObject("playerconfig.json"); + JSONObject jObj = JsonParser.getJSONObject("src/main/resources/playerconfig.json"); jObj.put("currentLocation", location.getName()); - jp.writeObject("playerconfig.json", jObj); + JsonParser.writeObject("src/main/resources/playerconfig.json", jObj); } public Player getPlayer() { @@ -45,8 +43,7 @@ public class TravelEngine { } public Location getLocationByName(String name) { - JsonParser jp = new JsonParser(); - JSONObject gameconfig = jp.getJSONObject("gameconfig.json"); + JSONObject gameconfig = JsonParser.getJSONObject("src/main/resources/gameconfig.json"); JSONArray locations = gameconfig.getJSONArray("locations"); JSONObject location = null; if (locationExists(name)) { diff --git a/src/main/java/org/bitbiome/commands/BlackJackCommand.java b/src/main/java/org/bitbiome/commands/BlackJackCommand.java index 52e854a..9794e95 100644 --- a/src/main/java/org/bitbiome/commands/BlackJackCommand.java +++ b/src/main/java/org/bitbiome/commands/BlackJackCommand.java @@ -2,6 +2,7 @@ package org.bitbiome.commands; import org.bitbiome.classes.BlackJack; +import org.bitbiome.classes.TravelEngine; import java.util.Random; import java.util.Scanner; @@ -9,7 +10,7 @@ import java.util.Scanner; public class BlackJackCommand implements CommandAPI { private boolean over; @Override - public void performCommand(Scanner scanner, boolean isRunning, String message) { + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { System.out.println("Du hast das Spiel BlackJack gestartet. Die Spielregeln lauten wie folgt: Du und dein Gegner bekommen jede Runde Zahlen von 4 - 11. \nDerjenige, der zuerst 21 Punkte hat gewinnt. Derjenige, der über 21 Punkte hat verliert. Möchte keiner mehr Karten ziehen, gewinnt der mit dem höchsten Blatt!\nViel Spaß!"); diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index db0316e..59d215f 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -4,6 +4,8 @@ package org.bitbiome.commands; import org.bitbiome.classes.JsonParser; import org.json.JSONArray; import org.json.JSONObject; +import org.bitbiome.classes.TravelEngine; + import java.util.Date; import java.util.Random; @@ -12,11 +14,11 @@ import java.util.Scanner; public class QuizCommand implements CommandAPI { private Scanner quizScanner; @Override - public void performCommand(Scanner scanner, boolean isRunning, String message) { + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { quizScanner = new Scanner(System.in); - String path = "src\\main\\resources\\quiz.json"; - JSONObject quiz = JsonParser.readJSONFile(path); + String path = "src/main/resources/quiz.json"; + JSONObject quiz = JsonParser.getJSONObject(path); long diffTime = canPlayAgain(quiz.getLong("lastPlayed")); if (diffTime > 0) { @@ -76,8 +78,8 @@ public class QuizCommand implements CommandAPI { } public static int addGold() { - String playerpath = "src\\main\\resources\\playerconfig.json"; - JSONObject playerconfig = JsonParser.readJSONFile(playerpath); + String playerpath = "src/main/resources/ssplayerconfig.json"; + JSONObject playerconfig = JsonParser.getJSONObject(playerpath); int gold = playerconfig.getInt("gold"); gold = gold + 5; playerconfig.put("gold", gold); diff --git a/src/main/java/org/bitbiome/entities/Player.java b/src/main/java/org/bitbiome/entities/Player.java index 3d46e17..72a903c 100644 --- a/src/main/java/org/bitbiome/entities/Player.java +++ b/src/main/java/org/bitbiome/entities/Player.java @@ -12,13 +12,12 @@ public class Player { private ArrayList inventory; - private JsonParser jp; + public Player(String name) { - jp = new JsonParser(); this.name = name; hp = 100.0F; - location = new Location(jp.getJSONObject("playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>()); + location = new Location(JsonParser.getJSONObject("src/main/resources/playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>()); inventory = new ArrayList<>(); } diff --git a/src/main/resources/playerconfig.json b/src/main/resources/playerconfig.json index 1a2c55e..9da94a1 100644 --- a/src/main/resources/playerconfig.json +++ b/src/main/resources/playerconfig.json @@ -1,17 +1,18 @@ { - "name": "null", - "gold": 0, - "hp": 10, - "currentLocation": "Wald", - "inventory": [ - { - "name": "Holz", - "amount": "5", - "durability": 1000 - },{ - "name": "Stein", - "amount": "5", - "durability": 1000 - } - ] + "gold": 0, + "name": "Dave", + "hp": 10, + "inventory": [ + { + "amount": "5", + "durability": 1000, + "name": "Holz" + }, + { + "amount": "5", + "durability": 1000, + "name": "Stein" + } + ], + "currentLocation": "Wald" } \ No newline at end of file diff --git a/src/main/resources/quiz.json b/src/main/resources/quiz.json index a36366a..ae61307 100644 --- a/src/main/resources/quiz.json +++ b/src/main/resources/quiz.json @@ -201,5 +201,5 @@ ] } ], - "lastPlayed": 0 + "lastPlayed": 1675768444160 } \ No newline at end of file