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() { public int hashCode() {
return Objects.hash(this.group, this.entries); 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 Logger LOGGER = LogManager.getLogger();
private static final boolean replacingPlaceholders = false; private static final boolean replacingPlaceholders = false;
private final Schematic schematic; private final Schematic schematic;
private final String group;
private final int size; private final int size;
private final String id; 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.schematic = schematic;
this.group = group;
this.size = size; this.size = size;
this.id = id; this.id = id;
} }
@ -84,10 +82,6 @@ public class PocketTemplateV2 {
return replacingPlaceholders; return replacingPlaceholders;
} }
public String getGroup() {
return this.group;
}
public Schematic getSchematic() { public Schematic getSchematic() {
return this.schematic; return this.schematic;
} }

View file

@ -30,7 +30,6 @@ public class SchematicV2Handler {
private final Map<Identifier, PocketTemplateV2> templates = Maps.newHashMap(); private final Map<Identifier, PocketTemplateV2> templates = Maps.newHashMap();
private final Map<String, WeightedList<VirtualPocket, PocketGenerationParameters>> templateMap = Maps.newHashMap(); //TODO: un-ugly-fy private final Map<String, WeightedList<VirtualPocket, PocketGenerationParameters>> templateMap = Maps.newHashMap(); //TODO: un-ugly-fy
private final List<PocketGroup> pocketTypes = Lists.newArrayList(); private final List<PocketGroup> pocketTypes = Lists.newArrayList();
private static final Random RANDOM = new Random(new Random().nextLong());
private boolean loaded = false; private boolean loaded = false;
private SchematicV2Handler() { private SchematicV2Handler() {
@ -55,8 +54,16 @@ public class SchematicV2Handler {
} }
JsonObject json = GSON.fromJson(String.join("", result), JsonObject.class); JsonObject json = GSON.fromJson(String.join("", result), JsonObject.class);
PocketGroup type = PocketGroup.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow(false, System.err::println).getFirst(); PocketGroup type = PocketGroup.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow(false, System.err::println).getFirst();
this.pocketTypes.add(type); 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) { } catch (IOException | URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -65,29 +72,19 @@ public class SchematicV2Handler {
LOGGER.info("Loaded schematics in {} seconds", System.currentTimeMillis() - startTime); LOGGER.info("Loaded schematics in {} seconds", System.currentTimeMillis() - startTime);
} }
/* public void loadSchematic(Identifier templateID, String group, int size, String id) {
TODO: Change, is currently ugly. try {
Maybe request schematic from within VirtualSchematicPocket#init() and add default void init() to VirtualPocket? if (templates.containsKey(templateID)) return;
*/ Path basePath = Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/schematic/v2/%s/", group)).toURI());
private void loadResourceSchematics(PocketGroup type) throws URISyntaxException, IOException { Path schemPath = basePath.resolve(id + ".schem");
String group = type.getGroup(); CompoundTag schemTag = NbtIo.readCompressed(Files.newInputStream(schemPath));
WeightedList<VirtualPocket, PocketGenerationParameters> weightedPockets = new WeightedList<>(); Schematic schematic = Schematic.fromTag(schemTag);
templateMap.put(group, weightedPockets); PocketTemplateV2 template = new PocketTemplateV2(schematic, size, id);
Path basePath = Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/schematic/v2/%s/", group)).toURI()); templates.put(templateID, template);
for (VirtualPocket virtualPocket : type.getEntries()) { } catch (URISyntaxException | IOException e) {
weightedPockets.add(virtualPocket); LOGGER.error("Could not load schematic!", e);
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 VirtualPocket getRandomPublicPocket(PocketGenerationParameters parameters) { public VirtualPocket getRandomPublicPocket(PocketGenerationParameters parameters) {
return getRandomPocketFromGroup("public", 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.datafixers.util.Pair;
import com.mojang.serialization.*; import com.mojang.serialization.*;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey; import net.minecraft.util.registry.RegistryKey;
import net.minecraft.util.registry.SimpleRegistry; 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.PocketGenerationParameters;
import org.dimdev.dimdoors.util.Weighted; import org.dimdev.dimdoors.util.Weighted;
import org.dimdev.dimdoors.world.pocket.Pocket; import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.VirtualLocation;
/*
*/ // TODO: possibly rename to PocketHandler (and rename subclasses accordingly)
// TODO: do something about getting correct Pocket sizes
// maybe PocketEntry is a better name? Only realised after I named this that the previous PocketEntry would be redundant.
public abstract class VirtualPocket implements Weighted<PocketGenerationParameters> { 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 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>() { public static final Codec<VirtualPocket> CODEC = new Codec<VirtualPocket>() {
@Override @Override
public <T> DataResult<Pair<VirtualPocket, T>> decode(DynamicOps<T> dynamicOps, T input) { 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(); 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, dynamicOps.get(input, "properties").getOrThrow(false, System.err::println)).map(pair -> pair.mapFirst(virtualPocket -> (VirtualPocket) virtualPocket)); // TODO: can you use forField() here? return REGISTRY.get(id).getCodec().decode(dynamicOps, input).map(pair -> pair.mapFirst(virtualPocket -> (VirtualPocket) virtualPocket));
} }
@Override @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 Pocket prepareAndPlacePocket(PocketGenerationParameters parameters);
public abstract String toString(); public abstract String toString();
// TODO: are equals() and hashCode() necessary? // TODO: are equals() and hashCode() necessary?
public abstract VirtualPocketType<? extends VirtualPocket> getType(); public abstract VirtualPocketType<? extends VirtualPocket> getType();
public abstract String getKey();
public interface VirtualPocketType<T extends VirtualPocket> { 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(); Codec<T> getCodec();
static void register() { 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); return Registry.register(REGISTRY, id, () -> codec);
} }
} }

View file

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

View file

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

View file

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