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;
import com.google.gson.JsonObject;
import net.minecraft.block.BlockState;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@ -24,9 +23,9 @@ public class DecayPattern {
this.processor = processor;
}
public static DecayPattern deserialize(JsonObject nbt) {
DecayPredicate predicate = DecayPredicate.deserialize(nbt.getAsJsonObject("predicate"));
DecayProcessor processor = DecayProcessor.deserialize(nbt.getAsJsonObject("processor"));
public static DecayPattern deserialize(NbtCompound nbt) {
DecayPredicate predicate = DecayPredicate.deserialize(nbt.getCompound("predicate"));
DecayProcessor processor = DecayProcessor.deserialize(nbt.getCompound("processor"));
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 com.google.gson.JsonObject;
import com.mojang.serialization.Lifecycle;
import net.minecraft.nbt.NbtCompound;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.world.decay.predicates.SimpleDecayPredicate;
import org.dimdev.dimdoors.world.decay.processors.SimpleDecayProcesor;
@ -25,7 +25,7 @@ public interface DecayPredicate {
private static final String ID = "none";
@Override
public DecayPredicate fromJson(JsonObject json) {
public DecayPredicate fromNbt(NbtCompound nbt) {
return this;
}
@ -45,20 +45,20 @@ public interface DecayPredicate {
}
};
static DecayPredicate deserialize(JsonObject nbt) {
Identifier id = Identifier.tryParse(nbt.get("type").getAsString());
return REGISTRY.getOrEmpty(id).orElse(DecayPredicateType.NONE_PREDICATE_TYPE).fromJson(nbt);
static DecayPredicate deserialize(NbtCompound nbt) {
Identifier id = Identifier.tryParse(nbt.getString("type"));
return REGISTRY.getOrEmpty(id).orElse(DecayPredicateType.NONE_PREDICATE_TYPE).fromNbt(nbt);
}
static JsonObject serialize(DecayPredicate modifier) {
return modifier.toJson(new JsonObject());
static NbtCompound serialize(DecayPredicate modifier) {
return modifier.toNbt(new NbtCompound());
}
DecayPredicate fromJson(JsonObject json);
DecayPredicate fromNbt(NbtCompound nbt);
default JsonObject toJson(JsonObject json) {
return this.getType().toJson(json);
default NbtCompound toNbt(NbtCompound nbt) {
return this.getType().toNbt(nbt);
}
DecayPredicate.DecayPredicateType<? extends DecayPredicate> getType();
@ -71,9 +71,9 @@ public interface DecayPredicate {
DecayPredicateType<DecayPredicate> NONE_PREDICATE_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY);
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() {
DimensionalDoorsInitializer.apiSubscribers.forEach(d -> d.registerDecayPredicates(REGISTRY));
@ -82,16 +82,16 @@ public interface DecayPredicate {
static <U extends DecayPredicate> DecayPredicateType<U> register(Identifier id, Supplier<U> factory) {
return Registry.register(REGISTRY, id, new DecayPredicateType<U>() {
@Override
public DecayPredicate fromJson(JsonObject json) {
return factory.get().fromJson(json);
public DecayPredicate fromNbt(NbtCompound nbt) {
return factory.get().fromNbt(nbt);
}
@Override
public JsonObject toJson(JsonObject json) {
json.addProperty("type", id.toString());
return json;
public NbtCompound toNbt(NbtCompound nbt) {
nbt.putString("type", id.toString());
return nbt;
}
});
}
}
}
}

View file

@ -2,8 +2,8 @@ package org.dimdev.dimdoors.world.decay;
import java.util.function.Supplier;
import com.google.gson.JsonObject;
import com.mojang.serialization.Lifecycle;
import net.minecraft.nbt.NbtCompound;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.world.decay.processors.SelfDecayProcessor;
import org.dimdev.dimdoors.world.decay.processors.SimpleDecayProcesor;
@ -23,7 +23,7 @@ public interface DecayProcessor {
DecayProcessor DUMMY = new DecayProcessor() {
@Override
public DecayProcessor fromJson(JsonObject json) {
public DecayProcessor fromNbt(NbtCompound nbt) {
return this;
}
@ -45,20 +45,20 @@ public interface DecayProcessor {
private static final String ID = "none";
};
static DecayProcessor deserialize(JsonObject nbt) {
Identifier id = Identifier.tryParse(nbt.get("type").getAsString());
return REGISTRY.getOrEmpty(id).orElse(DecayProcessorType.NONE_PROCESSOR_TYPE).fromJson(nbt);
static DecayProcessor deserialize(NbtCompound nbt) {
Identifier id = Identifier.tryParse(nbt.getString("type"));
return REGISTRY.getOrEmpty(id).orElse(DecayProcessorType.NONE_PROCESSOR_TYPE).fromNbt(nbt);
}
static JsonObject serialize(DecayProcessor modifier) {
return modifier.toJson(new JsonObject());
static NbtCompound serialize(DecayProcessor modifier) {
return modifier.toNbt(new NbtCompound());
}
DecayProcessor fromJson(JsonObject json);
DecayProcessor fromNbt(NbtCompound nbt);
default JsonObject toJson(JsonObject json) {
return this.getType().toJson(json);
default NbtCompound toNbt(NbtCompound nbt) {
return this.getType().toNbt(nbt);
}
DecayProcessor.DecayProcessorType<? extends DecayProcessor> getType();
@ -72,9 +72,9 @@ public interface DecayProcessor {
DecayProcessorType<DecayProcessor> NONE_PROCESSOR_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY);
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() {
DimensionalDoorsInitializer.apiSubscribers.forEach(d -> d.registerDecayProcessors(REGISTRY));
@ -83,16 +83,16 @@ public interface DecayProcessor {
static <U extends DecayProcessor> DecayProcessorType<U> register(Identifier id, Supplier<U> factory) {
return Registry.register(REGISTRY, id, new DecayProcessorType<U>() {
@Override
public DecayProcessor fromJson(JsonObject json) {
return factory.get().fromJson(json);
public DecayProcessor fromNbt(NbtCompound nbt) {
return factory.get().fromNbt(nbt);
}
@Override
public JsonObject toJson(JsonObject json) {
json.addProperty("type", id.toString());
return json;
public NbtCompound toNbt(NbtCompound nbt) {
nbt.putString("type", id.toString());
return nbt;
}
});
}
}
}
}

View file

@ -6,11 +6,11 @@ import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import com.google.gson.JsonElement;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.block.BlockState;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.resource.ResourceManager;
import org.apache.logging.log4j.LogManager;
@ -88,12 +88,12 @@ public final class LimboDecay {
@Override
public void reload(ResourceManager manager) {
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();
}
private DecayPattern loadPattern(JsonElement object, Identifier ignored) {
return DecayPattern.deserialize(object.getAsJsonObject());
private DecayPattern loadPattern(NbtElement nbt, Identifier ignored) {
return DecayPattern.deserialize((NbtCompound) nbt);
}
public @NotNull Collection<DecayPattern> getPatterns() {