Added option for persistent resources like configuration files

This commit is contained in:
Elias Müller 2023-11-11 22:45:23 +01:00
parent c6ecbe7548
commit 541eb1e733
2 changed files with 9 additions and 4 deletions

View File

@ -11,7 +11,7 @@ import java.util.logging.Logger;
* Predefined resources which are extracted on Runtime
*/
public enum Resource {
CONFIG("config.yml"),
CONFIG("config.yml", true),
HUB_MAP("maps/hub"),
LOBBY_MAP("maps/lobby"),
@ -20,18 +20,22 @@ public enum Resource {
private final Path path;
private final String name;
Resource(String name) {
Resource(String name, boolean keepOutdated) {
this.name = name;
this.path = Path.of("resources/" + name);
try {
Logger.getLogger("ressource").info("extracting resource " + name + " ... ");
ResourceUtils.extractResource(name);
ResourceUtils.extractResource(name, keepOutdated);
} catch (URISyntaxException | IOException e) {
throw new RuntimeException(e);
}
}
Resource(String name) {
this(name, false);
}
public Path getPath() {
return path;
}

View File

@ -14,7 +14,7 @@ import java.util.stream.Stream;
* Class from the Minestom Arena example
*/
public final class ResourceUtils {
public static void extractResource(String source) throws URISyntaxException, IOException {
public static void extractResource(String source, boolean keepOutdated) throws URISyntaxException, IOException {
final URI uri = Objects.requireNonNull(ResourceUtils.class.getResource("/" + source)).toURI();
FileSystem fileSystem = null;
@ -27,6 +27,7 @@ public final class ResourceUtils {
final Path jarPath = Paths.get(uri);
final Path target = Path.of("resources/" + source);
if (Files.exists(target)) {
if(keepOutdated) return;
try (Stream<Path> pathStream = Files.walk(target)) {
pathStream.sorted(Comparator.reverseOrder())
.forEach(path -> {