diff --git a/src/main/java/org/bitbiome/Boot.java b/src/main/java/org/bitbiome/Boot.java index 590ef38..b264413 100644 --- a/src/main/java/org/bitbiome/Boot.java +++ b/src/main/java/org/bitbiome/Boot.java @@ -1,5 +1,6 @@ package org.bitbiome; +import org.bitbiome.classes.InteractionLoop; import org.bitbiome.commands.CommandListener; public class Boot { diff --git a/src/main/java/org/bitbiome/JsonParser.java b/src/main/java/org/bitbiome/JsonParser.java deleted file mode 100644 index 5852e7e..0000000 --- a/src/main/java/org/bitbiome/JsonParser.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.bitbiome; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONTokener; - -import java.io.InputStream; - -public class JsonParser { - public static JSONObject getJSONObject(String fileName) { - String resourceName = "./../../" + fileName; - InputStream is = JsonParser.class.getResourceAsStream(resourceName); - if (is == null) { - throw new NullPointerException("Cannot find resource file " + resourceName); - } - - JSONTokener tokener = new JSONTokener(is); - JSONObject object = new JSONObject(tokener); - System.out.println("Name: " + object.getString("name")); - - System.out.println("Inventory: "); - JSONArray inventory = object.getJSONArray("inventory"); - - for (int i = 0; i < inventory.length(); i++) { - JSONObject invObj = inventory.getJSONObject(i); - System.out.println(" - " + invObj.getString("name")); - } - return object; - } -} diff --git a/src/main/java/org/bitbiome/Main.java b/src/main/java/org/bitbiome/Main.java index e614ea5..05a3de9 100644 --- a/src/main/java/org/bitbiome/Main.java +++ b/src/main/java/org/bitbiome/Main.java @@ -5,7 +5,6 @@ public class Main { public static void main(String[] args) { - //JSONObject playerConfig = JsonParser.getJSONObject("playerconfig.json"); new Boot(); } diff --git a/src/main/java/org/bitbiome/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java similarity index 88% rename from src/main/java/org/bitbiome/InteractionLoop.java rename to src/main/java/org/bitbiome/classes/InteractionLoop.java index e2a1cd0..6566b0a 100644 --- a/src/main/java/org/bitbiome/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -1,4 +1,6 @@ -package org.bitbiome; +package org.bitbiome.classes; + +import org.bitbiome.Boot; import java.util.Scanner; diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java new file mode 100644 index 0000000..be0e9b8 --- /dev/null +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -0,0 +1,36 @@ +package org.bitbiome.classes; + +import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.InputStream; + +public class JsonParser { + + /* + Usage of this method: + Get a whole json file with this method. + After that you have the JSON File as an object. + The following methods are available: + object.getString(String keyName); -> For values. + + object.getJSONArray(String keyName); -> For Arrays + jsonArray.getJSONObject(int index); + + More: https://www.baeldung.com/java-org-json + https://stleary.github.io/JSON-java/index.html + https://github.com/stleary/JSON-java + */ + + public static JSONObject getJSONObject(String fileName) { + String resourceName = "./../../" + fileName; + InputStream is = JsonParser.class.getResourceAsStream(resourceName); + if (is == null) { + throw new NullPointerException("Cannot find resource file " + resourceName); + } + + JSONTokener tokener = new JSONTokener(is); + + return new JSONObject(tokener); + } +}