serialize decay patterns from/ to nbt

This commit is contained in:
CreepyCre 2021-10-11 13:25:47 +02:00
parent 623079b45f
commit 62f99f378e
4 changed files with 45 additions and 46 deletions

View file

@ -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();
} }

View file

@ -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;
} }
}); });
} }

View file

@ -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;
} }
}); });
} }

View file

@ -6,11 +6,11 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import com.google.gson.JsonElement;
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;
@ -88,12 +88,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 = ResourceUtil.loadResourcePathToCollection(manager, "decay_patterns", ".json", new ArrayList<>(), ResourceUtil.JSON_READER.andThenReader(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(JsonElement object, Identifier ignored) { private DecayPattern loadPattern(NbtElement nbt, Identifier ignored) {
return DecayPattern.deserialize(object.getAsJsonObject()); return DecayPattern.deserialize((NbtCompound) nbt);
} }
public @NotNull Collection<DecayPattern> getPatterns() { public @NotNull Collection<DecayPattern> getPatterns() {