Clean up gateway init

Changes to be committed:
	modified:   src/main/java/org/dimdev/dimdoors/pockets/PocketTemplateV2.java
	modified:   src/main/java/org/dimdev/dimdoors/pockets/TemplateUtils.java
	modified:   src/main/java/org/dimdev/dimdoors/world/feature/gateway/schematic/SchematicV2Gateway.java
This commit is contained in:
SD 2020-10-11 10:41:56 +05:30
parent 8649c69d93
commit 4416ab96d1
No known key found for this signature in database
GPG key ID: E36B57EE08544BC5
3 changed files with 77 additions and 86 deletions

View file

@ -40,67 +40,6 @@ public class PocketTemplateV2 {
this.id = id;
}
public static void replacePlaceholders(Schematic schematic) {
// Replace placeholders (some schematics will contain them)
replacingPlaceholders = true;
List<CompoundTag> blockEntities = new ArrayList<>();
for (CompoundTag blockEntityTag : schematic.getBlockEntities()) {
if (blockEntityTag.contains("placeholder")) {
int x = blockEntityTag.getInt("x");
int y = blockEntityTag.getInt("y");
int z = blockEntityTag.getInt("z");
CompoundTag newTag = new CompoundTag();
EntranceRiftBlockEntity rift = Objects.requireNonNull(ModBlockEntityTypes.ENTRANCE_RIFT.instantiate());
switch (blockEntityTag.getString("placeholder")) {
case "deeper_depth_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.DEEPER_DUNGEON_DESTINATION);
newTag = rift.toTag(newTag);
break;
case "less_deep_depth_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.SHALLOWER_DUNGEON_DESTINATION);
newTag = rift.toTag(newTag);
break;
case "overworld_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.OVERWORLD_DESTINATION);
newTag = rift.toTag(newTag);
break;
case "entrance_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.TWO_WAY_POCKET_ENTRANCE);
newTag = rift.toTag(newTag);
break;
case "gateway_portal":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.OVERWORLD_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.GATEWAY_DESTINATION);
newTag = rift.toTag(newTag);
break;
default:
throw new RuntimeException("Unknown block entity placeholder: " + blockEntityTag.getString("placeholder"));
}
blockEntities.add(newTag);
} else {
blockEntities.add(blockEntityTag);
}
}
schematic.setBlockEntities(blockEntities);
// List<CompoundTag> entities = new ArrayList<>();
// for (CompoundTag entityTag : schematic.getEntities()) {
// TemplateUtils.setupEntityPlaceholders(entities, entityTag);
// }
// schematic.setEntities(entities);
replacingPlaceholders = false;
}
public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProperties) {
ServerWorld world = DimensionalDoorsInitializer.getWorld(pocket.world);

View file

@ -1,11 +1,14 @@
package org.dimdev.dimdoors.pockets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.block.entity.EntranceRiftBlockEntity;
import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes;
import org.dimdev.dimdoors.block.entity.RiftBlockEntity;
import org.dimdev.dimdoors.entity.ModEntityTypes;
import org.dimdev.dimdoors.entity.MonolithEntity;
@ -16,6 +19,7 @@ import org.dimdev.dimdoors.rift.targets.PocketExitMarker;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.math.MathUtil;
import org.dimdev.dimdoors.util.schematic.v2.Schematic;
import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.PocketRegistry;
@ -28,6 +32,7 @@ import net.minecraft.loot.context.LootContextTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
public class TemplateUtils {
static void setupEntityPlaceholders(List<CompoundTag> entities, CompoundTag entityTag) {
@ -111,4 +116,63 @@ public class TemplateUtils {
rift.markDirty();
}
}
public static void replacePlaceholders(Schematic schematic) {
// Replace placeholders (some schematics will contain them)
List<CompoundTag> blockEntities = new ArrayList<>();
for (CompoundTag blockEntityTag : schematic.getBlockEntities()) {
if (blockEntityTag.contains("placeholder")) {
int x = blockEntityTag.getInt("x");
int y = blockEntityTag.getInt("y");
int z = blockEntityTag.getInt("z");
CompoundTag newTag = new CompoundTag();
EntranceRiftBlockEntity rift = Objects.requireNonNull(ModBlockEntityTypes.ENTRANCE_RIFT.instantiate());
switch (blockEntityTag.getString("placeholder")) {
case "deeper_depth_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.DEEPER_DUNGEON_DESTINATION);
newTag = rift.toTag(newTag);
break;
case "less_deep_depth_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.SHALLOWER_DUNGEON_DESTINATION);
newTag = rift.toTag(newTag);
break;
case "overworld_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.OVERWORLD_DESTINATION);
newTag = rift.toTag(newTag);
break;
case "entrance_door":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.POCKET_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.TWO_WAY_POCKET_ENTRANCE);
newTag = rift.toTag(newTag);
break;
case "gateway_portal":
rift.setPos(new BlockPos(x, y, z));
rift.setProperties(DefaultDungeonDestinations.OVERWORLD_LINK_PROPERTIES);
rift.setDestination(DefaultDungeonDestinations.GATEWAY_DESTINATION);
newTag = rift.toTag(newTag);
break;
default:
throw new RuntimeException("Unknown block entity placeholder: " + blockEntityTag.getString("placeholder"));
}
blockEntities.add(newTag);
} else {
blockEntities.add(blockEntityTag);
}
}
schematic.setBlockEntities(blockEntities);
// List<CompoundTag> entities = new ArrayList<>();
// for (CompoundTag entityTag : schematic.getEntities()) {
// TemplateUtils.setupEntityPlaceholders(entities, entityTag);
// }
// schematic.setEntities(entities);
}
}

View file

@ -3,9 +3,11 @@ package org.dimdev.dimdoors.world.feature.gateway.schematic;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.pockets.TemplateUtils;
import org.dimdev.dimdoors.util.schematic.v2.Schematic;
import org.dimdev.dimdoors.util.schematic.v2.SchematicPlacer;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
@ -31,33 +33,19 @@ public abstract class SchematicV2Gateway implements Gateway {
public void init() {
String schematicJarDirectory = "/data/dimdoors/gateways/v2/";
InputStream schematicStream = DimensionalDoorsInitializer.class.getResourceAsStream(schematicJarDirectory + this.id + ".schem");
DataInputStream schematicDataStream = null;
boolean streamOpened = false;
if (schematicStream != null) {
schematicDataStream = new DataInputStream(schematicStream);
streamOpened = true;
} else {
LOGGER.warn("Schematic '" + this.id + "' was not found in the jar or config directory, neither with the .schem extension, nor with the .schematic extension.");
try (InputStream stream = DimensionalDoorsInitializer.class.getResourceAsStream(schematicJarDirectory + this.id + ".schem")) {
if (stream == null) {
throw new RuntimeException("Schematic '" + this.id + "' was not found in the jar or config directory, neither with the .schem extension, nor with the .schematic extension.");
}
CompoundTag tag;
this.schematic = null;
if (streamOpened) {
try {
tag = NbtIo.readCompressed(schematicDataStream);
this.schematic = Schematic.fromTag(tag);
schematicDataStream.close();
this.schematic = Schematic.fromTag(NbtIo.readCompressed(stream));
} catch (IOException ex) {
LOGGER.error("Schematic file for " + this.id + " could not be read as a valid schematic NBT file.", ex);
} finally {
try {
schematicDataStream.close();
} catch (IOException ex) {
LOGGER.error("Error occured while closing schematicDataStream", ex);
}
throw new RuntimeException("Schematic file for " + this.id + " could not be read as a valid schematic NBT file.", ex);
}
TemplateUtils.replacePlaceholders(this.schematic);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}