aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/levelapi/Levels.java
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2024-07-28 01:07:41 -0400
committersowgro <tpoke.ferrari@gmail.com>2024-07-28 01:07:41 -0400
commit0ce09f72f4af26412356b9699d402b52dbcfc94f (patch)
treeb01b94b1b80d1f3fc5aea559b3718024b79cfe91 /src/main/java/net/sowgro/npehero/levelapi/Levels.java
parentd04c277edff957d14b6261dd38da43c18b7ba189 (diff)
downloadNPEhero-0ce09f72f4af26412356b9699d402b52dbcfc94f.tar.gz
NPEhero-0ce09f72f4af26412356b9699d402b52dbcfc94f.tar.bz2
NPEhero-0ce09f72f4af26412356b9699d402b52dbcfc94f.zip
Finalize level API and new Json library
Diffstat (limited to 'src/main/java/net/sowgro/npehero/levelapi/Levels.java')
-rwxr-xr-xsrc/main/java/net/sowgro/npehero/levelapi/Levels.java97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/main/java/net/sowgro/npehero/levelapi/Levels.java b/src/main/java/net/sowgro/npehero/levelapi/Levels.java
new file mode 100755
index 0000000..84ffe51
--- /dev/null
+++ b/src/main/java/net/sowgro/npehero/levelapi/Levels.java
@@ -0,0 +1,97 @@
+package net.sowgro.npehero.levelapi;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.file.FileAlreadyExistsException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Comparator;
+import java.util.HashMap;
+
+import javafx.collections.FXCollections;
+import javafx.collections.ObservableList;
+
+/**
+ * Stores a list of all the levels
+ */
+public class Levels {
+
+ public static final ObservableList<Level> list = FXCollections.observableArrayList();
+ public static final HashMap<String, Exception> problems = new HashMap<>();
+
+ private static final File dir = new File("levels");
+
+ /**
+ * Reads contents of the levels folder and creates a level form each subfolder
+ * <p>
+ * All subfolders in the levels folder are assumed to be levels
+ * @throws FileNotFoundException If the levels folder is missing.
+ * @throws IOException If there is a problem reading in the levels.
+ */
+ public static void readData() throws IOException {
+ list.clear();
+ File[] fileList = dir.listFiles();
+ if (fileList == null) {
+ throw new FileNotFoundException();
+ }
+ for (File file: fileList) {
+ try {
+ Level level = new Level(file);
+ list.add(level);
+ } catch (IOException e) {
+ problems.put("", e);
+ e.printStackTrace();
+ }
+ }
+ list.sort(Comparator.naturalOrder());
+ }
+
+ /**
+ * Creates a subfolder in the levels folder for the new level then creates the level with it
+ * @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 {
+ File levelDir = new File(dir, text.toLowerCase().replaceAll("\\W+", "-"));
+ if (levelDir.exists()) {
+ throw new FileAlreadyExistsException(levelDir.getName());
+ }
+ if (levelDir.mkdirs()) {
+ Level temp = new Level(levelDir);
+ temp.title = text;
+ list.add(temp);
+ }
+ else {
+ throw new IOException();
+ }
+ }
+
+ /**
+ * Removes level from the filesystem then reloads this levelController
+ * @param level: the level to be removed
+ * @throws IOException If there is a problem deleting the level
+ */
+ public static void remove(Level level) throws IOException {
+ File hold = level.dir;
+ Files.walk(hold.toPath())
+ .sorted(Comparator.reverseOrder())
+ .map(Path::toFile)
+ .forEach(File::delete);
+ list.remove(level);
+ }
+
+ /**
+ * Gets a list of only the valid levels.
+ * @return A list of the valid levels.
+ */
+ public static ObservableList<Level> getValidList() {
+ ObservableList<Level> validList = FXCollections.observableArrayList();
+ for (Level level : list) {
+ if (level.isValid()) {
+ validList.add(level);
+ }
+ }
+ return validList;
+ }
+} \ No newline at end of file