aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/levelapi
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/sowgro/npehero/levelapi')
-rw-r--r--src/main/java/net/sowgro/npehero/levelapi/Difficulties.java3
-rwxr-xr-xsrc/main/java/net/sowgro/npehero/levelapi/Difficulty.java11
-rwxr-xr-xsrc/main/java/net/sowgro/npehero/levelapi/Levels.java5
3 files changed, 15 insertions, 4 deletions
diff --git a/src/main/java/net/sowgro/npehero/levelapi/Difficulties.java b/src/main/java/net/sowgro/npehero/levelapi/Difficulties.java
index ea17e21..cfb3673 100644
--- a/src/main/java/net/sowgro/npehero/levelapi/Difficulties.java
+++ b/src/main/java/net/sowgro/npehero/levelapi/Difficulties.java
@@ -82,7 +82,7 @@ public class Difficulties {
* @param text The name of the directory
* @throws IOException If there is a problem adding the level
*/
- public void add(String text) throws IOException {
+ public Difficulty add(String text) throws IOException {
File diffDir = new File(level.dir, text.toLowerCase().replaceAll("\\W+", "-"));
if (diffDir.exists()) {
throw new FileAlreadyExistsException(diffDir.getName());
@@ -92,6 +92,7 @@ public class Difficulties {
temp.title = text;
list.add(temp);
list.sort(Comparator.naturalOrder());
+ return temp;
}
else {
throw new IOException();
diff --git a/src/main/java/net/sowgro/npehero/levelapi/Difficulty.java b/src/main/java/net/sowgro/npehero/levelapi/Difficulty.java
index 2e99a7a..99fa285 100755
--- a/src/main/java/net/sowgro/npehero/levelapi/Difficulty.java
+++ b/src/main/java/net/sowgro/npehero/levelapi/Difficulty.java
@@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import java.io.*;
+import java.util.HashMap;
import java.util.Map;
/**
@@ -50,6 +51,9 @@ public class Difficulty implements Comparable<Difficulty>
return;
}
Map<String, Object> data = jsonParser.fromJson(new FileReader(jsonFile), Map.class);
+ if (data == null) {
+ data = new HashMap<>();
+ }
title = (String) data.getOrDefault("title", title);
bpm = (Double) data.getOrDefault("bpm", bpm);
@@ -78,8 +82,13 @@ public class Difficulty implements Comparable<Difficulty>
* @throws IOException If there is a problem writing to the file
*/
public void writeMetadata() throws IOException {
- jsonFile.createNewFile();
+ if (!jsonFile.createNewFile()) {
+ throw new IOException("Could not create file " + jsonFile.getAbsolutePath());
+ }
Map<String, Object> data = jsonParser.fromJson(new FileReader(jsonFile), Map.class); // start with previous values
+ if (data == null) {
+ data = new HashMap<>();
+ }
data.put("title", title);
data.put("endTime", endTime);
data.put("priority", order);
diff --git a/src/main/java/net/sowgro/npehero/levelapi/Levels.java b/src/main/java/net/sowgro/npehero/levelapi/Levels.java
index c82c2fa..37878d8 100755
--- a/src/main/java/net/sowgro/npehero/levelapi/Levels.java
+++ b/src/main/java/net/sowgro/npehero/levelapi/Levels.java
@@ -39,7 +39,7 @@ public class Levels {
try {
Level level = new Level(file);
list.add(level);
- } catch (IOException e) {
+ } catch (Exception e) {
problems.put(file.getName(), e);
e.printStackTrace();
}
@@ -52,7 +52,7 @@ public class Levels {
* @param text: the name of the directory and default title
* @throws IOException if there was an error adding the level
*/
- public static void add(String text) throws IOException {
+ public static Level add(String text) throws IOException {
File levelDir = new File(dir, text.toLowerCase().replaceAll("\\W+", "-"));
if (levelDir.exists()) {
throw new FileAlreadyExistsException(levelDir.getName());
@@ -61,6 +61,7 @@ public class Levels {
Level temp = new Level(levelDir);
temp.title = text;
list.add(temp);
+ return temp;
}
else {
throw new IOException();