From 1e2dedaafe6d84e755c68aec8bed4c9e950c8a86 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Sat, 15 Jun 2013 10:25:50 -0400 Subject: [PATCH] Overhauling DungeonHelper I changed the name filter for schematic names from CommandEndDungeonCreation to DungeonHelper, and renamed it to NamePattern. I also rewrote most of registerCustomDungeon() to be much more concise. The changes are incomplete but I'm making an intermediate commit. The aim is to change DungeonHelper into a proper singleton and to eliminate the clunky sections that manually map dungeon categories to their corresponding lists. Instead, I'll use data structures to implement that far more efficiently. --- .../commands/CommandEndDungeonCreation.java | 8 +- .../mod_pocketDim/helpers/DungeonHelper.java | 169 +++++++++++------- .../mod_pocketDim/mod_pocketDim.java | 2 +- 3 files changed, 105 insertions(+), 74 deletions(-) diff --git a/StevenDimDoors/mod_pocketDim/commands/CommandEndDungeonCreation.java b/StevenDimDoors/mod_pocketDim/commands/CommandEndDungeonCreation.java index 8c8971a9..891155a9 100644 --- a/StevenDimDoors/mod_pocketDim/commands/CommandEndDungeonCreation.java +++ b/StevenDimDoors/mod_pocketDim/commands/CommandEndDungeonCreation.java @@ -8,11 +8,11 @@ import net.minecraft.entity.player.EntityPlayer; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.DungeonGenerator; import StevenDimDoors.mod_pocketDim.mod_pocketDim; +import StevenDimDoors.mod_pocketDim.helpers.DungeonHelper; public class CommandEndDungeonCreation extends CommandBase { private static DDProperties properties = null; - private static Pattern nameFilter = Pattern.compile("[A-Za-z0-9_]+"); public CommandEndDungeonCreation() { @@ -59,13 +59,13 @@ public class CommandEndDungeonCreation extends CommandBase else if(!player.worldObj.isRemote) { //Check that the dungeon name is valid to prevent directory traversal and other forms of abuse - if (nameFilter.matcher(var2[0]).matches()) + if (DungeonHelper.NamePattern.matcher(var2[0]).matches()) { DungeonGenerator newDungeon = mod_pocketDim.dungeonHelper.exportDungeon(player.worldObj, x, y, z, properties.CustomSchematicDirectory + "/" + var2[0] + ".schematic"); - player.sendChatToPlayer("created dungeon schematic in " + properties.CustomSchematicDirectory +"/"+var2[0]+".schematic"); + player.sendChatToPlayer("created dungeon schematic in " + properties.CustomSchematicDirectory + "/" + var2[0]+".schematic"); mod_pocketDim.dungeonHelper.customDungeons.add(newDungeon); - if(mod_pocketDim.dungeonHelper.customDungeonStatus.containsKey(player.worldObj.provider.dimensionId)&&!player.worldObj.isRemote) + if (mod_pocketDim.dungeonHelper.customDungeonStatus.containsKey(player.worldObj.provider.dimensionId) && !player.worldObj.isRemote) { // mod_pocketDim.dungeonHelper.customDungeonStatus.remove(player.worldObj.provider.dimensionId); // dimHelper.instance.teleportToPocket(player.worldObj, mod_pocketDim.dungeonHelper.customDungeonStatus.get(player.worldObj.provider.dimensionId), player); diff --git a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java index 9a8936da..f36c3b96 100644 --- a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java +++ b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java @@ -5,6 +5,7 @@ import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; +import java.util.regex.Pattern; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; @@ -25,7 +26,11 @@ public class DungeonHelper private static DDProperties properties = null; private Random rand = new Random(); + private static final String SCHEMATIC_FILE_EXTENSION = ".schematic"; + private static final int DEFAULT_DUNGEON_WEIGHT = 100; + public static Pattern NamePattern = Pattern.compile("[A-Za-z0-9_]+"); + public HashMap customDungeonStatus = new HashMap(); public ArrayList customDungeons = new ArrayList(); @@ -53,96 +58,122 @@ public class DungeonHelper properties = DDProperties.instance(); } + public boolean validateSchematicName(String name) + { + String[] dungeonData = name.split("_"); + + //Check for a valid number of parts + if (dungeonData.length < 3 || dungeonData.length > 4) + return false; + + //Check if the category is valid + if (!tagList.contains(dungeonData[0])) + return false; + + //Check if the name is valid + if (!NamePattern.matcher(dungeonData[1]).matches()) + return false; + + //Check if the open/closed flag is present + if (!dungeonData[2].equalsIgnoreCase("open") && !dungeonData[2].equalsIgnoreCase("closed")) + return false; + + //If the weight is present, check that it is valid + if (dungeonData.length == 4) + { + try + { + int weight = Integer.parseInt(dungeonData[3]); + if (weight < 0) + return false; + } + catch (NumberFormatException e) + { + //Not a number + return false; + } + } + return true; + } + public void registerCustomDungeon(File schematicFile) { + String name = schematicFile.getName(); try { - if(schematicFile.getName().contains(".schematic")) + if (name.endsWith(SCHEMATIC_FILE_EXTENSION) && validateSchematicName(name)) { - String[] name = schematicFile.getName().split("_"); + //Strip off the file extension while splitting the file name + String[] dungeonData = name.substring(0, name.length() - SCHEMATIC_FILE_EXTENSION.length()).split("_"); + + String path = schematicFile.getAbsolutePath(); + boolean open = dungeonData[2].equals("open"); + int weight = (dungeonData.length == 4) ? Integer.parseInt(dungeonData[3]) : DEFAULT_DUNGEON_WEIGHT; - if(name.length<4) + //Change this code so that instead of using IFs, we use a hash table mapping (category) -> (list) + /*while(count