added depth dependent pockets

somewhat flattened the pocket json
This commit is contained in:
CreepyCre 2021-01-26 21:02:36 +01:00
parent 0af4b02a56
commit 43c5ebeeeb
8 changed files with 241 additions and 214 deletions

View file

@ -48,59 +48,4 @@ public final class PocketGroup {
public int hashCode() {
return Objects.hash(this.group, this.entries);
}
/*
public static final class PocketEntry {
public static final Codec<PocketEntry> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("size").forGetter(PocketEntry::getSize),
Codec.STRING.fieldOf("id").forGetter(PocketEntry::getName),
Codec.INT.optionalFieldOf("weight", 5).forGetter(PocketEntry::getWeight)
).apply(instance, PocketEntry::new));
private final int size;
private final String name;
private final int weight;
PocketEntry(int size, String name, int weight) {
this.size = size;
this.name = name;
this.weight = weight;
}
public int getSize() {
return this.size;
}
public String getName() {
return this.name;
}
public int getWeight() {
return this.weight;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
PocketEntry that = (PocketEntry) o;
return this.size == that.size &&
Float.compare(that.weight, this.weight) == 0 &&
this.name.equals(that.name);
}
@Override
public int hashCode() {
return Objects.hash(this.size, this.name, this.weight);
}
@Override
public String toString() {
return "PocketEntry{" +
"size=" + this.size +
", name='" + this.name + '\'' +
", weight=" + this.weight +
'}';
}
}
*/
}

View file

@ -28,13 +28,11 @@ public class PocketTemplateV2 {
private static final Logger LOGGER = LogManager.getLogger();
private static final boolean replacingPlaceholders = false;
private final Schematic schematic;
private final String group;
private final int size;
private final String id;
public PocketTemplateV2(Schematic schematic, String group, int size, String id) {
public PocketTemplateV2(Schematic schematic, int size, String id) {
this.schematic = schematic;
this.group = group;
this.size = size;
this.id = id;
}
@ -84,10 +82,6 @@ public class PocketTemplateV2 {
return replacingPlaceholders;
}
public String getGroup() {
return this.group;
}
public Schematic getSchematic() {
return this.schematic;
}

View file

@ -30,7 +30,6 @@ public class SchematicV2Handler {
private final Map<Identifier, PocketTemplateV2> templates = Maps.newHashMap();
private final Map<String, WeightedList<VirtualPocket, PocketGenerationParameters>> templateMap = Maps.newHashMap(); //TODO: un-ugly-fy
private final List<PocketGroup> pocketTypes = Lists.newArrayList();
private static final Random RANDOM = new Random(new Random().nextLong());
private boolean loaded = false;
private SchematicV2Handler() {
@ -55,8 +54,16 @@ public class SchematicV2Handler {
}
JsonObject json = GSON.fromJson(String.join("", result), JsonObject.class);
PocketGroup type = PocketGroup.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow(false, System.err::println).getFirst();
this.pocketTypes.add(type);
this.loadResourceSchematics(type);
WeightedList<VirtualPocket, PocketGenerationParameters> weightedPockets = new WeightedList<>();
templateMap.put(type.getGroup(), weightedPockets);
for (VirtualPocket virtualPocket : type.getEntries()) {
virtualPocket.init(type.getGroup());
weightedPockets.add(virtualPocket);
}
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
@ -65,29 +72,19 @@ public class SchematicV2Handler {
LOGGER.info("Loaded schematics in {} seconds", System.currentTimeMillis() - startTime);
}
/*
TODO: Change, is currently ugly.
Maybe request schematic from within VirtualSchematicPocket#init() and add default void init() to VirtualPocket?
*/
private void loadResourceSchematics(PocketGroup type) throws URISyntaxException, IOException {
String group = type.getGroup();
WeightedList<VirtualPocket, PocketGenerationParameters> weightedPockets = new WeightedList<>();
templateMap.put(group, weightedPockets);
Path basePath = Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/schematic/v2/%s/", group)).toURI());
for (VirtualPocket virtualPocket : type.getEntries()) {
weightedPockets.add(virtualPocket);
if (virtualPocket instanceof VirtualSchematicPocket) {
VirtualSchematicPocket schemPocket = (VirtualSchematicPocket) virtualPocket;
Path schemPath = basePath.resolve(schemPocket.getName() + ".schem");
CompoundTag schemTag = NbtIo.readCompressed(Files.newInputStream(schemPath));
Schematic schematic = Schematic.fromTag(schemTag);
PocketTemplateV2 template = new PocketTemplateV2(schematic, group, schemPocket.getSize(), schemPocket.getName());
Identifier templateID = new Identifier("dimdoors", schemPocket.getName());
templates.put(templateID, template);
schemPocket.setTemplateID(templateID);
}
}
}
public void loadSchematic(Identifier templateID, String group, int size, String id) {
try {
if (templates.containsKey(templateID)) return;
Path basePath = Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/schematic/v2/%s/", group)).toURI());
Path schemPath = basePath.resolve(id + ".schem");
CompoundTag schemTag = NbtIo.readCompressed(Files.newInputStream(schemPath));
Schematic schematic = Schematic.fromTag(schemTag);
PocketTemplateV2 template = new PocketTemplateV2(schematic, size, id);
templates.put(templateID, template);
} catch (URISyntaxException | IOException e) {
LOGGER.error("Could not load schematic!", e);
}
}
public VirtualPocket getRandomPublicPocket(PocketGenerationParameters parameters) {
return getRandomPocketFromGroup("public", parameters);

View file

@ -0,0 +1,82 @@
package org.dimdev.dimdoors.pockets;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.Pair;
import org.dimdev.dimdoors.util.PocketGenerationParameters;
import org.dimdev.dimdoors.world.pocket.Pocket;
import java.util.List;
import java.util.regex.Pattern;
public class VirtualDepthDependentPocket 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)
).apply(instance, Pair::new));
public static final Codec<VirtualDepthDependentPocket> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("id").forGetter(VirtualDepthDependentPocket::getName),
PAIR_CODEC.listOf().fieldOf("pockets").forGetter(VirtualDepthDependentPocket::getPocketList)
).apply(instance, VirtualDepthDependentPocket::new));
private final String name;
private final List<Pair<String, VirtualPocket>> pocketList;
public VirtualDepthDependentPocket(String name, List<Pair<String, VirtualPocket>> pocketList) {
this.name = name;
this.pocketList = pocketList;
}
public String getName() {
return name;
}
public List<Pair<String, VirtualPocket>> getPocketList() {
return pocketList;
}
@Override
public void init(String group) {
pocketList.forEach(pair -> pair.getRight().init(group));
}
@Override
public Pocket prepareAndPlacePocket(PocketGenerationParameters parameters) {
return getNextPocket(parameters).prepareAndPlacePocket(parameters);
}
// TODO: write method
@Override
public String toString() {
return null;
}
@Override
public VirtualPocketType<? extends VirtualPocket> getType() {
return VirtualPocketType.DEPTH_DEPENDENT;
}
@Override
public String getKey() {
return null;
}
@Override
public int getWeight(PocketGenerationParameters parameters) {
return getNextPocket(parameters).getWeight(parameters);
}
private VirtualPocket getNextPocket(PocketGenerationParameters parameters) {
for (Pair<String, VirtualPocket> pair : pocketList) {
if (Pattern.compile(pair.getLeft()).matcher(String.valueOf(parameters.getVirtualLocation().getDepth())).matches()) {
return pair.getRight();
}
}
return pocketList.get(0).getRight();
}
}

