Merge remote-tracking branch 'origin/1.17' into 1.17
This commit is contained in:
commit
f43263c751
7 changed files with 144 additions and 135 deletions
|
@ -4,6 +4,7 @@ import java.util.Objects;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import net.fabricmc.fabric.api.tag.TagRegistry;
|
||||||
import net.minecraft.advancement.criterion.AbstractCriterion;
|
import net.minecraft.advancement.criterion.AbstractCriterion;
|
||||||
import net.minecraft.advancement.criterion.AbstractCriterionConditions;
|
import net.minecraft.advancement.criterion.AbstractCriterionConditions;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
|
@ -14,14 +15,12 @@ import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.tag.Tag;
|
import net.minecraft.tag.Tag;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.tag.TagFactory;
|
|
||||||
|
|
||||||
public class TagBlockBreakCriteria extends AbstractCriterion<TagBlockBreakCriteria.Conditions> {
|
public class TagBlockBreakCriteria extends AbstractCriterion<TagBlockBreakCriteria.Conditions> {
|
||||||
public static final Identifier ID = new Identifier("dimdoors", "tag_block_break");
|
public static final Identifier ID = new Identifier("dimdoors", "tag_block_break");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Conditions conditionsFromJson(JsonObject obj, EntityPredicate.Extended playerPredicate, AdvancementEntityPredicateDeserializer predicateDeserializer) {
|
protected Conditions conditionsFromJson(JsonObject obj, EntityPredicate.Extended playerPredicate, AdvancementEntityPredicateDeserializer predicateDeserializer) {
|
||||||
return new Conditions(playerPredicate, TagFactory.BLOCK.create(Identifier.tryParse(obj.get("tag").getAsString())));
|
return new Conditions(playerPredicate, TagRegistry.block(Identifier.tryParse(obj.get("tag").getAsString())));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void trigger(ServerPlayerEntity player, Block block) {
|
public void trigger(ServerPlayerEntity player, Block block) {
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
package org.dimdev.dimdoors.pockets;
|
package org.dimdev.dimdoors.pockets;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.BiFunction;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.google.gson.*;
|
|
||||||
import com.mojang.serialization.JsonOps;
|
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
|
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
|
||||||
import net.minecraft.nbt.*;
|
import net.minecraft.nbt.*;
|
||||||
import net.minecraft.resource.ResourceManager;
|
import net.minecraft.resource.ResourceManager;
|
||||||
|
@ -24,11 +17,11 @@ import org.dimdev.dimdoors.api.util.SimpleTree;
|
||||||
import org.dimdev.dimdoors.pockets.generator.PocketGenerator;
|
import org.dimdev.dimdoors.pockets.generator.PocketGenerator;
|
||||||
import org.dimdev.dimdoors.pockets.virtual.VirtualPocket;
|
import org.dimdev.dimdoors.pockets.virtual.VirtualPocket;
|
||||||
import org.dimdev.dimdoors.api.util.WeightedList;
|
import org.dimdev.dimdoors.api.util.WeightedList;
|
||||||
|
import org.dimdev.dimdoors.util.ResourceUtil;
|
||||||
import org.dimdev.dimdoors.util.schematic.Schematic;
|
import org.dimdev.dimdoors.util.schematic.Schematic;
|
||||||
|
|
||||||
public class PocketLoader implements SimpleSynchronousResourceReloadListener {
|
public class PocketLoader implements SimpleSynchronousResourceReloadListener {
|
||||||
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 PocketLoader INSTANCE = new PocketLoader();
|
private static final PocketLoader INSTANCE = new PocketLoader();
|
||||||
private SimpleTree<String, PocketGenerator> pocketGenerators = new SimpleTree<>(String.class);
|
private SimpleTree<String, PocketGenerator> pocketGenerators = new SimpleTree<>(String.class);
|
||||||
private SimpleTree<String, VirtualPocket> pocketGroups = new SimpleTree<>(String.class);
|
private SimpleTree<String, VirtualPocket> pocketGroups = new SimpleTree<>(String.class);
|
||||||
|
@ -47,12 +40,12 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
|
||||||
templates.clear();
|
templates.clear();
|
||||||
dataTree.clear();
|
dataTree.clear();
|
||||||
|
|
||||||
dataTree = loadResourcePathFromJsonToTree(manager, "pockets/json", t -> t).join();
|
dataTree = ResourceUtil.loadResourcePathToMap(manager, "pockets/json", ".json", new SimpleTree<>(String.class), ResourceUtil.NBT_READER.composeIdentity(), ResourceUtil.PATH_KEY_PROVIDER).join();
|
||||||
|
|
||||||
CompletableFuture<SimpleTree<String, PocketGenerator>> futurePocketGeneratorMap = loadResourcePathFromJsonToTree(manager, "pockets/generators", this::loadPocketGenerator);
|
CompletableFuture<SimpleTree<String, PocketGenerator>> futurePocketGeneratorMap = ResourceUtil.loadResourcePathToMap(manager, "pockets/generators", ".json", new SimpleTree<>(String.class), ResourceUtil.NBT_READER.andThenReader(this::loadPocketGenerator), ResourceUtil.PATH_KEY_PROVIDER);
|
||||||
CompletableFuture<SimpleTree<String, VirtualPocket>> futurePocketGroups = loadResourcePathFromJsonToTree(manager, "pockets/groups", this::loadVirtualPocket);
|
CompletableFuture<SimpleTree<String, VirtualPocket>> futurePocketGroups = ResourceUtil.loadResourcePathToMap(manager, "pockets/groups", ".json", new SimpleTree<>(String.class), ResourceUtil.NBT_READER.andThenReader(this::loadVirtualPocket), ResourceUtil.PATH_KEY_PROVIDER);
|
||||||
CompletableFuture<SimpleTree<String, VirtualPocket>> futureVirtualPockets = loadResourcePathFromJsonToTree(manager, "pockets/virtual", this::loadVirtualPocket);
|
CompletableFuture<SimpleTree<String, VirtualPocket>> futureVirtualPockets = ResourceUtil.loadResourcePathToMap(manager, "pockets/virtual", ".json", new SimpleTree<>(String.class), ResourceUtil.NBT_READER.andThenReader(this::loadVirtualPocket), ResourceUtil.PATH_KEY_PROVIDER);
|
||||||
CompletableFuture<SimpleTree<String, PocketTemplate>> futureTemplates = loadResourcePathFromCompressedNbtToTree(manager, "pockets/schematic", ".schem", this::loadPocketTemplate);
|
CompletableFuture<SimpleTree<String, PocketTemplate>> futureTemplates = ResourceUtil.loadResourcePathToMap(manager, "pockets/schematic", ".schem", new SimpleTree<>(String.class), ResourceUtil.COMPRESSED_NBT_READER.andThenReader(this::loadPocketTemplate), ResourceUtil.PATH_KEY_PROVIDER);
|
||||||
|
|
||||||
|
|
||||||
pocketGenerators = futurePocketGeneratorMap.join();
|
pocketGenerators = futurePocketGeneratorMap.join();
|
||||||
|
@ -64,45 +57,6 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
|
||||||
virtualPockets.values().forEach(VirtualPocket::init);
|
virtualPockets.values().forEach(VirtualPocket::init);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> CompletableFuture<SimpleTree<String, T>> loadResourcePathFromJsonToTree(ResourceManager manager, String startingPath, Function<NbtElement, T> reader) {
|
|
||||||
int sub = startingPath.endsWith("/") ? 0 : 1;
|
|
||||||
|
|
||||||
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(".json"));
|
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
|
||||||
SimpleTree<String, T> tree = new SimpleTree<>(String.class);
|
|
||||||
tree.putAll(ids.parallelStream().unordered().collect(Collectors.toConcurrentMap(
|
|
||||||
id -> Path.stringPath(id.getNamespace() + ":" + id.getPath().substring(0, id.getPath().lastIndexOf(".")).substring(startingPath.length() + sub)),
|
|
||||||
id -> {
|
|
||||||
try {
|
|
||||||
JsonElement json = GSON.fromJson(new InputStreamReader(manager.getResource(id).getInputStream()), JsonElement.class);
|
|
||||||
return reader.apply(JsonOps.INSTANCE.convertTo(NbtOps.INSTANCE, json));
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Error loading resource: " + id);
|
|
||||||
}
|
|
||||||
})));
|
|
||||||
return tree;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> CompletableFuture<SimpleTree<String, T>> loadResourcePathFromCompressedNbtToTree(ResourceManager manager, String startingPath, String extension, BiFunction<NbtCompound, String, T> reader) {
|
|
||||||
int sub = startingPath.endsWith("/") ? 0 : 1;
|
|
||||||
Function<Identifier, Path<String>> normalizer = id -> Path.stringPath(id.getNamespace() + ":" + id.getPath().substring(0, id.getPath().lastIndexOf(".")).substring(startingPath.length() + sub));
|
|
||||||
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(extension));
|
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
|
||||||
SimpleTree<String, T> tree = new SimpleTree<>(String.class);
|
|
||||||
tree.putAll(ids.parallelStream().unordered().collect(Collectors.toConcurrentMap(
|
|
||||||
normalizer,
|
|
||||||
id -> {
|
|
||||||
try {
|
|
||||||
return reader.apply(NbtIo.readCompressed(manager.getResource(id).getInputStream()), normalizer.apply(id).reduce(String::concat).get());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Error loading resource: " + id);
|
|
||||||
}
|
|
||||||
})));
|
|
||||||
return tree;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// public void load() {
|
// public void load() {
|
||||||
// long startTime = System.currentTimeMillis();
|
// long startTime = System.currentTimeMillis();
|
||||||
//
|
//
|
||||||
|
@ -132,17 +86,17 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
|
||||||
return NbtUtil.asNbtCompound(getDataNbt(id), "Could not convert NbtElement \"" + id + "\" to NbtCompound!");
|
return NbtUtil.asNbtCompound(getDataNbt(id), "Could not convert NbtElement \"" + id + "\" to NbtCompound!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private VirtualPocket loadVirtualPocket(NbtElement nbt) {
|
private VirtualPocket loadVirtualPocket(NbtElement nbt, Path<String> ignore) {
|
||||||
return VirtualPocket.deserialize(nbt);
|
return VirtualPocket.deserialize(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
private PocketGenerator loadPocketGenerator(NbtElement nbt) {
|
private PocketGenerator loadPocketGenerator(NbtElement nbt, Path<String> ignore) {
|
||||||
return PocketGenerator.deserialize(NbtUtil.asNbtCompound(nbt, "Could not load PocketGenerator since its json does not represent an NbtCompound!"));
|
return PocketGenerator.deserialize(NbtUtil.asNbtCompound(nbt, "Could not load PocketGenerator since its json does not represent an NbtCompound!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private PocketTemplate loadPocketTemplate(NbtCompound nbt, String id) {
|
private PocketTemplate loadPocketTemplate(NbtCompound nbt, Path<String> id) {
|
||||||
try {
|
try {
|
||||||
return new PocketTemplate(Schematic.fromNbt(nbt), new Identifier(id));
|
return new PocketTemplate(Schematic.fromNbt(nbt), new Identifier(id.reduce(String::concat).orElseThrow()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Error loading " + nbt.toString(), e);
|
throw new RuntimeException("Error loading " + nbt.toString(), e);
|
||||||
}
|
}
|
||||||
|
|
85
src/main/java/org/dimdev/dimdoors/util/ResourceUtil.java
Normal file
85
src/main/java/org/dimdev/dimdoors/util/ResourceUtil.java
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
package org.dimdev.dimdoors.util;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.mojang.serialization.JsonOps;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
|
import net.minecraft.nbt.NbtElement;
|
||||||
|
import net.minecraft.nbt.NbtIo;
|
||||||
|
import net.minecraft.nbt.NbtOps;
|
||||||
|
import net.minecraft.resource.ResourceManager;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import org.dimdev.dimdoors.api.util.Path;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ResourceUtil {
|
||||||
|
private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create();
|
||||||
|
|
||||||
|
public static final BiFunction<String, Identifier, Path<String>> PATH_KEY_PROVIDER = (startingPath, id) -> Path.stringPath(id.getNamespace() + ":" + id.getPath().substring(0, id.getPath().lastIndexOf(".")).substring(startingPath.length() + (startingPath.endsWith("/") ? 0 : 1)));
|
||||||
|
|
||||||
|
public static final ComposableFunction<JsonElement, NbtElement> JSON_TO_NBT = json -> JsonOps.INSTANCE.convertTo(NbtOps.INSTANCE, json);
|
||||||
|
|
||||||
|
public static final ComposableFunction<InputStream, JsonElement> JSON_READER = inputStream -> GSON.fromJson(new InputStreamReader(inputStream), JsonElement.class);
|
||||||
|
public static final ComposableFunction<InputStream, NbtElement> NBT_READER = JSON_READER.andThenComposable(JSON_TO_NBT);
|
||||||
|
public static final ComposableFunction<InputStream, NbtCompound> COMPRESSED_NBT_READER = inputStream -> {
|
||||||
|
try {
|
||||||
|
return NbtIo.readCompressed(inputStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static <K, T, M extends Map<K, T>> CompletableFuture<M> loadResourcePathToMap(ResourceManager manager, String startingPath, String extension, M map, BiFunction<InputStream, K, T> reader, BiFunction<String, Identifier, K> keyProvider) {
|
||||||
|
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(extension));
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
map.putAll(ids.parallelStream().unordered().collect(Collectors.toConcurrentMap(
|
||||||
|
id -> keyProvider.apply(startingPath, id),
|
||||||
|
id -> {
|
||||||
|
try {
|
||||||
|
return reader.apply(manager.getResource(id).getInputStream(), keyProvider.apply(startingPath, id));
|
||||||
|
} catch (IOException | RuntimeException e) {
|
||||||
|
throw new RuntimeException("Error loading resource: " + id);
|
||||||
|
}
|
||||||
|
})));
|
||||||
|
return map;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T, M extends Collection<T>> CompletableFuture<M> loadResourcePathToCollection(ResourceManager manager, String startingPath, String extension, M collection, BiFunction<InputStream, Identifier, T> reader) {
|
||||||
|
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(extension));
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
collection.addAll(ids.parallelStream().unordered().map(id -> {
|
||||||
|
try {
|
||||||
|
return reader.apply(manager.getResource(id).getInputStream(), id);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Error loading resource: " + id);
|
||||||
|
}
|
||||||
|
}).collect(Collectors.toList())); // TODO: change this to smthn concurrent
|
||||||
|
return collection;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ComposableFunction<T, R> extends Function<T, R> {
|
||||||
|
default <K, V> BiFunction<T, K, V> andThenReader(BiFunction<R, K, V> function) {
|
||||||
|
return (t, k) -> function.apply(apply(t), k);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <K> BiFunction<T, K, R> composeIdentity() {
|
||||||
|
return (t, k) -> apply(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
default <V> ComposableFunction<T, V> andThenComposable(Function<? super R, ? extends V> after) {
|
||||||
|
return (T t) -> after.apply(apply(t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,7 @@
|
||||||
package org.dimdev.dimdoors.world.decay;
|
package org.dimdev.dimdoors.world.decay;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
@ -24,9 +23,9 @@ public class DecayPattern {
|
||||||
this.processor = processor;
|
this.processor = processor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DecayPattern deserialize(JsonObject nbt) {
|
public static DecayPattern deserialize(NbtCompound nbt) {
|
||||||
DecayPredicate predicate = DecayPredicate.deserialize(nbt.getAsJsonObject("predicate"));
|
DecayPredicate predicate = DecayPredicate.deserialize(nbt.getCompound("predicate"));
|
||||||
DecayProcessor processor = DecayProcessor.deserialize(nbt.getAsJsonObject("processor"));
|
DecayProcessor processor = DecayProcessor.deserialize(nbt.getCompound("processor"));
|
||||||
return DecayPattern.builder().predicate(predicate).processor(processor).create();
|
return DecayPattern.builder().predicate(predicate).processor(processor).create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ package org.dimdev.dimdoors.world.decay;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.mojang.serialization.Lifecycle;
|
import com.mojang.serialization.Lifecycle;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
|
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
|
||||||
import org.dimdev.dimdoors.world.decay.predicates.SimpleDecayPredicate;
|
import org.dimdev.dimdoors.world.decay.predicates.SimpleDecayPredicate;
|
||||||
import org.dimdev.dimdoors.world.decay.processors.SimpleDecayProcesor;
|
import org.dimdev.dimdoors.world.decay.processors.SimpleDecayProcesor;
|
||||||
|
@ -25,7 +25,7 @@ public interface DecayPredicate {
|
||||||
private static final String ID = "none";
|
private static final String ID = "none";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DecayPredicate fromJson(JsonObject json) {
|
public DecayPredicate fromNbt(NbtCompound nbt) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,20 +45,20 @@ public interface DecayPredicate {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static DecayPredicate deserialize(JsonObject nbt) {
|
static DecayPredicate deserialize(NbtCompound nbt) {
|
||||||
Identifier id = Identifier.tryParse(nbt.get("type").getAsString());
|
Identifier id = Identifier.tryParse(nbt.getString("type"));
|
||||||
return REGISTRY.getOrEmpty(id).orElse(DecayPredicateType.NONE_PREDICATE_TYPE).fromJson(nbt);
|
return REGISTRY.getOrEmpty(id).orElse(DecayPredicateType.NONE_PREDICATE_TYPE).fromNbt(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static JsonObject serialize(DecayPredicate modifier) {
|
static NbtCompound serialize(DecayPredicate modifier) {
|
||||||
return modifier.toJson(new JsonObject());
|
return modifier.toNbt(new NbtCompound());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DecayPredicate fromJson(JsonObject json);
|
DecayPredicate fromNbt(NbtCompound nbt);
|
||||||
|
|
||||||
default JsonObject toJson(JsonObject json) {
|
default NbtCompound toNbt(NbtCompound nbt) {
|
||||||
return this.getType().toJson(json);
|
return this.getType().toNbt(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
DecayPredicate.DecayPredicateType<? extends DecayPredicate> getType();
|
DecayPredicate.DecayPredicateType<? extends DecayPredicate> getType();
|
||||||
|
@ -71,9 +71,9 @@ public interface DecayPredicate {
|
||||||
DecayPredicateType<DecayPredicate> NONE_PREDICATE_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY);
|
DecayPredicateType<DecayPredicate> NONE_PREDICATE_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY);
|
||||||
DecayPredicateType<SimpleDecayPredicate> SIMPLE_PREDICATE_TYPE = register(new Identifier("dimdoors", SimpleDecayProcesor.KEY), SimpleDecayPredicate::new);
|
DecayPredicateType<SimpleDecayPredicate> SIMPLE_PREDICATE_TYPE = register(new Identifier("dimdoors", SimpleDecayProcesor.KEY), SimpleDecayPredicate::new);
|
||||||
|
|
||||||
DecayPredicate fromJson(JsonObject nbt);
|
DecayPredicate fromNbt(NbtCompound nbt);
|
||||||
|
|
||||||
JsonObject toJson(JsonObject nbt);
|
NbtCompound toNbt(NbtCompound nbt);
|
||||||
|
|
||||||
static void register() {
|
static void register() {
|
||||||
DimensionalDoorsInitializer.apiSubscribers.forEach(d -> d.registerDecayPredicates(REGISTRY));
|
DimensionalDoorsInitializer.apiSubscribers.forEach(d -> d.registerDecayPredicates(REGISTRY));
|
||||||
|
@ -82,14 +82,14 @@ public interface DecayPredicate {
|
||||||
static <U extends DecayPredicate> DecayPredicateType<U> register(Identifier id, Supplier<U> factory) {
|
static <U extends DecayPredicate> DecayPredicateType<U> register(Identifier id, Supplier<U> factory) {
|
||||||
return Registry.register(REGISTRY, id, new DecayPredicateType<U>() {
|
return Registry.register(REGISTRY, id, new DecayPredicateType<U>() {
|
||||||
@Override
|
@Override
|
||||||
public DecayPredicate fromJson(JsonObject json) {
|
public DecayPredicate fromNbt(NbtCompound nbt) {
|
||||||
return factory.get().fromJson(json);
|
return factory.get().fromNbt(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonObject toJson(JsonObject json) {
|
public NbtCompound toNbt(NbtCompound nbt) {
|
||||||
json.addProperty("type", id.toString());
|
nbt.putString("type", id.toString());
|
||||||
return json;
|
return nbt;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package org.dimdev.dimdoors.world.decay;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.mojang.serialization.Lifecycle;
|
import com.mojang.serialization.Lifecycle;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
|
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
|
||||||
import org.dimdev.dimdoors.world.decay.processors.SelfDecayProcessor;
|
import org.dimdev.dimdoors.world.decay.processors.SelfDecayProcessor;
|
||||||
import org.dimdev.dimdoors.world.decay.processors.SimpleDecayProcesor;
|
import org.dimdev.dimdoors.world.decay.processors.SimpleDecayProcesor;
|
||||||
|
@ -23,7 +23,7 @@ public interface DecayProcessor {
|
||||||
|
|
||||||
DecayProcessor DUMMY = new DecayProcessor() {
|
DecayProcessor DUMMY = new DecayProcessor() {
|
||||||
@Override
|
@Override
|
||||||
public DecayProcessor fromJson(JsonObject json) {
|
public DecayProcessor fromNbt(NbtCompound nbt) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,20 +45,20 @@ public interface DecayProcessor {
|
||||||
private static final String ID = "none";
|
private static final String ID = "none";
|
||||||
};
|
};
|
||||||
|
|
||||||
static DecayProcessor deserialize(JsonObject nbt) {
|
static DecayProcessor deserialize(NbtCompound nbt) {
|
||||||
Identifier id = Identifier.tryParse(nbt.get("type").getAsString());
|
Identifier id = Identifier.tryParse(nbt.getString("type"));
|
||||||
return REGISTRY.getOrEmpty(id).orElse(DecayProcessorType.NONE_PROCESSOR_TYPE).fromJson(nbt);
|
return REGISTRY.getOrEmpty(id).orElse(DecayProcessorType.NONE_PROCESSOR_TYPE).fromNbt(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static JsonObject serialize(DecayProcessor modifier) {
|
static NbtCompound serialize(DecayProcessor modifier) {
|
||||||
return modifier.toJson(new JsonObject());
|
return modifier.toNbt(new NbtCompound());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DecayProcessor fromJson(JsonObject json);
|
DecayProcessor fromNbt(NbtCompound nbt);
|
||||||
|
|
||||||
default JsonObject toJson(JsonObject json) {
|
default NbtCompound toNbt(NbtCompound nbt) {
|
||||||
return this.getType().toJson(json);
|
return this.getType().toNbt(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
DecayProcessor.DecayProcessorType<? extends DecayProcessor> getType();
|
DecayProcessor.DecayProcessorType<? extends DecayProcessor> getType();
|
||||||
|
@ -72,9 +72,9 @@ public interface DecayProcessor {
|
||||||
DecayProcessorType<DecayProcessor> NONE_PROCESSOR_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY);
|
DecayProcessorType<DecayProcessor> NONE_PROCESSOR_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY);
|
||||||
DecayProcessorType<SelfDecayProcessor> SELF = register(new Identifier("dimdoors", SelfDecayProcessor.KEY), SelfDecayProcessor::instance);
|
DecayProcessorType<SelfDecayProcessor> SELF = register(new Identifier("dimdoors", SelfDecayProcessor.KEY), SelfDecayProcessor::instance);
|
||||||
|
|
||||||
DecayProcessor fromJson(JsonObject nbt);
|
DecayProcessor fromNbt(NbtCompound nbt);
|
||||||
|
|
||||||
JsonObject toJson(JsonObject nbt);
|
NbtCompound toNbt(NbtCompound nbt);
|
||||||
|
|
||||||
static void register() {
|
static void register() {
|
||||||
DimensionalDoorsInitializer.apiSubscribers.forEach(d -> d.registerDecayProcessors(REGISTRY));
|
DimensionalDoorsInitializer.apiSubscribers.forEach(d -> d.registerDecayProcessors(REGISTRY));
|
||||||
|
@ -83,14 +83,14 @@ public interface DecayProcessor {
|
||||||
static <U extends DecayProcessor> DecayProcessorType<U> register(Identifier id, Supplier<U> factory) {
|
static <U extends DecayProcessor> DecayProcessorType<U> register(Identifier id, Supplier<U> factory) {
|
||||||
return Registry.register(REGISTRY, id, new DecayProcessorType<U>() {
|
return Registry.register(REGISTRY, id, new DecayProcessorType<U>() {
|
||||||
@Override
|
@Override
|
||||||
public DecayProcessor fromJson(JsonObject json) {
|
public DecayProcessor fromNbt(NbtCompound nbt) {
|
||||||
return factory.get().fromJson(json);
|
return factory.get().fromNbt(nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonObject toJson(JsonObject json) {
|
public NbtCompound toNbt(NbtCompound nbt) {
|
||||||
json.addProperty("type", id.toString());
|
nbt.putString("type", id.toString());
|
||||||
return json;
|
return nbt;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,22 @@
|
||||||
package org.dimdev.dimdoors.world.decay;
|
package org.dimdev.dimdoors.world.decay;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
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.stream.Collectors;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
|
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.nbt.NbtCompound;
|
||||||
|
import net.minecraft.nbt.NbtElement;
|
||||||
import net.minecraft.resource.ResourceManager;
|
import net.minecraft.resource.ResourceManager;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
|
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
|
||||||
import org.dimdev.dimdoors.api.util.Path;
|
import org.dimdev.dimdoors.util.ResourceUtil;
|
||||||
import org.dimdev.dimdoors.api.util.SimpleTree;
|
|
||||||
import org.dimdev.dimdoors.world.decay.DecayPattern;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
@ -77,7 +66,7 @@ public final class LimboDecay {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DecayLoader implements SimpleSynchronousResourceReloadListener {
|
public static class DecayLoader implements SimpleSynchronousResourceReloadListener {
|
||||||
private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
private static final DecayLoader INSTANCE = new DecayLoader();
|
private static final DecayLoader INSTANCE = new DecayLoader();
|
||||||
private List<DecayPattern> patterns = new ArrayList<>();
|
private List<DecayPattern> patterns = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -91,29 +80,12 @@ public final class LimboDecay {
|
||||||
@Override
|
@Override
|
||||||
public void reload(ResourceManager manager) {
|
public void reload(ResourceManager manager) {
|
||||||
patterns.clear();
|
patterns.clear();
|
||||||
CompletableFuture<List<DecayPattern>> futurePatternMap = loadResourcePathFromJsonToTree(manager, "decay_patterns", this::loadPattern);
|
CompletableFuture<List<DecayPattern>> futurePatternMap = ResourceUtil.loadResourcePathToCollection(manager, "decay_patterns", ".json", new ArrayList<>(), ResourceUtil.NBT_READER.andThenReader(this::loadPattern));
|
||||||
patterns = futurePatternMap.join();
|
patterns = futurePatternMap.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
private DecayPattern loadPattern(JsonObject object) {
|
private DecayPattern loadPattern(NbtElement nbt, Identifier ignored) {
|
||||||
return DecayPattern.deserialize(object);
|
return DecayPattern.deserialize((NbtCompound) nbt);
|
||||||
}
|
|
||||||
|
|
||||||
private <T> CompletableFuture<List<T>> loadResourcePathFromJsonToTree(ResourceManager manager, String startingPath, Function<JsonObject, T> reader) {
|
|
||||||
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(".json"));
|
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
|
||||||
List<T> tree = new ArrayList<>();
|
|
||||||
ids.parallelStream().unordered().map(
|
|
||||||
id -> {
|
|
||||||
try {
|
|
||||||
JsonElement json = GSON.fromJson(new InputStreamReader(manager.getResource(id).getInputStream()), JsonElement.class);
|
|
||||||
return reader.apply(json.getAsJsonObject());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Error loading resource: " + id);
|
|
||||||
}
|
|
||||||
}).sequential().forEach(tree::add);
|
|
||||||
return tree;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Collection<DecayPattern> getPatterns() {
|
public @NotNull Collection<DecayPattern> getPatterns() {
|
||||||
|
|
Loading…
Reference in a new issue