loading NbtTags from different json file by reference

This commit is contained in:
CreepyCre 2021-02-04 20:19:53 +01:00
parent 1ac749affe
commit 70807ece50
2 changed files with 28 additions and 5 deletions

View file

@ -7,6 +7,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import com.google.common.collect.*;
import com.google.gson.*;
@ -35,7 +36,7 @@ public class SchematicV2Handler {
public void load() {
if (this.loaded) {
throw new UnsupportedOperationException("Attempted to load schematics twice!");
throw new UnsupportedOperationException("Attempted to load pockets twice!");
}
this.loaded = true;
long startTime = System.currentTimeMillis();
@ -58,6 +59,28 @@ public class SchematicV2Handler {
}
}
public Tag readNbtFromJson(String id) {
try {
Path path = Paths.get(SchematicV2Handler.class.getResource("/data/dimdoors/pockets/json/" + id.replaceAll("\\.", "/") + ".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 <T> T readNbtSerializableFromJson(String id, Function<Tag, T> reader) {
Tag tag = readNbtFromJson(id);
if (tag == null) return null;
return reader.apply(tag);
}
private void loadJson(Path path, String[] idParts, BiConsumer<String, Tag> loader) {
if (Files.isDirectory(path)) {
try {

View file

@ -24,6 +24,7 @@ import org.dimdev.dimdoors.block.ModBlocks;
import org.dimdev.dimdoors.block.entity.EntranceRiftBlockEntity;
import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes;
import org.dimdev.dimdoors.block.entity.RiftData;
import org.dimdev.dimdoors.pockets.SchematicV2Handler;
import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.targets.PocketEntranceMarker;
import org.dimdev.dimdoors.rift.targets.PocketExitMarker;
@ -40,6 +41,7 @@ public class DimensionalDoorModifier implements Modifier {
private Direction facing;
private String doorTypeString;
private DimensionalDoorBlock doorType;
private String doorDataReference;
private CompoundTag doorData;
private String x;
@ -67,7 +69,8 @@ public class DimensionalDoorModifier implements Modifier {
}
doorType = (DimensionalDoorBlock) doorBlock;
if (tag.contains("door_data")) doorData = tag.getCompound("door_data");
if (tag.getType("door_data") == NbtType.STRING) doorData = (CompoundTag) SchematicV2Handler.getInstance().readNbtFromJson(tag.getString("door_data"));
else if (tag.getType("door_data") == NbtType.COMPOUND) doorData = tag.getCompound("door_data");
try {
x = tag.getString("x");
@ -129,7 +132,4 @@ public class DimensionalDoorModifier implements Modifier {
}
world.setBlockEntity(pos, rift);
}
// TODO: move this to utility class
}