From 356bfe4bd036c5e7116e4f2845fad1b111042581 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Wed, 31 Jul 2013 08:05:58 -0400 Subject: [PATCH] Limited Sizes of Generated Dungeons Added validation to SchematicLoader that prevents dungeons beyond certain maximum dimensions from being loaded. Our current limits are the same as the largest dungeon we could export. Rewrote generateDungeonPocket() to handle that possible failure better. --- .../mod_pocketDim/SchematicLoader.java | 78 +++++++++++++------ .../mod_pocketDim/helpers/DungeonHelper.java | 7 +- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/StevenDimDoors/mod_pocketDim/SchematicLoader.java b/StevenDimDoors/mod_pocketDim/SchematicLoader.java index 8116cec..c47cf4a 100644 --- a/StevenDimDoors/mod_pocketDim/SchematicLoader.java +++ b/StevenDimDoors/mod_pocketDim/SchematicLoader.java @@ -1,6 +1,7 @@ package StevenDimDoors.mod_pocketDim; import java.io.File; import java.io.FileNotFoundException; +import java.util.HashMap; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.dungeon.DungeonSchematic; @@ -9,41 +10,52 @@ import StevenDimDoors.mod_pocketDim.helpers.dimHelper; import StevenDimDoors.mod_pocketDim.schematic.InvalidSchematicException; public class SchematicLoader -{ +{ private SchematicLoader() { } - public static void generateDungeonPocket(LinkData link, DDProperties properties) + public static boolean generateDungeonPocket(LinkData link, DDProperties properties) { //TODO: Phase this function out in the next update. ~SenseiKiwi - String filePath=DungeonHelper.instance().defaultBreak.schematicPath; - if(dimHelper.dimList.containsKey(link.destDimID)) + if (link == null || properties == null) { - if(dimHelper.dimList.get(link.destDimID).dungeonGenerator == null) - { - DungeonHelper.instance().generateDungeonLink(link); - } - filePath = dimHelper.dimList.get(link.destDimID).dungeonGenerator.schematicPath; + return false; } - - //this.generateSchematic(link.destXCoord, link.destYCoord, link.destZCoord, link.linkOrientation, link.destDimID, link.locDimID, filePath); - try { + String schematicPath; int originDimID = link.locDimID; int destDimID = link.destDimID; - DungeonSchematic dungeon; - if ((new File(filePath)).exists()) + HashMap dimList = dimHelper.dimList; + + if (dimList.containsKey(destDimID)) { - dungeon = DungeonSchematic.readFromFile(filePath); + if (dimList.get(destDimID).dungeonGenerator == null) + { + DungeonHelper.instance().generateDungeonLink(link); + } + schematicPath = dimList.get(destDimID).dungeonGenerator.schematicPath; } else { - dungeon = DungeonSchematic.readFromResource(filePath); + return false; } - dungeon.applyImportFilters(properties); - dimHelper.dimList.get(destDimID).hasBeenFilled = true; + DungeonSchematic dungeon = checkSourceAndLoad(schematicPath); + + //Validate the dungeon's dimensions + if (!hasValidDimensions(dungeon)) + { + //TODO: In the future, remove this dungeon from the generation lists altogether. + //That will have to wait until our code is updated to support that more easily. + System.err.println("The following schematic file has dimensions that exceed the maximum permitted dimensions for dungeons: " + schematicPath); + System.err.println("The dungeon will not be loaded."); + + dungeon = checkSourceAndLoad(DungeonHelper.instance().defaultBreak.schematicPath); + } + + dungeon.applyImportFilters(properties); + dimList.get(destDimID).hasBeenFilled = true; if (dimHelper.getWorld(destDimID) == null) { dimHelper.initDimension(destDimID); @@ -51,14 +63,34 @@ public class SchematicLoader World world = dimHelper.getWorld(destDimID); dungeon.copyToWorld(world, new Point3D(link.destXCoord, link.destYCoord, link.destZCoord), link.linkOrientation, originDimID, destDimID); + return true; } - catch (FileNotFoundException e) - { - e.printStackTrace(); - } - catch (InvalidSchematicException e) + catch (Exception e) { e.printStackTrace(); + return false; } } + + private static boolean hasValidDimensions(DungeonSchematic dungeon) + { + return (dungeon.getWidth() <= DungeonHelper.MAX_DUNGEON_WIDTH && + dungeon.getHeight() <= DungeonHelper.MAX_DUNGEON_HEIGHT && + dungeon.getLength() <= DungeonHelper.MAX_DUNGEON_LENGTH); + } + + private static DungeonSchematic checkSourceAndLoad(String schematicPath) throws FileNotFoundException, InvalidSchematicException + { + //TODO: Change this code once we introduce an isInternal flag in dungeon data + DungeonSchematic dungeon; + if ((new File(schematicPath)).exists()) + { + dungeon = DungeonSchematic.readFromFile(schematicPath); + } + else + { + dungeon = DungeonSchematic.readFromResource(schematicPath); + } + return dungeon; + } } \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java index 3103893..798c37a 100644 --- a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java +++ b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java @@ -31,6 +31,9 @@ public class DungeonHelper private static final int DEFAULT_DUNGEON_WEIGHT = 100; public static final int MAX_DUNGEON_WEIGHT = 10000; //Used to prevent overflows and math breaking down private static final int MAX_EXPORT_RADIUS = 50; + public static final short MAX_DUNGEON_WIDTH = 2 * MAX_EXPORT_RADIUS + 1; + public static final short MAX_DUNGEON_HEIGHT = 2 * MAX_EXPORT_RADIUS + 1; + public static final short MAX_DUNGEON_LENGTH = 2 * MAX_EXPORT_RADIUS + 1; public static final int FABRIC_OF_REALITY_EXPORT_ID = 1973; public static final int PERMAFABRIC_EXPORT_ID = 220; @@ -340,9 +343,9 @@ public class DungeonHelper //Write schematic data to a file try { - short size = (short) 2 * MAX_EXPORT_RADIUS + 1; DungeonSchematic dungeon = DungeonSchematic.copyFromWorld(world, - centerX - MAX_EXPORT_RADIUS, centerY - MAX_EXPORT_RADIUS, centerZ - MAX_EXPORT_RADIUS, size, size, size, true); + centerX - MAX_EXPORT_RADIUS, centerY - MAX_EXPORT_RADIUS, centerZ - MAX_EXPORT_RADIUS, + MAX_DUNGEON_WIDTH, MAX_DUNGEON_HEIGHT, MAX_DUNGEON_LENGTH, true); dungeon.applyExportFilters(properties); dungeon.writeToFile(exportPath); return true;