View file

@ -3,30 +3,24 @@ 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.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.util.registry.SimpleRegistry;
import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.PocketGenerationParameters;
import org.dimdev.dimdoors.util.Weighted;
import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.VirtualLocation;
/*
*/
// maybe PocketEntry is a better name? Only realised after I named this that the previous PocketEntry would be redundant.
// TODO: possibly rename to PocketHandler (and rename subclasses accordingly)
// 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) {
Identifier id = Identifier.CODEC.decode(dynamicOps, dynamicOps.get(input, "virtual_type").getOrThrow(false, System.err::println)).getOrThrow(false, System.err::println).getFirst();
return REGISTRY.get(id).getCodec().decode(dynamicOps, dynamicOps.get(input, "properties").getOrThrow(false, System.err::println)).map(pair -> pair.mapFirst(virtualPocket -> (VirtualPocket) virtualPocket)); // TODO: can you use forField() here?
Identifier id = new Identifier("dimdoors", Codec.STRING.decode(dynamicOps, dynamicOps.get(input, "virtual_type").getOrThrow(false, System.err::println)).getOrThrow(false, System.err::println).getFirst());
return REGISTRY.get(id).getCodec().decode(dynamicOps, input).map(pair -> pair.mapFirst(virtualPocket -> (VirtualPocket) virtualPocket));
}
@Override
@ -35,21 +29,29 @@ public abstract class VirtualPocket implements Weighted<PocketGenerationParamete
}
};
public abstract void init(String group);
public abstract Pocket prepareAndPlacePocket(PocketGenerationParameters parameters);
public abstract String toString();
// TODO: are equals() and hashCode() necessary?
public abstract VirtualPocketType<? extends VirtualPocket> getType();
public abstract String getKey();
public interface VirtualPocketType<T extends VirtualPocket> {
VirtualPocketType<VirtualSchematicPocket> SCHEMATIC = register("dimdoors:schematic", VirtualSchematicPocket.CODEC);
VirtualPocketType<VirtualSchematicPocket> SCHEMATIC = register(new Identifier("dimdoors", VirtualSchematicPocket.KEY), VirtualSchematicPocket.CODEC);
VirtualPocketType<VirtualDepthDependentPocket> DEPTH_DEPENDENT = register(new Identifier("dimdoors", VirtualDepthDependentPocket.KEY), VirtualDepthDependentPocket.CODEC);
Codec<T> getCodec();
static void register() {
}
static <T extends VirtualPocket> VirtualPocketType<T> register(String id, Codec<T> codec) {
static <T extends VirtualPocket> VirtualPocketType<T> register(Identifier id, Codec<T> codec) {
return Registry.register(REGISTRY, id, () -> codec);
}
}

View file

@ -15,6 +15,7 @@ import org.dimdev.dimdoors.world.pocket.VirtualLocation;
public class VirtualSchematicPocket extends VirtualPocket{
private static final Logger LOGGER = LogManager.getLogger();
public static final String KEY = "schematic";
public static final Codec<VirtualSchematicPocket> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("size").forGetter(VirtualSchematicPocket::getSize),
@ -24,13 +25,15 @@ public class VirtualSchematicPocket extends VirtualPocket{
private final int size;
private final String name;
private Identifier templateID;
private final Identifier templateID;
private final int weight;
VirtualSchematicPocket(int size, String name, int weight) {
this.size = size;
this.name = name;
this.weight = weight;
this.templateID = new Identifier("dimdoors", name);
}
public int getSize() {
@ -45,15 +48,16 @@ public class VirtualSchematicPocket extends VirtualPocket{
return templateID;
}
public void setTemplateID(Identifier templateID) {
this.templateID = templateID;
}
@Override
public int getWeight(PocketGenerationParameters parameters){
return this.weight;
}
@Override
public void init(String group) {
SchematicV2Handler.getInstance().loadSchematic(templateID, group, size, name);
}
@Override
public Pocket prepareAndPlacePocket(PocketGenerationParameters parameters) {
ServerWorld world = parameters.getWorld();
@ -85,4 +89,9 @@ public class VirtualSchematicPocket extends VirtualPocket{
public VirtualPocketType<? extends VirtualPocket> getType() {
return VirtualPocketType.SCHEMATIC;
}
@Override
public String getKey() {
return KEY;
}
}

View file

@ -2,68 +2,52 @@
"group": "private",
"pockets": [
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_0",
"size": 0,
"weight": 20
}
"virtual_type": "schematic",
"id": "private_pocket_0",
"size": 0,
"weight": 20
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_1",
"size": 1,
"weight": 17
}
"virtual_type": "schematic",
"id": "private_pocket_1",
"size": 1,
"weight": 17
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_2",
"size": 2,
"weight": 14
}
"virtual_type": "schematic",
"id": "private_pocket_2",
"size": 2,
"weight": 14
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_3",
"size": 3,
"weight": 11
}
"virtual_type": "schematic",
"id": "private_pocket_3",
"size": 3,
"weight": 11
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_4",
"size": 4,
"weight": 8
}
"virtual_type": "schematic",
"id": "private_pocket_4",
"size": 4,
"weight": 8
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_5",
"size": 5,
"weight": 5
}
"virtual_type": "schematic",
"id": "private_pocket_5",
"size": 5,
"weight": 5
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_6",
"size": 6,
"weight": 3
}
"virtual_type": "schematic",
"id": "private_pocket_6",
"size": 6,
"weight": 3
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "private_pocket_7",
"size": 7,
"weight": 1
}
"virtual_type": "schematic",
"id": "private_pocket_7",
"size": 7,
"weight": 1
}
]
}

