Edited LimboDecay

This commit is contained in:
Waterpicker 2021-10-09 05:52:16 -05:00
parent f607c47bb4
commit fa202dfc1e

View file

@ -2,7 +2,11 @@ package org.dimdev.dimdoors.world.decay;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Function; import java.util.function.Function;
@ -84,7 +88,7 @@ public final class LimboDecay {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create(); private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create();
private static final DecayLoader INSTANCE = new DecayLoader(); private static final DecayLoader INSTANCE = new DecayLoader();
private SimpleTree<String, DecayPattern> patterns = new SimpleTree<>(String.class); private List<DecayPattern> patterns = new ArrayList<>();
private DecayLoader() { private DecayLoader() {
} }
@ -96,7 +100,7 @@ public final class LimboDecay {
@Override @Override
public void reload(ResourceManager manager) { public void reload(ResourceManager manager) {
patterns.clear(); patterns.clear();
CompletableFuture<SimpleTree<String, DecayPattern>> futurePatternMap = loadResourcePathFromJsonToTree(manager, "decay_patterns", this::loadPattern); CompletableFuture<List<DecayPattern>> futurePatternMap = loadResourcePathFromJsonToTree(manager, "decay_patterns", this::loadPattern);
patterns = futurePatternMap.join(); patterns = futurePatternMap.join();
} }
@ -104,14 +108,11 @@ public final class LimboDecay {
return DecayPattern.deserialize(object); return DecayPattern.deserialize(object);
} }
private <T> CompletableFuture<SimpleTree<String, T>> loadResourcePathFromJsonToTree(ResourceManager manager, String startingPath, Function<JsonObject, T> reader) { private <T> CompletableFuture<List<T>> loadResourcePathFromJsonToTree(ResourceManager manager, String startingPath, Function<JsonObject, T> reader) {
int sub = startingPath.endsWith("/") ? 0 : 1;
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(".json")); Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(".json"));
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
SimpleTree<String, T> tree = new SimpleTree<>(String.class); List<T> tree = new ArrayList<>();
tree.putAll(ids.parallelStream().unordered().collect(Collectors.toConcurrentMap( ids.parallelStream().unordered().<T>map(
id -> Path.stringPath(id.getNamespace() + ":" + id.getPath().substring(0, id.getPath().lastIndexOf(".")).substring(startingPath.length() + sub)),
id -> { id -> {
try { try {
JsonElement json = GSON.fromJson(new InputStreamReader(manager.getResource(id).getInputStream()), JsonElement.class); JsonElement json = GSON.fromJson(new InputStreamReader(manager.getResource(id).getInputStream()), JsonElement.class);
@ -119,13 +120,13 @@ public final class LimboDecay {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Error loading resource: " + id); throw new RuntimeException("Error loading resource: " + id);
} }
}))); }).forEach(tree::add);
return tree; return tree;
}); });
} }
public @NotNull Collection<DecayPattern> getPatterns() { public @NotNull Collection<DecayPattern> getPatterns() {
return patterns.values(); return patterns;
} }
@Override @Override