switched VirtualPockets from Codecs to fromTag/ toTag serialization
This commit is contained in:
parent
cf94db47d8
commit
0fb93068bd
7 changed files with 181 additions and 65 deletions
|
@ -3,21 +3,50 @@ package org.dimdev.dimdoors.pockets;
|
|||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
|
||||
public final class PocketGroup {
|
||||
/*
|
||||
public static final Codec<PocketGroup> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
Codec.STRING.fieldOf("group").forGetter(PocketGroup::getGroup),
|
||||
VirtualPocket.CODEC.listOf().fieldOf("pockets").forGetter(PocketGroup::getEntries)
|
||||
).apply(instance, PocketGroup::new));
|
||||
private final String group;
|
||||
private final List<VirtualPocket> entries;
|
||||
*/
|
||||
|
||||
private String group;
|
||||
private List<VirtualPocket> entries;
|
||||
|
||||
public PocketGroup() {
|
||||
}
|
||||
|
||||
public PocketGroup(String group, List<VirtualPocket> entries) {
|
||||
this.group = group;
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
public PocketGroup fromTag(CompoundTag tag) {
|
||||
this.group = tag.getString("group");
|
||||
|
||||
ListTag pockets = tag.getList("pockets", 10);
|
||||
this.entries = Lists.newArrayList();
|
||||
for (int i = 0; i < pockets.size(); i++) {
|
||||
CompoundTag pocket = pockets.getCompound(i);
|
||||
entries.add(VirtualPocket.deserialize(pocket));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompoundTag toTag(CompoundTag tag) {
|
||||
tag.putString("group", this.group);
|
||||
|
||||
ListTag pockets = new ListTag();
|
||||
entries.forEach(pocket -> pockets.add(pocket.toTag(new CompoundTag())));
|
||||
tag.put("pockets", pockets);
|
||||
return tag;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return this.group;
|
||||
}
|
||||
|
|
|
@ -11,8 +11,8 @@ import java.util.*;
|
|||
import com.google.common.collect.*;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.nbt.*;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
@ -20,9 +20,6 @@ import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
|||
import org.dimdev.dimdoors.util.WeightedList;
|
||||
import org.dimdev.dimdoors.util.schematic.v2.Schematic;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtIo;
|
||||
|
||||
public class SchematicV2Handler {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create();
|
||||
|
@ -43,17 +40,18 @@ public class SchematicV2Handler {
|
|||
long startTime = System.currentTimeMillis();
|
||||
Set<String> names = ImmutableSet.of("default_private", "default_public");
|
||||
for (String name : names) {
|
||||
try (BufferedReader reader = Files.newBufferedReader(Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/json/v2/%s.json", name)).toURI()))) {
|
||||
List<String> result = new ArrayList<>();
|
||||
while (true) {
|
||||
String line = reader.readLine();
|
||||
if (line == null) {
|
||||
break;
|
||||
}
|
||||
result.add(line);
|
||||
}
|
||||
JsonObject json = GSON.fromJson(String.join("", result), JsonObject.class);
|
||||
PocketGroup type = PocketGroup.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow(false, System.err::println).getFirst();
|
||||
try (BufferedReader reader = Files.newBufferedReader(Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/json/v2/%s.json", name)).toURI()))) {
|
||||
List<String> result = new ArrayList<>();
|
||||
while (true) {
|
||||
String line = reader.readLine();
|
||||
if (line == null) {
|
||||
break;
|
||||
}
|
||||
result.add(line);
|
||||
}
|
||||
|
||||
CompoundTag groupTag = StringNbtReader.parse(String.join("", result));
|
||||
PocketGroup type = new PocketGroup().fromTag(groupTag);
|
||||
|
||||
this.pocketGroups.add(type);
|
||||
|
||||
|
@ -64,7 +62,7 @@ public class SchematicV2Handler {
|
|||
virtualPocket.init(type.getGroup());
|
||||
weightedPockets.add(virtualPocket);
|
||||
}
|
||||
} catch (IOException | URISyntaxException e) {
|
||||
} catch (IOException | URISyntaxException | CommandSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package org.dimdev.dimdoors.pockets;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.serialization.*;
|
||||
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.registry.RegistryKey;
|
||||
|
@ -13,10 +13,13 @@ import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
|||
import org.dimdev.dimdoors.util.Weighted;
|
||||
import org.dimdev.dimdoors.world.pocket.Pocket;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
// TODO: do something about getting correct Pocket sizes
|
||||
public abstract class VirtualPocket implements Weighted<PocketGenerationParameters> {
|
||||
public static final Registry<VirtualPocketType<? extends VirtualPocket>> REGISTRY = FabricRegistryBuilder.from(new SimpleRegistry<VirtualPocketType<? extends VirtualPocket>>(RegistryKey.ofRegistry(new Identifier("dimdoors", "virtual_pocket_type")), Lifecycle.stable())).buildAndRegister();
|
||||
/*
|
||||
public static final Codec<VirtualPocket> CODEC = new Codec<VirtualPocket>() {
|
||||
@Override
|
||||
public <T> DataResult<Pair<VirtualPocket, T>> decode(DynamicOps<T> dynamicOps, T input) {
|
||||
|
@ -29,6 +32,22 @@ public abstract class VirtualPocket implements Weighted<PocketGenerationParamete
|
|||
return null; // TODO: write encode function
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
public static VirtualPocket deserialize(CompoundTag tag) {
|
||||
Identifier id = Identifier.tryParse(tag.getString("type")); // TODO: return some NONE VirtualPocket if type cannot be found or deserialization fails.
|
||||
return REGISTRY.get(id).fromTag(tag);
|
||||
}
|
||||
|
||||
public static CompoundTag serialize(VirtualPocket virtualPocket) {
|
||||
return virtualPocket.toTag(new CompoundTag());
|
||||
}
|
||||
|
||||
public abstract VirtualPocket fromTag(CompoundTag tag);
|
||||
|
||||
public CompoundTag toTag(CompoundTag tag) {
|
||||
return this.getType().toTag(tag);
|
||||
}
|
||||
|
||||
public abstract void init(String group);
|
||||
|
||||
|
@ -41,20 +60,33 @@ public abstract class VirtualPocket implements Weighted<PocketGenerationParamete
|
|||
|
||||
public abstract String getKey();
|
||||
|
||||
|
||||
public interface VirtualPocketType<T extends VirtualPocket> {
|
||||
VirtualPocketType<SchematicGenerator> SCHEMATIC = register(new Identifier("dimdoors", SchematicGenerator.KEY), SchematicGenerator.CODEC);
|
||||
VirtualPocketType<SchematicGenerator> SCHEMATIC = register(new Identifier("dimdoors", SchematicGenerator.KEY), SchematicGenerator::new);
|
||||
|
||||
VirtualPocketType<DepthDependentSelector> DEPTH_DEPENDENT = register(new Identifier("dimdoors", DepthDependentSelector.KEY), DepthDependentSelector.CODEC);
|
||||
VirtualPocketType<DepthDependentSelector> DEPTH_DEPENDENT = register(new Identifier("dimdoors", DepthDependentSelector.KEY), DepthDependentSelector::new);
|
||||
|
||||
|
||||
VirtualPocket fromTag(CompoundTag tag);
|
||||
|
||||
Codec<T> getCodec();
|
||||
CompoundTag toTag(CompoundTag tag);
|
||||
|
||||
static void register() {
|
||||
}
|
||||
|
||||
static <T extends VirtualPocket> VirtualPocketType<T> register(Identifier id, Codec<T> codec) {
|
||||
return Registry.register(REGISTRY, id, () -> codec);
|
||||
static <U extends VirtualPocket> VirtualPocketType<U> register(Identifier id, Supplier<U> constructor) {
|
||||
return Registry.register(REGISTRY, id, new VirtualPocketType<U>() {
|
||||
@Override
|
||||
public VirtualPocket fromTag(CompoundTag tag) {
|
||||
return constructor.get().fromTag(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(CompoundTag tag) {
|
||||
tag.putString("type", id.toString());
|
||||
return tag;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.dimdev.dimdoors.pockets.generator;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -20,18 +19,22 @@ public class SchematicGenerator extends VirtualPocket {
|
|||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final String KEY = "schematic";
|
||||
|
||||
/*
|
||||
public static final Codec<SchematicGenerator> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
Codec.INT.fieldOf("size").forGetter(SchematicGenerator::getSize),
|
||||
Codec.STRING.fieldOf("id").forGetter(SchematicGenerator::getName),
|
||||
Codec.INT.optionalFieldOf("weight", 5).forGetter(schematicGenerator -> schematicGenerator.getWeight(null))
|
||||
).apply(instance, SchematicGenerator::new));
|
||||
*/
|
||||
|
||||
private final int size;
|
||||
private final String name;
|
||||
private final Identifier templateID;
|
||||
private final int weight;
|
||||
private int size;
|
||||
private String name;
|
||||
private Identifier templateID;
|
||||
private int weight;
|
||||
|
||||
SchematicGenerator(int size, String name, int weight) {
|
||||
public SchematicGenerator() {}
|
||||
|
||||
public SchematicGenerator(int size, String name, int weight) {
|
||||
this.size = size;
|
||||
this.name = name;
|
||||
this.weight = weight;
|
||||
|
@ -56,6 +59,26 @@ public class SchematicGenerator extends VirtualPocket {
|
|||
return this.weight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VirtualPocket fromTag(CompoundTag tag) {
|
||||
this.name = tag.getString("id");
|
||||
this.size = tag.getInt("size");
|
||||
this.weight = tag.contains("weight") ? tag.getInt("weight") : 5;
|
||||
|
||||
this.templateID = new Identifier("dimdoors", name);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(CompoundTag tag) {
|
||||
super.toTag(tag);
|
||||
|
||||
tag.putString("id", this.name);
|
||||
tag.putInt("size", this.size);
|
||||
tag.putInt("weight", this.weight);
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(String group) {
|
||||
SchematicV2Handler.getInstance().loadSchematic(templateID, group, size, name);
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
package org.dimdev.dimdoors.pockets.selection;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import net.minecraft.util.Pair;
|
||||
import com.google.common.collect.Maps;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import org.dimdev.dimdoors.pockets.VirtualPocket;
|
||||
import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
||||
import org.dimdev.dimdoors.world.pocket.Pocket;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DepthDependentSelector extends VirtualPocket {
|
||||
public static final String KEY = "depth_dependent";
|
||||
|
||||
/*
|
||||
private static final Codec<Pair<String, VirtualPocket>> PAIR_CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
Codec.STRING.fieldOf("regex").forGetter(Pair::getLeft),
|
||||
VirtualPocket.CODEC.fieldOf("pocket").forGetter(Pair::getRight)
|
||||
|
@ -22,13 +23,17 @@ public class DepthDependentSelector extends VirtualPocket {
|
|||
Codec.STRING.fieldOf("id").forGetter(DepthDependentSelector::getName),
|
||||
PAIR_CODEC.listOf().fieldOf("pockets").forGetter(DepthDependentSelector::getPocketList)
|
||||
).apply(instance, DepthDependentSelector::new));
|
||||
*/
|
||||
|
||||
|
||||
|
||||
private final String name;
|
||||
private final List<Pair<String, VirtualPocket>> pocketList;
|
||||
private String name;
|
||||
private LinkedHashMap<String, VirtualPocket> pocketList;
|
||||
|
||||
public DepthDependentSelector(String name, List<Pair<String, VirtualPocket>> pocketList) {
|
||||
public DepthDependentSelector() {
|
||||
|
||||
}
|
||||
public DepthDependentSelector(String name, LinkedHashMap<String, VirtualPocket> pocketList) {
|
||||
this.name = name;
|
||||
this.pocketList = pocketList;
|
||||
}
|
||||
|
@ -37,13 +42,42 @@ public class DepthDependentSelector extends VirtualPocket {
|
|||
return name;
|
||||
}
|
||||
|
||||
public List<Pair<String, VirtualPocket>> getPocketList() {
|
||||
public LinkedHashMap<String, VirtualPocket> getPocketList() {
|
||||
return pocketList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VirtualPocket fromTag(CompoundTag tag) {
|
||||
this.name = tag.getString("id");
|
||||
ListTag regexPockets = tag.getList("pockets", 10);
|
||||
pocketList = Maps.newLinkedHashMap();
|
||||
for (int i = 0; i < regexPockets.size(); i++) {
|
||||
CompoundTag pocket = regexPockets.getCompound(i);
|
||||
pocketList.put(pocket.getString("regex"), VirtualPocket.deserialize(pocket.getCompound("pocket")));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(CompoundTag tag) {
|
||||
super.toTag(tag);
|
||||
|
||||
tag.putString("id", this.name);
|
||||
|
||||
ListTag regexPockets = new ListTag();
|
||||
pocketList.forEach((regex, pocket) -> {
|
||||
CompoundTag compound = new CompoundTag();
|
||||
compound.putString("regex", regex);
|
||||
compound.put("pocket", pocket.toTag(new CompoundTag()));
|
||||
regexPockets.add(compound);
|
||||
});
|
||||
tag.put("pockets", regexPockets);
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(String group) {
|
||||
pocketList.forEach(pair -> pair.getRight().init(group));
|
||||
pocketList.forEach((regex, pocket) -> pocket.init(group));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -73,11 +107,11 @@ public class DepthDependentSelector extends VirtualPocket {
|
|||
}
|
||||
|
||||
private VirtualPocket getNextPocket(PocketGenerationParameters parameters) {
|
||||
for (Pair<String, VirtualPocket> pair : pocketList) {
|
||||
if (Pattern.compile(pair.getLeft()).matcher(String.valueOf(parameters.getSourceVirtualLocation().getDepth())).matches()) {
|
||||
return pair.getRight();
|
||||
for (Map.Entry<String, VirtualPocket> entry : pocketList.entrySet()) {
|
||||
if (Pattern.compile(entry.getKey()).matcher(String.valueOf(parameters.getSourceVirtualLocation().getDepth())).matches()) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
return pocketList.get(0).getRight();
|
||||
return pocketList.values().stream().findFirst().get();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,49 +2,49 @@
|
|||
"group": "private",
|
||||
"pockets": [
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_0",
|
||||
"size": 0,
|
||||
"weight": 20
|
||||
},
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_1",
|
||||
"size": 1,
|
||||
"weight": 17
|
||||
},
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_2",
|
||||
"size": 2,
|
||||
"weight": 14
|
||||
},
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_3",
|
||||
"size": 3,
|
||||
"weight": 11
|
||||
},
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_4",
|
||||
"size": 4,
|
||||
"weight": 8
|
||||
},
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_5",
|
||||
"size": 5,
|
||||
"weight": 5
|
||||
},
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_6",
|
||||
"size": 6,
|
||||
"weight": 3
|
||||
},
|
||||
{
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "private_pocket_7",
|
||||
"size": 7,
|
||||
"weight": 1
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
"group": "public",
|
||||
"pockets": [
|
||||
{
|
||||
"virtual_type": "depth_dependent",
|
||||
"type": "dimdoors:depth_dependent",
|
||||
"id": "public_pocket",
|
||||
"pockets": [
|
||||
{
|
||||
"regex": "6",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_0",
|
||||
"size": 0,
|
||||
"weight": 20
|
||||
|
@ -17,7 +17,7 @@
|
|||
{
|
||||
"regex": "7",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_1",
|
||||
"size": 1,
|
||||
"weight": 17
|
||||
|
@ -26,7 +26,7 @@
|
|||
{
|
||||
"regex": "8",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_2",
|
||||
"size": 2,
|
||||
"weight": 14
|
||||
|
@ -35,7 +35,7 @@
|
|||
{
|
||||
"regex": "9",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_3",
|
||||
"size": 3,
|
||||
"weight": 11
|
||||
|
@ -44,7 +44,7 @@
|
|||
{
|
||||
"regex": "10",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_4",
|
||||
"size": 4,
|
||||
"weight": 8
|
||||
|
@ -53,7 +53,7 @@
|
|||
{
|
||||
"regex": "11",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_5",
|
||||
"size": 5,
|
||||
"weight": 5
|
||||
|
@ -62,7 +62,7 @@
|
|||
{
|
||||
"regex": "12",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_6",
|
||||
"size": 6,
|
||||
"weight": 3
|
||||
|
@ -71,7 +71,7 @@
|
|||
{
|
||||
"regex": "[0-9]+",
|
||||
"pocket": {
|
||||
"virtual_type": "schematic",
|
||||
"type": "dimdoors:schematic",
|
||||
"id": "public_pocket_7",
|
||||
"size": 7,
|
||||
"weight": 1
|
||||
|
|
Loading…
Reference in a new issue