diff options
Diffstat (limited to 'src/main/java/net')
17 files changed, 225 insertions, 179 deletions
diff --git a/src/main/java/net/sowgro/npehero/Driver.java b/src/main/java/net/sowgro/npehero/Driver.java index 4c737f7..3e1bd6e 100755 --- a/src/main/java/net/sowgro/npehero/Driver.java +++ b/src/main/java/net/sowgro/npehero/Driver.java @@ -12,10 +12,14 @@ import javafx.scene.input.KeyEvent; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.util.Duration; +import net.sowgro.npehero.editor.ErrorDisplay; import net.sowgro.npehero.main.*; import net.sowgro.npehero.gui.MainMenu; +import java.io.FileNotFoundException; import java.net.URL; +import java.util.List; +import java.util.Stack; public class Driver extends Application @@ -42,9 +46,6 @@ public class Driver extends Application */ @Override public void start(Stage newPrimaryStage) { - Settings.read(); - Levels.readData(); - Control.readFromFile(); primaryStage = newPrimaryStage; @@ -65,8 +66,7 @@ public class Driver extends Application primaryStage.setTitle("NPE Hero"); primaryPane.getStyleClass().remove("scroll-pane"); - - setMenu(new MainMenu()); + setMenuBackground(); Sound.playSong(Sound.MENU_SONG); @@ -79,6 +79,28 @@ public class Driver extends Application primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH); primaryStage.setFullScreenExitHint(""); primaryStage.show(); + + Stack<String> errors = new Stack<>(); + try { + Settings.read(); + } catch (Exception e) { + errors.push("Failed to load settings from file\n"+e); + } + try { + Levels.readData(); + } catch (FileNotFoundException e) { + errors.push("Failed to load levels: Level folder is missing\n"); + } + try { + Control.readFromFile(); + } catch (Exception e) { + errors.push("Failed to load controls from file\n"+e); + } + Page last = new MainMenu(); + while (!errors.empty()) { + last = new ErrorDisplay(errors.pop(), last); + } + Driver.setMenu(last); } /** diff --git a/src/main/java/net/sowgro/npehero/editor/DiffEditor.java b/src/main/java/net/sowgro/npehero/editor/DiffEditor.java index f98ff46..6940485 100755 --- a/src/main/java/net/sowgro/npehero/editor/DiffEditor.java +++ b/src/main/java/net/sowgro/npehero/editor/DiffEditor.java @@ -61,7 +61,7 @@ public class DiffEditor extends Page Button playLevel = new Button("Play level"); playLevel.setOnAction(_ -> { - if (diff.isValid && diff.level.isValid) { + if (diff.isValid() && diff.level.isValid()) { Driver.setMenu(new LevelSurround(diff.level, diff, this)); } else { diff --git a/src/main/java/net/sowgro/npehero/editor/DiffList.java b/src/main/java/net/sowgro/npehero/editor/DiffList.java index 13e4795..7017c32 100755 --- a/src/main/java/net/sowgro/npehero/editor/DiffList.java +++ b/src/main/java/net/sowgro/npehero/editor/DiffList.java @@ -14,6 +14,7 @@ import net.sowgro.npehero.main.Level; import net.sowgro.npehero.main.Page; import net.sowgro.npehero.main.Sound; +import java.io.IOException; import java.util.Collections; public class DiffList extends Page @@ -33,7 +34,7 @@ public class DiffList extends Page titleCol.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().title)); validCol.setCellValueFactory(data -> { - if (data.getValue().isValid) { + if (data.getValue().isValid()) { return new ReadOnlyStringWrapper("Yes"); } else { @@ -63,7 +64,13 @@ public class DiffList extends Page edit.disableProperty().bind(diffs.getSelectionModel().selectedItemProperty().isNull()); Button remove = new Button("Delete"); - remove.setOnAction(e -> level.difficulties.remove(diffs.getSelectionModel().getSelectedItem())); + remove.setOnAction(e -> { + try { + level.difficulties.remove(diffs.getSelectionModel().getSelectedItem()); + } catch (IOException ex) { + Driver.setMenu(new ErrorDisplay("Failed to remove difficulty\n"+e, this)); + } + }); remove.setDisable(true); remove.disableProperty().bind(diffs.getSelectionModel().selectedItemProperty().isNull()); @@ -139,7 +146,11 @@ public class DiffList extends Page }); newLevelButton.setOnAction(_ -> { - level.difficulties.add(newLevelEntry.getText()); + try { + level.difficulties.add(newLevelEntry.getText()); + } catch (IOException e) { + Driver.setMenu(new ErrorDisplay("Failed to add level\n"+e, this)); + } newLevelEntry.clear(); refresh.fire(); sidebar.getChildren().clear(); diff --git a/src/main/java/net/sowgro/npehero/editor/LevelEditor.java b/src/main/java/net/sowgro/npehero/editor/LevelEditor.java index 56a2924..b3ae2b8 100755 --- a/src/main/java/net/sowgro/npehero/editor/LevelEditor.java +++ b/src/main/java/net/sowgro/npehero/editor/LevelEditor.java @@ -78,7 +78,7 @@ public class LevelEditor extends Page ValidIndicator diffsInvalid = new ValidIndicator(); - if (level.difficulties.validList.isEmpty()) { + if (level.difficulties.getValidList().isEmpty()) { diffsInvalid.setInvalid("This level contains no valid difficulties!"); } HBox diffLabel = new HBox(new Text("Difficulties"), diffsInvalid); @@ -95,7 +95,7 @@ public class LevelEditor extends Page diffCol.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().title)); validCol.setCellValueFactory(data -> { - if (data.getValue().isValid) { + if (data.getValue().isValid()) { return new ReadOnlyStringWrapper("Yes"); } else { @@ -131,14 +131,19 @@ public class LevelEditor extends Page level.colors[2] = colorsPickers[2].getValue(); level.colors[3] = colorsPickers[3].getValue(); level.colors[4] = colorsPickers[4].getValue(); - if (selectedBackground != null && selectedBackground.exists()) { - level.addFile(selectedBackground,"background." + getFileExtension(selectedBackground)); - } - if (selectedPreview != null && selectedPreview.exists()) { - level.addFile(selectedPreview,"preview." + getFileExtension(selectedPreview)); - } - if (selectedSong != null) { - level.addFile(selectedSong,"song." + getFileExtension(selectedSong)); + + try { + if (selectedBackground != null && selectedBackground.exists()) { + level.addFile(selectedBackground, "background." + getFileExtension(selectedBackground)); + } + if (selectedPreview != null && selectedPreview.exists()) { + level.addFile(selectedPreview, "preview." + getFileExtension(selectedPreview)); + } + if (selectedSong != null) { + level.addFile(selectedSong, "song." + getFileExtension(selectedSong)); + } + } catch (Exception _) { + // TODO } level.writeMetadata(); }); @@ -166,7 +171,7 @@ public class LevelEditor extends Page Driver.setMenu(prev); }); - HBox bottom = new HBox(save, exit); + HBox bottom = new HBox(exit, save); bottom.setAlignment(Pos.CENTER); bottom.setSpacing(10); diff --git a/src/main/java/net/sowgro/npehero/editor/LevelList.java b/src/main/java/net/sowgro/npehero/editor/LevelList.java index b479f49..1bc85dd 100755 --- a/src/main/java/net/sowgro/npehero/editor/LevelList.java +++ b/src/main/java/net/sowgro/npehero/editor/LevelList.java @@ -14,6 +14,9 @@ import net.sowgro.npehero.main.Levels; import net.sowgro.npehero.main.Page; import net.sowgro.npehero.main.Sound; +import java.io.FileNotFoundException; +import java.io.IOException; + public class LevelList extends Page { private HBox content = new HBox(); @@ -34,7 +37,7 @@ public class LevelList extends Page titleCol.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().title)); artistCol.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().artist)); validCol.setCellValueFactory(data -> { - if (data.getValue().isValid) { + if (data.getValue().isValid()) { return new ReadOnlyStringWrapper("Yes"); } else { @@ -63,13 +66,23 @@ public class LevelList extends Page edit.disableProperty().bind(levels.getSelectionModel().selectedItemProperty().isNull()); Button remove = new Button("Delete"); - remove.setOnAction(e -> Levels.remove(levels.getSelectionModel().getSelectedItem())); + remove.setOnAction(e -> { + try { + Levels.remove(levels.getSelectionModel().getSelectedItem()); + } catch (IOException ex) { + Driver.setMenu(new ErrorDisplay("Failed to remove this level\n"+e.toString(), this)); + } + }); remove.setDisable(true); remove.disableProperty().bind(levels.getSelectionModel().selectedItemProperty().isNull()); Button refresh = new Button("Refresh"); refresh.setOnAction(e -> { - Levels.readData(); + try { + Levels.readData(); + } catch (FileNotFoundException ex) { + Driver.setMenu(new ErrorDisplay("Failed to load levels: Level folder is missing\n"+e.toString(), this)); + } levels.setItems(Levels.list); }); @@ -120,7 +133,11 @@ public class LevelList extends Page }); newLevelButton.setOnAction(_ -> { - Levels.add(newLevelEntry.getText()); + try { + Levels.add(newLevelEntry.getText()); + } catch (IOException e) { + Driver.setMenu(new ErrorDisplay("Failed to create this level\n"+e.toString(), this)); + } newLevelEntry.clear(); refresh.fire(); sidebar.getChildren().clear(); diff --git a/src/main/java/net/sowgro/npehero/editor/NotesEditor2.java b/src/main/java/net/sowgro/npehero/editor/NotesEditor2.java index 38ad234..8d17900 100644 --- a/src/main/java/net/sowgro/npehero/editor/NotesEditor2.java +++ b/src/main/java/net/sowgro/npehero/editor/NotesEditor2.java @@ -175,7 +175,7 @@ public class NotesEditor2 extends Page { buttons.setAlignment(Pos.CENTER); Runnable updateEndLine = () -> { - System.out.println("LISTENER CALLED"); +// System.out.println("LISTENER CALLED"); if (newEndTime.get() != 0) { endLine.layoutYProperty().bind(secondToScreenPos(newEndTime.get())); } diff --git a/src/main/java/net/sowgro/npehero/gameplay/SongPlayer.java b/src/main/java/net/sowgro/npehero/gameplay/SongPlayer.java index 2f95ee8..36f706c 100755 --- a/src/main/java/net/sowgro/npehero/gameplay/SongPlayer.java +++ b/src/main/java/net/sowgro/npehero/gameplay/SongPlayer.java @@ -143,7 +143,7 @@ public class SongPlayer extends Pane { * The keyboard detection for the game: when a key is pressed it * calls the checkNote() method for the corresponding lane */ - System.out.println(e.getCode()); +// System.out.println(e.getCode()); // if (super.isVisible()) // { if (e.getCode() == Control.LANE0.getKey()) { diff --git a/src/main/java/net/sowgro/npehero/gui/ControlEditor.java b/src/main/java/net/sowgro/npehero/gui/ControlEditor.java index 1d1a73d..90161e0 100644 --- a/src/main/java/net/sowgro/npehero/gui/ControlEditor.java +++ b/src/main/java/net/sowgro/npehero/gui/ControlEditor.java @@ -8,10 +8,12 @@ import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.*; import net.sowgro.npehero.Driver; +import net.sowgro.npehero.editor.ErrorDisplay; import net.sowgro.npehero.main.Control; import net.sowgro.npehero.main.Page; import net.sowgro.npehero.main.Sound; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -70,9 +72,13 @@ public class ControlEditor extends Page { controlButton.setText(keyToString(control.keyProperty.get())); control.keyProperty.addListener(_ -> { controlButton.setText(keyToString(control.keyProperty.get())); - Control.writeToFile(); + try { + Control.writeToFile(); + } catch (IOException e) { + Driver.setMenu(new ErrorDisplay("An error occured while saving your controls\n"+e, this)); + } }); - controlButton.setOnMouseClicked(_ -> { + controlButton.setOnAction(_ -> { EventHandler<KeyEvent> keyListener = new EventHandler<>() { @Override public void handle(KeyEvent k) { @@ -93,7 +99,7 @@ public class ControlEditor extends Page { // reset button Button resetButton = new Button("Reset"); - resetButton.setOnMouseClicked(_ -> { + resetButton.setOnAction(_ -> { control.keyProperty.set(control.defaultKey); }); controls.add(resetButton, 2, i); diff --git a/src/main/java/net/sowgro/npehero/gui/LevelDetails.java b/src/main/java/net/sowgro/npehero/gui/LevelDetails.java index afbdce4..4bd1e09 100755 --- a/src/main/java/net/sowgro/npehero/gui/LevelDetails.java +++ b/src/main/java/net/sowgro/npehero/gui/LevelDetails.java @@ -86,7 +86,7 @@ public class LevelDetails extends VBox FlowPane diffSelector = new FlowPane(); diffSelector.setAlignment(Pos.CENTER); ToggleGroup diffToggleGroup = new ToggleGroup(); //allows only one to be selected at a time - for (Difficulty diff : level.difficulties.validList) //adds a button for each diff + for (Difficulty diff : level.difficulties.getValidList()) //adds a button for each diff { RadioButton temp = new RadioButton(); temp.getStyleClass().remove("radio-button"); //makes the buttons not look like a radio button and instead a normal button diff --git a/src/main/java/net/sowgro/npehero/gui/LevelSelector.java b/src/main/java/net/sowgro/npehero/gui/LevelSelector.java index 1c840b4..3c8f25f 100755 --- a/src/main/java/net/sowgro/npehero/gui/LevelSelector.java +++ b/src/main/java/net/sowgro/npehero/gui/LevelSelector.java @@ -32,7 +32,7 @@ public class LevelSelector extends Page titleCol.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().title)); artistCol.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().artist)); - levels.setItems(Levels.validList); + levels.setItems(Levels.getValidList()); levels.prefWidthProperty().bind(content.prefWidthProperty().multiply(0.25)); levels.prefHeightProperty().bind(content.prefHeightProperty().multiply(0.75)); diff --git a/src/main/java/net/sowgro/npehero/gui/SettingsEditor.java b/src/main/java/net/sowgro/npehero/gui/SettingsEditor.java index d9bad03..12555ed 100755 --- a/src/main/java/net/sowgro/npehero/gui/SettingsEditor.java +++ b/src/main/java/net/sowgro/npehero/gui/SettingsEditor.java @@ -11,10 +11,13 @@ import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.text.Text; import net.sowgro.npehero.Driver; +import net.sowgro.npehero.editor.ErrorDisplay; import net.sowgro.npehero.main.Page; import net.sowgro.npehero.main.Settings; import net.sowgro.npehero.main.Sound; +import java.io.IOException; + public class SettingsEditor extends Page { private final HBox content = new HBox(); @@ -89,7 +92,11 @@ public class SettingsEditor extends Page Button exit = new Button(); exit.setText("Back"); exit.setOnAction(e -> { - Settings.save(); + try { + Settings.save(); + } catch (IOException ex) { + Driver.setMenu(new ErrorDisplay("Failed to save settings"+e, this)); + } Sound.playSfx(Sound.BACKWARD); Driver.setMenu(new MainMenu()); }); diff --git a/src/main/java/net/sowgro/npehero/main/Control.java b/src/main/java/net/sowgro/npehero/main/Control.java index d9e3942..6e608ec 100644 --- a/src/main/java/net/sowgro/npehero/main/Control.java +++ b/src/main/java/net/sowgro/npehero/main/Control.java @@ -79,27 +79,15 @@ public enum Control { }; } - public static void writeToFile() { + public static void writeToFile() throws IOException { for (Control control : Control.values()) { jsonFile.set(control.toString(), control.getKey().toString()); } - - try { - jsonFile.write(); - } - catch (IOException e) { - System.err.println("Error writing to controls.json"); - } + jsonFile.write(); } - public static void readFromFile() { - try { - jsonFile.read(); - } - catch (Exception e) { - System.err.println("Error reading from controls.json"); - } - + public static void readFromFile() throws Exception { + jsonFile.read(); for (Control control : Control.values()) { if (jsonFile.containsKey(control.toString())) { control.setKey(KeyCode.valueOf(jsonFile.getString(control.toString(), null))); diff --git a/src/main/java/net/sowgro/npehero/main/Difficulties.java b/src/main/java/net/sowgro/npehero/main/Difficulties.java index f2ccd99..9549a5a 100644 --- a/src/main/java/net/sowgro/npehero/main/Difficulties.java +++ b/src/main/java/net/sowgro/npehero/main/Difficulties.java @@ -1,7 +1,6 @@ package net.sowgro.npehero.main; import javafx.collections.FXCollections; -import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; import java.io.File; @@ -9,21 +8,11 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; +import java.util.List; public class Difficulties { public ObservableList<Difficulty> list = FXCollections.observableArrayList(); - public ObservableList<Difficulty> validList = FXCollections.observableArrayList(); - { - list.addListener((ListChangeListener<? super Difficulty>) e -> { - validList.clear(); - for (Difficulty difficulty : list) { - if (difficulty.isValid) { - validList.add(difficulty); - } - } - }); - } Level level; public Difficulties(Level level) { @@ -31,12 +20,16 @@ public class Difficulties { } public void read() { - for(File cur: level.dir.listFiles()) //iterates through all files/folders in /levels/LEVEL + list.clear(); + File[] fileList = level.dir.listFiles(); + if (fileList == null) { + return; + } + for(File cur: fileList) //iterates through all files/folders in /levels/LEVEL { if (cur.isDirectory()) //all subfolders within a level folder are difficulties { Difficulty diff = new Difficulty(cur,level); - diff.readData(); list.add(diff); } } @@ -47,41 +40,47 @@ public class Difficulties { * Removes the difficaulty from the filesystem then reloads the level * @param diff: the difficulty to be removed */ - public void remove(Difficulty diff) + public void remove(Difficulty diff) throws IOException { File hold = diff.thisDir; - try { - Files.walk(hold.toPath()) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); - list.remove(diff); - } catch (IOException e) { - e.printStackTrace(); - } + Files.walk(hold.toPath()) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + list.remove(diff); } /** * Adds a difficulty by creating a directory and required files * @param text: the name of the directory and default title */ - public void add(String text) - { + public void add(String text) throws IOException { File diffDir = new File(level.dir, text); - diffDir.mkdirs(); - Difficulty temp = new Difficulty(diffDir,level); - temp.title = text; - list.add(temp); - list.sort(Comparator.naturalOrder()); + if (diffDir.mkdirs()) { + Difficulty temp = new Difficulty(diffDir, level); + temp.title = text; + list.add(temp); + list.sort(Comparator.naturalOrder()); + } + else { + throw new IOException(); + } } public void saveOrder() { - list.forEach(d -> d.order = list.indexOf(d)); + for (Difficulty d : list) { + d.order = list.indexOf(d); + d.write(); + } } - - - - - + public List<Difficulty> getValidList() { + ObservableList<Difficulty> validList = FXCollections.observableArrayList(); + for (Difficulty difficulty : list) { + if (difficulty.isValid()) { + validList.add(difficulty); + } + } + return validList; + } } diff --git a/src/main/java/net/sowgro/npehero/main/Difficulty.java b/src/main/java/net/sowgro/npehero/main/Difficulty.java index c80e650..0308fac 100755 --- a/src/main/java/net/sowgro/npehero/main/Difficulty.java +++ b/src/main/java/net/sowgro/npehero/main/Difficulty.java @@ -9,6 +9,7 @@ public class Difficulty implements Comparable<Difficulty> { public File thisDir; public Level level; + private final JSONFile metadataYaml; public String title = "Unnamed"; public Double bpm = 0.0; @@ -18,10 +19,6 @@ public class Difficulty implements Comparable<Difficulty> public Leaderboard leaderboard; public Notes notes; - public boolean isValid = true; - - private final JSONFile metadataYaml; - /** * Creates a new Difficulty and gives it a file path * @param newDir: The file path of the Difficulty @@ -30,6 +27,10 @@ public class Difficulty implements Comparable<Difficulty> thisDir = newDir; this.level = level; metadataYaml = new JSONFile(new File(thisDir, "metadata.json")); + notes = new Notes(new File(thisDir, "notes.txt"), this); + leaderboard = new Leaderboard(new File(thisDir, "leaderboard.json")); + + readData(); } public void readData() { @@ -51,21 +52,10 @@ public class Difficulty implements Comparable<Difficulty> } order = metadataYaml.getInt("priority", order); - notes = new Notes(new File(thisDir, "notes.txt"), this); - leaderboard = new Leaderboard(new File(thisDir, "leaderboard.json")); - - validate(); - } - public void validate() { - if (notes.list.isEmpty()) { - isValid = false; - } - -// if (numBeats == 0) { -// isValid = false; -// } + public boolean isValid() { + return !notes.list.isEmpty(); } /** diff --git a/src/main/java/net/sowgro/npehero/main/Level.java b/src/main/java/net/sowgro/npehero/main/Level.java index 1b10f3d..6f66893 100755 --- a/src/main/java/net/sowgro/npehero/main/Level.java +++ b/src/main/java/net/sowgro/npehero/main/Level.java @@ -10,8 +10,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; -public class Level -{ +public class Level implements Comparable<Level>{ + public File dir; public String title = "Unnamed"; @@ -25,12 +25,10 @@ public class Level public Difficulties difficulties; - public boolean isValid = true; - private JSONFile metadataJson; /** - * Creates a new level and gives it a file path + * Creates a new level * @param newDir: The path of the Level */ public Level(File newDir) @@ -42,14 +40,16 @@ public class Level readData(); } + /** + * Check for a song file, background file and preview image file + * Parse metadata.json + */ public void readData() { var fileList = dir.listFiles(); if (fileList == null) { return; } - - // support any file extension (maybe a bad idea) for (File file : fileList) { String fileName = file.getName(); if (fileName.contains("song")) { @@ -63,26 +63,6 @@ public class Level } } - parseMetadata(); - validate(); - } - - public void validate() { - if (song == null) { - System.err.println(dir +" is missing song file"); - } - - if (difficulties.validList.isEmpty()) { - System.err.println(dir +" contains no valid difficulties"); - isValid = false; - } - } - - /** - * Reads in json metadata and assigns values to variables - */ - public void parseMetadata() - { try { metadataJson.read(); } @@ -100,6 +80,24 @@ public class Level } /** + * Checks if the level is valid. + * A valid level has a song file and 1 or more valid difficulties + * @return true if the level is valid + */ + public boolean isValid() { + if (song == null) { +// System.out.println(dir +" is missing song file"); + return false; + } + + if (difficulties.getValidList().isEmpty()) { +// System.out.println(dir +" contains no valid difficulties"); + return false; + } + return true; + } + + /** * Writes metadata to json file */ public void writeMetadata() @@ -125,14 +123,15 @@ public class Level * Copies a file into the level directory * @param newFile: the file to be copied * @param name: the new file name + * @throws IOException if there was an error adding the file */ - public void addFile(File newFile, String name) - { - try { - Files.copy(newFile.toPath(), new File(dir, name).toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - e.printStackTrace(); - } + public void addFile(File newFile, String name) throws IOException { + Files.copy(newFile.toPath(), new File(dir, name).toPath(), StandardCopyOption.REPLACE_EXISTING); readData(); } + + @Override + public int compareTo(Level other) { + return title.compareTo(other.title); + } } diff --git a/src/main/java/net/sowgro/npehero/main/Levels.java b/src/main/java/net/sowgro/npehero/main/Levels.java index fff7387..ce720c6 100755 --- a/src/main/java/net/sowgro/npehero/main/Levels.java +++ b/src/main/java/net/sowgro/npehero/main/Levels.java @@ -1,10 +1,12 @@ package net.sowgro.npehero.main; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; +import java.util.List; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; @@ -14,61 +16,73 @@ public class Levels { private static final File dir = new File("levels"); public static ObservableList<Level> list = FXCollections.observableArrayList(); - public static ObservableList<Level> validList = FXCollections.observableArrayList(); - static { - list.addListener((ListChangeListener<? super Level>) e -> { - validList.clear(); - for (Level level : list) { - if (level.isValid) { - validList.add(level); - } - } - }); - } /** - * Reads contents of folder and creates cooresponding levels + * Reads contents of the levels folder and creates a level form each subfolder + * All subfolders in the levels folder are assumed to be levels + * @throws FileNotFoundException when the levels folder is not present */ - public static void readData() + public static void readData() throws FileNotFoundException { - for (File cur: dir.listFiles()) //iterates through all files/folders in levels + list.clear(); + File[] fileList = dir.listFiles(); + if (fileList == null) { + throw new FileNotFoundException(); + } + for (File file: fileList) { - Level level = new Level(cur); + Level level = new Level(file); level.readData(); list.add(level); } + list.sort(Comparator.naturalOrder()); } /** - * Adds a level to the list by creating a directory and required files + * 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) + public static void add(String text) throws IOException { File levelDir = new File(dir,text); - levelDir.mkdirs(); - Level temp = new Level(levelDir); - temp.title = text; - list.add(temp); + 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 was an error deleting the level */ - public static void remove(Level level) + public static void remove(Level level) throws IOException { File hold = level.dir; list.remove(level); - try { - Files.walk(hold.toPath()) + // delete files recursively + // TODO clean this up + Files.walk(hold.toPath()) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete); - } catch (IOException e) { - e.printStackTrace(); - } + list.remove(level); } + + 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 diff --git a/src/main/java/net/sowgro/npehero/main/Settings.java b/src/main/java/net/sowgro/npehero/main/Settings.java index cd3448c..8cba8f7 100755 --- a/src/main/java/net/sowgro/npehero/main/Settings.java +++ b/src/main/java/net/sowgro/npehero/main/Settings.java @@ -17,14 +17,8 @@ public class Settings /** * Reads json data from settings.json */ - public static void read() - { - try { - jsonFile.read(); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Error reading settings.json"); - } + public static void read() throws Exception { + jsonFile.read(); effectsVol.set(jsonFile.getDouble("effectsVol", 1)); musicVol.set(jsonFile.getDouble("musicVol", 1)); enableMenuMusic.set(jsonFile.getBoolean("enableMenuMusic", true)); @@ -33,16 +27,10 @@ public class Settings /** * Writes json data to settings.json */ - public static void save() - { + public static void save() throws IOException { jsonFile.set("effectsVol", effectsVol.get()); jsonFile.set("musicVol", musicVol.get()); jsonFile.set("enableMenuMusic", enableMenuMusic.get()); - try { - jsonFile.write(); - } - catch (IOException e) { - System.out.println("Error writing settings.json"); - } + jsonFile.write(); } } |