View file

@ -2,68 +2,82 @@
"group": "public",
"pockets": [
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_0",
"size": 0,
"weight": 20
}
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_1",
"size": 1,
"weight": 17
}
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_2",
"size": 2,
"weight": 14
}
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_3",
"size": 3,
"weight": 11
}
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_4",
"size": 4,
"weight": 8
}
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_5",
"size": 5,
"weight": 5
}
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_6",
"size": 6,
"weight": 3
}
},
{
"virtual_type": "dimdoors:schematic",
"properties": {
"id": "public_pocket_7",
"size": 7,
"weight": 1
}
"virtual_type": "depth_dependent",
"id": "public_pocket",
"pockets": [
{
"regex": "0",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_0",
"size": 0,
"weight": 20
}
},
{
"regex": "1",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_1",
"size": 1,
"weight": 17
}
},
{
"regex": "2",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_2",
"size": 2,
"weight": 14
}
},
{
"regex": "3",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_3",
"size": 3,
"weight": 11
}
},
{
"regex": "4",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_4",
"size": 4,
"weight": 8
}
},
{
"regex": "5",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_5",
"size": 5,
"weight": 5
}
},
{
"regex": "6",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_6",
"size": 6,
"weight": 3
}
},
{
"regex": "[0-9]+",
"pocket": {
"virtual_type": "schematic",
"id": "public_pocket_7",
"size": 7,
"weight": 1
}
}
]
}
]
}