Cache rift data

This commit is contained in:
SD 2021-03-26 08:47:56 +05:30
parent 2c196e3bf2
commit 059b6bd2a8
38 changed files with 96 additions and 102 deletions

View file

@ -2,9 +2,12 @@ package org.dimdev.dimdoors.api.util;
import com.mojang.serialization.Codec;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.fabricmc.fabric.api.util.NbtType;
public class NbtUtil {
public static <T> T deserialize(Tag data, Codec<T> codec) {
return NbtOps.INSTANCE.withParser(codec).apply(data).getOrThrow(true, a -> {
@ -15,4 +18,12 @@ public class NbtUtil {
return NbtOps.INSTANCE.withEncoder(codec).apply(data).getOrThrow(true, a -> {
});
}
public static CompoundTag asCompoundTag(Tag tag, String error) {
if (tag == null || tag.getType() == NbtType.COMPOUND) {
return (CompoundTag) tag;
}
throw new RuntimeException(error);
}
}

View file

@ -23,6 +23,8 @@ import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.api.util.NbtUtil;
import org.dimdev.dimdoors.block.entity.RiftData;
import org.dimdev.dimdoors.pockets.generator.PocketGenerator;
import org.dimdev.dimdoors.pockets.virtual.VirtualPocket;
import org.dimdev.dimdoors.api.util.WeightedList;
@ -35,6 +37,7 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
private Map<Identifier, PocketGenerator> pocketGeneratorMap = new ConcurrentHashMap<>();
private Map<Identifier, VirtualPocket> pocketGroups = new ConcurrentHashMap<>();
private Map<Identifier, PocketTemplate> templates = new ConcurrentHashMap<>();
private Map<Identifier, CompoundTag> riftDataMap = new ConcurrentHashMap<>();
private PocketLoader() {
}
@ -44,14 +47,17 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
pocketGeneratorMap.clear();
pocketGroups.clear();
templates.clear();
riftDataMap.clear();
CompletableFuture<Map<Identifier, PocketGenerator>> futurePocketGeneratorMap = loadResourcePathFromJsonToMap(manager, "pockets/generators", this::loadPocketGenerator);
CompletableFuture<Map<Identifier, VirtualPocket>> futurePocketGroups = loadResourcePathFromJsonToMap(manager, "pockets/groups", this::loadPocketGroup);
CompletableFuture<Map<Identifier, PocketTemplate>> futureTemplates = loadResourcePathFromCompressedNbtToMap(manager, "pockets/schematic", ".schem", this::loadPocketTemplate);
CompletableFuture<Map<Identifier, CompoundTag>> futureRiftDataMap = loadResourcePathFromJsonToMap(manager, "pockets/json", t -> NbtUtil.asCompoundTag(t, "Could not load rift data since its json does not represent a CompoundTag!"));
pocketGeneratorMap = futurePocketGeneratorMap.join();
pocketGroups = futurePocketGroups.join();
templates = futureTemplates.join();
riftDataMap = futureRiftDataMap.join();
}
private <T> CompletableFuture<Map<Identifier, T>> loadResourcePathFromJsonToMap(ResourceManager manager, String startingPath, Function<Tag, T> reader) {
@ -71,7 +77,7 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
})));
}
private <T> CompletableFuture<Map<Identifier, T>> loadResourcePathFromCompressedNbtToMap(ResourceManager manager, String startingPath, String extension, Function<Tag, T> reader) {
private <T> CompletableFuture<Map<Identifier, T>> loadResourcePathFromCompressedNbtToMap(ResourceManager manager, String startingPath, String extension, Function<CompoundTag, T> reader) {
int sub = startingPath.endsWith("/") ? 0 : 1;
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(extension));
@ -108,23 +114,8 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
// }
// }
// TODO: load via resource loader
public Tag readNbtFromJson(String id) {
try {
Path path = Paths.get(PocketLoader.class.getResource("/resourcepacks/default_pockets/data/dimdoors/pockets/json/" + id + ".json").toURI());
if (!Files.isRegularFile(path)) {
return null;
}
try {
JsonElement json = GSON.fromJson(String.join("", Files.readAllLines(path)), JsonElement.class);
return JsonOps.INSTANCE.convertTo(NbtOps.INSTANCE, json);
} catch (IOException e) {
LOGGER.error(e);
}
} catch (URISyntaxException e) {
LOGGER.error(e);
}
return null;
public Tag getRiftDataTag(String id) {
return this.riftDataMap.get(new Identifier(id));
}
private VirtualPocket loadPocketGroup(Tag tag) {
@ -132,18 +123,12 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
}
private PocketGenerator loadPocketGenerator(Tag tag) {
if (tag == null || tag.getType() != NbtType.COMPOUND) {
throw new RuntimeException("Could not load PocketGenerator since its json does not represent a CompoundTag!");
}
return PocketGenerator.deserialize((CompoundTag) tag);
return PocketGenerator.deserialize(NbtUtil.asCompoundTag(tag, "Could not load PocketGenerator since its json does not represent a CompoundTag!"));
}
private PocketTemplate loadPocketTemplate(Tag tag) {
if (tag == null || tag.getType() != NbtType.COMPOUND) {
throw new RuntimeException("Could not load Schematic since its json does not represent a CompoundTag!");
}
private PocketTemplate loadPocketTemplate(CompoundTag tag) {
try {
return new PocketTemplate(Schematic.fromTag((CompoundTag) tag));
return new PocketTemplate(Schematic.fromTag(tag));
} catch (Exception e) {
throw new RuntimeException("Error loading " + tag.toString(), e);
}

View file

@ -49,7 +49,6 @@ public class DimensionalDoorModifier implements LazyCompatibleModifier {
private Equation yEquation;
private Equation zEquation;
@Override
public Modifier fromTag(CompoundTag tag) {
String facingString = tag.getString("facing");
@ -67,7 +66,7 @@ public class DimensionalDoorModifier implements LazyCompatibleModifier {
if (tag.getType("rift_data") == NbtType.STRING) {
doorDataReference = tag.getString("rift_data");
doorData = (CompoundTag) PocketLoader.getInstance().readNbtFromJson(doorDataReference);
doorData = (CompoundTag) PocketLoader.getInstance().getRiftDataTag(doorDataReference);
}
else if (tag.getType("rift_data") == NbtType.COMPOUND) doorData = tag.getCompound("rift_data");

View file

@ -32,7 +32,7 @@ public class RiftDataModifier implements Modifier {
public Modifier fromTag(CompoundTag tag) {
if (tag.getType("rift_data") == NbtType.STRING) {
doorDataReference = tag.getString("rift_data");
doorData = (CompoundTag) PocketLoader.getInstance().readNbtFromJson(doorDataReference);
doorData = (CompoundTag) PocketLoader.getInstance().getRiftDataTag(doorDataReference);
}
else if (tag.getType("rift_data") == NbtType.COMPOUND) doorData = tag.getCompound("rift_data");

View file

@ -43,10 +43,6 @@ public final class ModDimensions {
return Objects.equals(type, PERSONAL) || Objects.equals(type, PUBLIC) || Objects.equals(type, DUNGEON);
}
public static boolean isLimbo(StructureWorldAccess world) {
return world != null && world.getDimension() == LIMBO_TYPE;
}
public static boolean isLimboDimension(World world) {
return world != null && world.getRegistryKey().equals(LIMBO);
}

View file

@ -39,7 +39,7 @@ public enum LimboGateway implements Gateway {
@Override
public boolean isLocationValid(StructureWorldAccess world, BlockPos pos) {
return ModDimensions.isLimbo(world);
return ModDimensions.isLimboDimension(world.toServerWorld());
}
private void placePortal(StructureWorldAccess world, BlockPos pos, Direction facing) {

View file

@ -31,7 +31,7 @@ public class Pocket extends AbstractPocket<Pocket> implements AddonProvider {
private final Map<Identifier, PocketAddon> addons = new HashMap<>();
private int range = -1;
public BlockBox box; // TODO: make protected
private BlockBox box; // TODO: make protected
public VirtualLocation virtualLocation;
public Pocket(int id, RegistryKey<World> world, int x, int z) {
@ -42,7 +42,6 @@ public class Pocket extends AbstractPocket<Pocket> implements AddonProvider {
}
protected Pocket() {
}
public boolean hasAddon(Identifier id) {
@ -182,6 +181,10 @@ public class Pocket extends AbstractPocket<Pocket> implements AddonProvider {
return new PocketBuilder(AbstractPocketType.POCKET);
}
protected void setBox(BlockBox box) {
this.box = box;
}
// TODO: flesh this out a bit more, stuff like box() makes little sense in how it is implemented atm
public static class PocketBuilder<P extends PocketBuilder<P, T>, T extends Pocket> extends AbstractPocketBuilder<P, T> {
private final Map<Identifier, PocketAddon.PocketBuilderAddon<?>> addons = new HashMap<>();
@ -248,7 +251,7 @@ public class Pocket extends AbstractPocket<Pocket> implements AddonProvider {
T instance = super.build();
instance.setRange(range);
instance.box = BlockBox.create(origin.getX(), origin.getY(), origin.getZ(), origin.getX() + size.getX(), origin.getY() + size.getY(), origin.getZ() + size.getZ());
instance.setBox(BlockBox.create(origin.getX(), origin.getY(), origin.getZ(), origin.getX() + size.getX(), origin.getY() + size.getY(), origin.getZ() + size.getZ()));
instance.virtualLocation = virtualLocation;
addons.values().forEach(addon -> addon.apply(instance));

View file

@ -27,8 +27,8 @@ public class DyeableAddon implements PocketAddon {
private int count = 0;
private static int amountOfDyeRequiredToColor(Pocket pocket) {
int outerVolume = pocket.box.getBlockCountX() * pocket.box.getBlockCountY() * pocket.box.getBlockCountZ();
int innerVolume = (pocket.box.getBlockCountX() - 5) * (pocket.box.getBlockCountY() - 5) * (pocket.box.getBlockCountZ() - 5);
int outerVolume = pocket.getBox().getBlockCountX() * pocket.getBox().getBlockCountY() * pocket.getBox().getBlockCountZ();
int innerVolume = (pocket.getBox().getBlockCountX() - 5) * (pocket.getBox().getBlockCountY() - 5) * (pocket.getBox().getBlockCountZ() - 5);
return Math.max((outerVolume - innerVolume) / BLOCKS_PAINTED_PER_DYE, 1);
}

View file

@ -9,12 +9,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -9,12 +9,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -18,7 +18,7 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
}
],
"offset_x": "1",

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,17 +8,17 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,22 +8,22 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:rift_data",
"ids": [3],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1,2,3,4,5,6,7,8],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/escape"
"rift_data": "dimdoors:rift_data/escape"
}
]
}

View file

@ -8,17 +8,17 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/escape"
"rift_data": "dimdoors:rift_data/escape"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:relative",

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:relative",

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:relative",

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,17 +8,17 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,7 +8,7 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,7 +8,7 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -18,7 +18,7 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -9,12 +9,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,12 +8,12 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -9,17 +9,17 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}

View file

@ -8,7 +8,7 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
}
]
}

View file

@ -9,17 +9,17 @@
{
"type": "dimdoors:rift_data",
"ids": [0],
"rift_data": "rift_data/pocket_entrance"
"rift_data": "dimdoors:rift_data/pocket_entrance"
},
{
"type": "dimdoors:rift_data",
"ids": [1],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
},
{
"type": "dimdoors:rift_data",
"ids": [2],
"rift_data": "rift_data/available_link"
"rift_data": "dimdoors:rift_data/available_link"
}
]
}