removed PocketGroup to flatten json
This commit is contained in:
parent
e8320ad60f
commit
0df83c8c0d
11 changed files with 121 additions and 152 deletions
|
@ -6,7 +6,7 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
|
||||
import org.dimdev.dimdoors.ModConfig;
|
||||
import org.dimdev.dimdoors.pockets.virtual.VirtualPocket;
|
||||
import org.dimdev.dimdoors.pockets.virtual.reference.PocketGeneratorReference;
|
||||
import org.dimdev.dimdoors.rift.registry.LinkProperties;
|
||||
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
|
||||
import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
||||
|
@ -47,7 +47,7 @@ public final class PocketGenerator {
|
|||
}
|
||||
|
||||
public static Pocket generatePrivatePocketV2(VirtualLocation virtualLocation) {
|
||||
return generateRandomPocketFromGroupV2(DimensionalDoorsInitializer.getWorld(ModDimensions.PERSONAL), "private", virtualLocation, null, null);
|
||||
return generateFromPocketGroupV2(DimensionalDoorsInitializer.getWorld(ModDimensions.PERSONAL), "private", virtualLocation, null, null);
|
||||
}
|
||||
|
||||
// TODO: size of public pockets should increase with depth
|
||||
|
@ -57,16 +57,16 @@ public final class PocketGenerator {
|
|||
}
|
||||
|
||||
public static Pocket generatePublicPocketV2(VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) {
|
||||
return generateRandomPocketFromGroupV2(DimensionalDoorsInitializer.getWorld(ModDimensions.PUBLIC), "public", virtualLocation, linkTo, linkProperties);
|
||||
return generateFromPocketGroupV2(DimensionalDoorsInitializer.getWorld(ModDimensions.PUBLIC), "public", virtualLocation, linkTo, linkProperties);
|
||||
}
|
||||
|
||||
public static Pocket generateRandomPocketFromGroupV2(ServerWorld world, String group, VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) {
|
||||
public static Pocket generateFromPocketGroupV2(ServerWorld world, String group, VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) {
|
||||
PocketGenerationParameters parameters = new PocketGenerationParameters(world, group, virtualLocation, linkTo, linkProperties);
|
||||
return generatePocketV2(SchematicV2Handler.getInstance().getRandomPocketFromGroup(group, parameters), parameters);
|
||||
return generatePocketV2(SchematicV2Handler.getInstance().getGroup(group).getNextPocketGeneratorReference(parameters), parameters);
|
||||
}
|
||||
|
||||
public static Pocket generatePocketV2(VirtualPocket virtualPocket, PocketGenerationParameters parameters) {
|
||||
return virtualPocket.prepareAndPlacePocket(parameters);
|
||||
public static Pocket generatePocketV2(PocketGeneratorReference pocketGeneratorReference, PocketGenerationParameters parameters) {
|
||||
return pocketGeneratorReference.prepareAndPlacePocket(parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
package org.dimdev.dimdoors.pockets;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import org.dimdev.dimdoors.pockets.virtual.VirtualPocketList;
|
||||
|
||||
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 String id;
|
||||
private VirtualPocketList pocketList;
|
||||
|
||||
public PocketGroup(String id) {
|
||||
}
|
||||
|
||||
public PocketGroup(String id, VirtualPocketList pocketList) {
|
||||
this.id = id;
|
||||
this.pocketList = pocketList;
|
||||
}
|
||||
|
||||
public PocketGroup fromTag(CompoundTag tag) {
|
||||
this.pocketList = VirtualPocketList.deserialize(tag.getList("pockets", 10));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompoundTag toTag(CompoundTag tag) {
|
||||
tag.put("pockets", pocketList.toTag(new ListTag()));
|
||||
return tag;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public VirtualPocketList getPocketList() {
|
||||
return this.pocketList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PocketType{" +
|
||||
"id='" + this.id + '\'' +
|
||||
|
||||
", entries=" + this.pocketList +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (super.equals(o)) return true;
|
||||
if (o == null || this.getClass() != o.getClass()) return false;
|
||||
PocketGroup that = (PocketGroup) o;
|
||||
return Objects.equals(this.id, that.id) &&
|
||||
Objects.equals(this.pocketList, that.pocketList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.id, this.pocketList);
|
||||
}
|
||||
}
|
|
@ -9,18 +9,16 @@ import java.util.*;
|
|||
import java.util.function.BiConsumer;
|
||||
|
||||
import com.google.common.collect.*;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.*;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
|
||||
import net.fabricmc.fabric.api.util.NbtType;
|
||||
import net.minecraft.nbt.*;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.dimdev.dimdoors.pockets.generator.PocketGenerator;
|
||||
import org.dimdev.dimdoors.pockets.virtual.VirtualPocket;
|
||||
import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
||||
import org.dimdev.dimdoors.util.schematic.v2.Schematic;
|
||||
|
||||
public class SchematicV2Handler {
|
||||
|
@ -28,7 +26,7 @@ public class SchematicV2Handler {
|
|||
private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create();
|
||||
private static final SchematicV2Handler INSTANCE = new SchematicV2Handler();
|
||||
private final Map<String, PocketGenerator> pocketGeneratorMap = Maps.newHashMap();
|
||||
private final Map<String, PocketGroup> pocketGroups = Maps.newHashMap();
|
||||
private final Map<String, VirtualPocket> pocketGroups = Maps.newHashMap();
|
||||
private final Map<Identifier, PocketTemplateV2> templates = Maps.newHashMap();
|
||||
private boolean loaded = false;
|
||||
|
||||
|
@ -60,7 +58,7 @@ public class SchematicV2Handler {
|
|||
}
|
||||
}
|
||||
|
||||
private void loadCompound(Path path, String[] idParts, BiConsumer<String, CompoundTag> loader) {
|
||||
private void loadCompound(Path path, String[] idParts, BiConsumer<String, Tag> loader) {
|
||||
if (Files.isDirectory(path)) {
|
||||
try {
|
||||
for (Path directoryPath : Files.newDirectoryStream(path)) {
|
||||
|
@ -76,22 +74,43 @@ public class SchematicV2Handler {
|
|||
} else if(Files.isRegularFile(path) && path.getFileName().toString().endsWith(".json")) {
|
||||
String id = String.join(".", idParts);
|
||||
try {
|
||||
JsonObject json = GSON.fromJson(String.join("", Files.readAllLines(path)), JsonObject.class);
|
||||
CompoundTag tag = CompoundTag.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow(false, LOGGER::error).getFirst();
|
||||
loader.accept(id, tag);
|
||||
JsonElement json = GSON.fromJson(String.join("", Files.readAllLines(path)), JsonElement.class);
|
||||
loader.accept(id, jsonToTag(json));
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("could not load pocket data in path " + path.toString() + " due to malformed json.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadPocketGroup(String id, CompoundTag tag) {
|
||||
PocketGroup group = new PocketGroup(id).fromTag(tag);
|
||||
private Tag jsonToTag(JsonElement json) {
|
||||
if (json.isJsonArray()) {
|
||||
ListTag listTag = new ListTag();
|
||||
for (JsonElement jsonElement : (JsonArray) json) {
|
||||
Tag tag = jsonToTag(jsonElement);
|
||||
if (tag != null) listTag.add(jsonToTag(jsonElement));
|
||||
}
|
||||
return listTag;
|
||||
}
|
||||
else if (json.isJsonObject()) {
|
||||
return CompoundTag.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow(false, LOGGER::error).getFirst();
|
||||
} else {
|
||||
LOGGER.error("JsonElement was not JsonObject or JsonArray!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadPocketGroup(String id, Tag tag) {
|
||||
VirtualPocket group = VirtualPocket.deserialize(tag);
|
||||
LOGGER.info(VirtualPocket.serialize(group).toString());
|
||||
pocketGroups.put(id, group);
|
||||
}
|
||||
|
||||
private void loadPocketGenerator(String id, CompoundTag tag) {
|
||||
PocketGenerator gen = PocketGenerator.deserialize(tag);
|
||||
private void loadPocketGenerator(String id, Tag tag) {
|
||||
if (tag == null || tag.getType() != NbtType.COMPOUND) {
|
||||
LOGGER.error("Could not load PocketGenerator " + id + " since the json does not represent a CompoundTag!");
|
||||
return;
|
||||
}
|
||||
PocketGenerator gen = PocketGenerator.deserialize((CompoundTag) tag);
|
||||
if (gen != null) pocketGeneratorMap.put(id, gen);
|
||||
}
|
||||
|
||||
|
@ -108,8 +127,8 @@ public class SchematicV2Handler {
|
|||
}
|
||||
}
|
||||
|
||||
public VirtualPocket getRandomPocketFromGroup(String group, PocketGenerationParameters parameters) {
|
||||
return pocketGroups.get(group).getPocketList().getNextRandomWeighted(parameters);
|
||||
public VirtualPocket getGroup(String group) {
|
||||
return pocketGroups.get(group);
|
||||
}
|
||||
|
||||
public static SchematicV2Handler getInstance() {
|
||||
|
@ -120,7 +139,7 @@ public class SchematicV2Handler {
|
|||
return this.templates;
|
||||
}
|
||||
|
||||
public Map<String, PocketGroup> getPocketGroups() {
|
||||
public Map<String, VirtualPocket> getPocketGroups() {
|
||||
return this.pocketGroups;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ package org.dimdev.dimdoors.pockets.virtual;
|
|||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import org.dimdev.dimdoors.pockets.PocketGroup;
|
||||
import org.dimdev.dimdoors.pockets.virtual.reference.PocketGeneratorReference;
|
||||
import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
||||
import org.dimdev.dimdoors.util.Weighted;
|
||||
import org.dimdev.dimdoors.world.pocket.Pocket;
|
||||
|
@ -26,4 +26,8 @@ public interface VirtualPocket extends Weighted<PocketGenerationParameters> {
|
|||
|
||||
|
||||
Pocket prepareAndPlacePocket(PocketGenerationParameters parameters);
|
||||
|
||||
PocketGeneratorReference getNextPocketGeneratorReference(PocketGenerationParameters parameters);
|
||||
|
||||
PocketGeneratorReference peekNextPocketGeneratorReference(PocketGenerationParameters parameters);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package org.dimdev.dimdoors.pockets.virtual;
|
||||
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import org.dimdev.dimdoors.pockets.PocketGroup;
|
||||
import org.dimdev.dimdoors.pockets.virtual.reference.PocketGeneratorReference;
|
||||
import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
||||
import org.dimdev.dimdoors.util.WeightedList;
|
||||
import org.dimdev.dimdoors.world.pocket.Pocket;
|
||||
|
@ -30,15 +30,23 @@ public class VirtualPocketList extends WeightedList<VirtualPocket, PocketGenerat
|
|||
}
|
||||
|
||||
public ListTag toTag(ListTag tag) {
|
||||
int pointer = tag.size() - 1;
|
||||
for(VirtualPocket virtualPocket : this) {
|
||||
tag.set(pointer, VirtualPocket.serialize(virtualPocket));
|
||||
tag.add(VirtualPocket.serialize(virtualPocket));
|
||||
}
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pocket prepareAndPlacePocket(PocketGenerationParameters parameters) {
|
||||
return getNextRandomWeighted(parameters).prepareAndPlacePocket(parameters);
|
||||
return getNextPocketGeneratorReference(parameters).prepareAndPlacePocket(parameters);
|
||||
}
|
||||
|
||||
public PocketGeneratorReference getNextPocketGeneratorReference(PocketGenerationParameters parameters) {
|
||||
return getNextRandomWeighted(parameters).getNextPocketGeneratorReference(parameters);
|
||||
}
|
||||
|
||||
public PocketGeneratorReference peekNextPocketGeneratorReference(PocketGenerationParameters parameters) {
|
||||
return peekNextRandomWeighted(parameters).peekNextPocketGeneratorReference(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -48,8 +48,6 @@ public abstract class VirtualSingularPocket implements VirtualPocket {
|
|||
return this.getType().toTag(tag);
|
||||
}
|
||||
|
||||
public abstract Pocket prepareAndPlacePocket(PocketGenerationParameters parameters);
|
||||
|
||||
public abstract VirtualSingularPocketType<? extends VirtualSingularPocket> getType();
|
||||
|
||||
public abstract String getKey();
|
||||
|
|
|
@ -30,12 +30,12 @@ public class IdReference extends PocketGeneratorReference {
|
|||
}
|
||||
|
||||
@Override
|
||||
public PocketGenerator peekReferencedPocket(PocketGenerationParameters parameters) {
|
||||
return getReferencedPocket(parameters);
|
||||
public PocketGenerator peekReferencedPocketGenerator(PocketGenerationParameters parameters) {
|
||||
return getReferencedPocketGenerator(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PocketGenerator getReferencedPocket(PocketGenerationParameters parameters) {
|
||||
public PocketGenerator getReferencedPocketGenerator(PocketGenerationParameters parameters) {
|
||||
return SchematicV2Handler.getInstance().getGenerator(id);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import net.minecraft.nbt.CompoundTag;
|
|||
import net.minecraft.nbt.ListTag;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.dimdev.dimdoors.pockets.SchematicV2Handler;
|
||||
import org.dimdev.dimdoors.pockets.generator.PocketGenerator;
|
||||
import org.dimdev.dimdoors.pockets.modifier.Modifier;
|
||||
import org.dimdev.dimdoors.pockets.virtual.VirtualSingularPocket;
|
||||
|
@ -79,7 +78,7 @@ public abstract class PocketGeneratorReference extends VirtualSingularPocket {
|
|||
|
||||
@Override
|
||||
public double getWeight(PocketGenerationParameters parameters) {
|
||||
return weightEquation != null ? this.weightEquation.apply(parameters.toVariableMap(Maps.newHashMap())) : peekReferencedPocket(parameters).getWeight(parameters);
|
||||
return weightEquation != null ? this.weightEquation.apply(parameters.toVariableMap(Maps.newHashMap())) : peekReferencedPocketGenerator(parameters).getWeight(parameters);
|
||||
}
|
||||
|
||||
public void applyModifiers(Pocket pocket, PocketGenerationParameters parameters) {
|
||||
|
@ -90,7 +89,7 @@ public abstract class PocketGeneratorReference extends VirtualSingularPocket {
|
|||
|
||||
@Override
|
||||
public Pocket prepareAndPlacePocket(PocketGenerationParameters parameters) {
|
||||
PocketGenerator generator = getReferencedPocket(parameters);
|
||||
PocketGenerator generator = getReferencedPocketGenerator(parameters);
|
||||
Pocket pocket = generator.prepareAndPlacePocket(parameters);
|
||||
generator.applyModifiers(pocket, parameters);
|
||||
this.applyModifiers(pocket, parameters);
|
||||
|
@ -98,7 +97,17 @@ public abstract class PocketGeneratorReference extends VirtualSingularPocket {
|
|||
return pocket;
|
||||
}
|
||||
|
||||
public abstract PocketGenerator peekReferencedPocket(PocketGenerationParameters parameters);
|
||||
@Override
|
||||
public PocketGeneratorReference peekNextPocketGeneratorReference(PocketGenerationParameters parameters) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract PocketGenerator getReferencedPocket(PocketGenerationParameters parameters);
|
||||
@Override
|
||||
public PocketGeneratorReference getNextPocketGeneratorReference(PocketGenerationParameters parameters) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract PocketGenerator peekReferencedPocketGenerator(PocketGenerationParameters parameters);
|
||||
|
||||
public abstract PocketGenerator getReferencedPocketGenerator(PocketGenerationParameters parameters);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import net.minecraft.nbt.CompoundTag;
|
|||
import net.minecraft.nbt.ListTag;
|
||||
import org.dimdev.dimdoors.pockets.virtual.VirtualPocket;
|
||||
import org.dimdev.dimdoors.pockets.virtual.VirtualSingularPocket;
|
||||
import org.dimdev.dimdoors.pockets.virtual.reference.PocketGeneratorReference;
|
||||
import org.dimdev.dimdoors.util.PocketGenerationParameters;
|
||||
import org.dimdev.dimdoors.world.pocket.Pocket;
|
||||
|
||||
|
@ -32,8 +33,8 @@ public class DepthDependentSelector extends VirtualSingularPocket {
|
|||
private LinkedHashMap<String, VirtualPocket> pocketList;
|
||||
|
||||
public DepthDependentSelector() {
|
||||
|
||||
}
|
||||
|
||||
public DepthDependentSelector(String name, LinkedHashMap<String, VirtualPocket> pocketList) {
|
||||
this.name = name;
|
||||
this.pocketList = pocketList;
|
||||
|
@ -83,10 +84,14 @@ public class DepthDependentSelector extends VirtualSingularPocket {
|
|||
return getNextPocket(parameters).prepareAndPlacePocket(parameters);
|
||||
}
|
||||
|
||||
// TODO: write method
|
||||
@Override
|
||||
public String toString() {
|
||||
return null;
|
||||
public PocketGeneratorReference getNextPocketGeneratorReference(PocketGenerationParameters parameters) {
|
||||
return getNextPocket(parameters).getNextPocketGeneratorReference(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PocketGeneratorReference peekNextPocketGeneratorReference(PocketGenerationParameters parameters) {
|
||||
return getNextPocket(parameters).peekNextPocketGeneratorReference(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,36 +1,34 @@
|
|||
{
|
||||
"pockets": [
|
||||
{
|
||||
"id": "private.default_0",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_1",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_2",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_3",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_4",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_5",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_6",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_7",
|
||||
"type": "dimdoors:id"
|
||||
}
|
||||
]
|
||||
}
|
||||
[
|
||||
{
|
||||
"id": "private.default_0",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_1",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_2",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_3",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_4",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_5",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_6",
|
||||
"type": "dimdoors:id"
|
||||
},
|
||||
{
|
||||
"id": "private.default_7",
|
||||
"type": "dimdoors:id"
|
||||
}
|
||||
]
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
{
|
||||
"pockets": [
|
||||
{
|
||||
"id": "public.default",
|
||||
"type": "dimdoors:id"
|
||||
}
|
||||
]
|
||||
"id": "public.default",
|
||||
"type": "dimdoors:id"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue