Browse Source

Merge remote-tracking branch 'origin/develop' into shop

remotes/origin/shop
Max Gerbeth 2 years ago
parent
commit
ac6deabcfe
  1. 1
      src/main/java/org/bitbiome/Boot.java
  2. 30
      src/main/java/org/bitbiome/JsonParser.java
  3. 1
      src/main/java/org/bitbiome/Main.java
  4. 4
      src/main/java/org/bitbiome/classes/InteractionLoop.java
  5. 36
      src/main/java/org/bitbiome/classes/JsonParser.java

1
src/main/java/org/bitbiome/Boot.java

@ -1,5 +1,6 @@
package org.bitbiome; package org.bitbiome;
import org.bitbiome.classes.InteractionLoop;
import org.bitbiome.commands.CommandListener; import org.bitbiome.commands.CommandListener;
public class Boot { public class Boot {

30
src/main/java/org/bitbiome/JsonParser.java

@ -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;
}
}

1
src/main/java/org/bitbiome/Main.java

@ -5,7 +5,6 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
//JSONObject playerConfig = JsonParser.getJSONObject("playerconfig.json");
new Boot(); new Boot();
} }

4
src/main/java/org/bitbiome/InteractionLoop.java → 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; import java.util.Scanner;

36
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);
}
}
Loading…
Cancel
Save