blob: f6251e980636bcc0c349ebb2ed81c26339a5fd25 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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(file.getName(), 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;
}
}
|