Schematic resource loader
Changes to be committed: modified: src/main/java/org/dimdev/dimdoors/pockets/DefaultDungeonDestinations.java new file: src/main/java/org/dimdev/dimdoors/pockets/PocketType.java modified: src/main/java/org/dimdev/dimdoors/pockets/SchematicHandler.java new file: src/main/java/org/dimdev/dimdoors/pockets/SchematicV2Handler.java renamed: src/main/resources/data/dimdoors/pockets/json/default_private.json -> src/main/resources/data/dimdoors/pockets/json/v2/default_private.json renamed: src/main/resources/data/dimdoors/pockets/json/default_public.json -> src/main/resources/data/dimdoors/pockets/json/v2/default_public.json
This commit is contained in:
parent
0431dfc7c2
commit
1becf90e60
6 changed files with 181 additions and 23 deletions
|
@ -10,59 +10,58 @@ import org.dimdev.dimdoors.rift.targets.RandomTarget;
|
|||
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
|
||||
|
||||
public final class DefaultDungeonDestinations { // TODO: lower weights?
|
||||
public static final LinkProperties POCKET_LINK_PROPERTIES = LinkProperties
|
||||
.builder()
|
||||
public static final LinkProperties POCKET_LINK_PROPERTIES = LinkProperties.builder()
|
||||
.groups(new HashSet<>(Arrays.asList(0, 1)))
|
||||
.linksRemaining(1).build();
|
||||
.linksRemaining(1)
|
||||
.build();
|
||||
|
||||
public static final LinkProperties OVERWORLD_LINK_PROPERTIES = LinkProperties
|
||||
.builder()
|
||||
public static final LinkProperties OVERWORLD_LINK_PROPERTIES = LinkProperties.builder()
|
||||
.groups(new HashSet<>(Arrays.asList(0, 1)))
|
||||
.entranceWeight(50)
|
||||
.linksRemaining(1).build();
|
||||
.linksRemaining(1)
|
||||
.build();
|
||||
|
||||
public static final VirtualTarget DEEPER_DUNGEON_DESTINATION = RandomTarget
|
||||
.builder()
|
||||
public static final VirtualTarget DEEPER_DUNGEON_DESTINATION = RandomTarget.builder()
|
||||
.acceptedGroups(Collections.singleton(0))
|
||||
.coordFactor(1)
|
||||
.negativeDepthFactor(10000)
|
||||
.positiveDepthFactor(160)
|
||||
.weightMaximum(100)
|
||||
.newRiftWeight(1).build();
|
||||
.newRiftWeight(1)
|
||||
.build();
|
||||
|
||||
public static final VirtualTarget SHALLOWER_DUNGEON_DESTINATION = RandomTarget
|
||||
.builder()
|
||||
public static final VirtualTarget SHALLOWER_DUNGEON_DESTINATION = RandomTarget.builder()
|
||||
.acceptedGroups(Collections.singleton(0))
|
||||
.coordFactor(1)
|
||||
.negativeDepthFactor(160)
|
||||
.positiveDepthFactor(10000)
|
||||
.weightMaximum(100)
|
||||
.newRiftWeight(1).build();
|
||||
.newRiftWeight(1)
|
||||
.build();
|
||||
|
||||
public static final VirtualTarget OVERWORLD_DESTINATION = RandomTarget
|
||||
.builder()
|
||||
public static final VirtualTarget OVERWORLD_DESTINATION = RandomTarget.builder()
|
||||
.acceptedGroups(Collections.singleton(0))
|
||||
.coordFactor(1)
|
||||
.negativeDepthFactor(0.00000000001) // The division result is cast to an int, so Double.MIN_VALUE would cause an overflow
|
||||
.positiveDepthFactor(Double.POSITIVE_INFINITY)
|
||||
.weightMaximum(100)
|
||||
.newRiftWeight(1).build();
|
||||
.newRiftWeight(1)
|
||||
.build();
|
||||
|
||||
public static final VirtualTarget TWO_WAY_POCKET_ENTRANCE = PocketEntranceMarker
|
||||
.builder()
|
||||
public static final VirtualTarget TWO_WAY_POCKET_ENTRANCE = PocketEntranceMarker.builder()
|
||||
.weight(1)
|
||||
.ifDestination(new PocketEntranceMarker())
|
||||
.otherwiseDestination(RandomTarget
|
||||
.builder()
|
||||
.otherwiseDestination(RandomTarget.builder()
|
||||
.acceptedGroups(Collections.singleton(0))
|
||||
.coordFactor(1)
|
||||
.negativeDepthFactor(80)
|
||||
.positiveDepthFactor(10000)
|
||||
.weightMaximum(100)
|
||||
.newRiftWeight(1).build()).build();
|
||||
.newRiftWeight(1)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
public static final VirtualTarget GATEWAY_DESTINATION = RandomTarget
|
||||
.builder()
|
||||
public static final VirtualTarget GATEWAY_DESTINATION = RandomTarget.builder()
|
||||
.acceptedGroups(Collections.singleton(0))
|
||||
.coordFactor(1) // TODO: lower value?
|
||||
.negativeDepthFactor(Double.POSITIVE_INFINITY)
|
||||
|
|
95
src/main/java/org/dimdev/dimdoors/pockets/PocketType.java
Normal file
95
src/main/java/org/dimdev/dimdoors/pockets/PocketType.java
Normal file
|
@ -0,0 +1,95 @@
|
|||
package org.dimdev.dimdoors.pockets;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
public final class PocketType {
|
||||
public static final Codec<PocketType> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
Codec.STRING.fieldOf("group").forGetter(PocketType::getGroup),
|
||||
PocketEntry.CODEC.listOf().fieldOf("pockets").forGetter(PocketType::getEntries)
|
||||
).apply(instance, PocketType::new));
|
||||
private final String group;
|
||||
private final List<PocketEntry> entries;
|
||||
|
||||
public PocketType(String group, List<PocketEntry> entries) {
|
||||
this.group = group;
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return this.group;
|
||||
}
|
||||
|
||||
public List<PocketEntry> getEntries() {
|
||||
return this.entries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PocketType{" +
|
||||
"group='" + this.group + '\'' +
|
||||
", entries=" + this.entries +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (super.equals(o)) return true;
|
||||
if (o == null || this.getClass() != o.getClass()) return false;
|
||||
PocketType that = (PocketType) o;
|
||||
return Objects.equals(this.group, that.group) &&
|
||||
Objects.equals(this.entries, that.entries);
|
||||
}
|
||||
|
||||
@Override
|
||||
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("name").forGetter(PocketEntry::getName)
|
||||
).apply(instance, PocketEntry::new));
|
||||
private final int size;
|
||||
private final String name;
|
||||
|
||||
PocketEntry(int size, String name) {
|
||||
this.size = size;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@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 &&
|
||||
Objects.equals(this.name, that.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.size, this.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PocketEntry{" +
|
||||
"size=" + this.size +
|
||||
", name=" + this.name +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
|
@ -46,6 +46,9 @@ public class SchematicHandler { // TODO: parts of this should be moved to the or
|
|||
private static final String SAVED_POCKETS_GROUP_NAME = "saved_pockets";
|
||||
public static final SchematicHandler INSTANCE = new SchematicHandler();
|
||||
|
||||
private SchematicHandler() {
|
||||
}
|
||||
|
||||
private static String getFolder() {
|
||||
return "config"; // TODO
|
||||
}
|
||||
|
@ -453,7 +456,7 @@ public class SchematicHandler { // TODO: parts of this should be moved to the or
|
|||
PocketTemplate template2 = this.usageList.get(index).getKey();
|
||||
if (index >= this.usageList.size() || template != template2) {
|
||||
entry.setValue(this.usageList.size());
|
||||
this.usageList.add(new SimpleEntry(template, 1));
|
||||
this.usageList.add(new SimpleEntry<>(template, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package org.dimdev.dimdoors.pockets;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.dimdev.dimcore.schematic.v2.Schematic;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtIo;
|
||||
|
||||
public class SchematicV2Handler {
|
||||
private static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create();
|
||||
private final List<PocketTemplateV2> templates = Lists.newArrayList();
|
||||
private Map<String, Map<String, Integer>> nameMap;
|
||||
|
||||
public void load() {
|
||||
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);
|
||||
PocketType type = PocketType.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow(false, System.err::println).getFirst();
|
||||
} catch (IOException | URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadResourceSchematics(PocketType type) throws URISyntaxException, IOException {
|
||||
String group = type.getGroup();
|
||||
Path basePath = Paths.get(SchematicV2Handler.class.getResource(String.format("/data/dimdoors/pockets/schematic/v2/%s/", group)).toURI());
|
||||
for (PocketType.PocketEntry entry : type.getEntries()) {
|
||||
Path schemPath = basePath.resolve(entry.getName() + ".schem");
|
||||
CompoundTag schemTag = NbtIo.readCompressed(Files.newInputStream(schemPath));
|
||||
Schematic schematic = Schematic.fromTag(schemTag);
|
||||
this.templates.add(new PocketTemplateV2(schematic, group, entry.getSize(), entry.getName()));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue