Changed DungeonHelper into a singleton

Changed DungeonHelper into a singleton. Changed code in other classes to
interface with it properly.
This commit is contained in:
SenseiKiwi 2013-06-16 01:00:05 -04:00
parent a5159055a4
commit f56893018d
7 changed files with 40 additions and 29 deletions

View file

@ -159,7 +159,7 @@ public class SchematicLoader
}
public int transformMetadata(int metadata, int orientation, int blockID)
{
if(mod_pocketDim.dungeonHelper.metadataFlipList.contains(blockID))
if (DungeonHelper.instance().metadataFlipList.contains(blockID))
{

View file

@ -8,6 +8,7 @@ import StevenDimDoors.mod_pocketDim.DimData;
import StevenDimDoors.mod_pocketDim.DungeonGenerator;
import StevenDimDoors.mod_pocketDim.LinkData;
import StevenDimDoors.mod_pocketDim.mod_pocketDim;
import StevenDimDoors.mod_pocketDim.helpers.DungeonHelper;
import StevenDimDoors.mod_pocketDim.helpers.dimHelper;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
@ -27,8 +28,9 @@ public class CommandAddDungeonRift extends CommandBase
@Override
public void processCommand(ICommandSender var1, String[] var2)
{
DungeonHelper dungeonHelper = DungeonHelper.instance();
if(var2==null||this.getCommandSenderAsPlayer(var1).worldObj.isRemote)
{
return;
@ -54,7 +56,7 @@ public class CommandAddDungeonRift extends CommandBase
}
else if(var2.length!=0&&var2[0].equals("list"))
{
for(DungeonGenerator dungeonGen : mod_pocketDim.dungeonHelper.registeredDungeons)
for(DungeonGenerator dungeonGen : dungeonHelper.registeredDungeons)
{
String dungeonName =dungeonGen.schematicPath;
if(dungeonName.contains("DimDoors_Custom_schematics"))
@ -69,7 +71,7 @@ public class CommandAddDungeonRift extends CommandBase
}
for(DungeonGenerator dungeonGen : mod_pocketDim.dungeonHelper.customDungeons)
for(DungeonGenerator dungeonGen : dungeonHelper.customDungeons)
{
String dungeonName =dungeonGen.schematicPath;
if(dungeonName.contains("DimDoors_Custom_schematics"))
@ -89,7 +91,7 @@ public class CommandAddDungeonRift extends CommandBase
else if(var2.length!=0)
{
for(DungeonGenerator dungeonGen : mod_pocketDim.dungeonHelper.registeredDungeons)
for(DungeonGenerator dungeonGen : dungeonHelper.registeredDungeons)
{
String dungeonName =dungeonGen.schematicPath.toLowerCase();
@ -113,7 +115,7 @@ public class CommandAddDungeonRift extends CommandBase
}
for(DungeonGenerator dungeonGen : mod_pocketDim.dungeonHelper.customDungeons)
for(DungeonGenerator dungeonGen : dungeonHelper.customDungeons)
{
String dungeonName =dungeonGen.schematicPath.toLowerCase();

View file

@ -28,9 +28,11 @@ public class CommandEndDungeonCreation extends CommandBase
@Override
public void processCommand(ICommandSender var1, String[] var2)
{
DungeonHelper dungeonHelper = DungeonHelper.instance();
EntityPlayer player = this.getCommandSenderAsPlayer(var1);
if(!mod_pocketDim.dungeonHelper.customDungeonStatus.containsKey(player.worldObj.provider.dimensionId))
if (!dungeonHelper.customDungeonStatus.containsKey(player.worldObj.provider.dimensionId))
{
if(var2.length<2)
{
@ -61,10 +63,10 @@ public class CommandEndDungeonCreation extends CommandBase
//Check that the dungeon name is valid to prevent directory traversal and other forms of abuse
if (DungeonHelper.NamePattern.matcher(var2[0]).matches())
{
DungeonGenerator newDungeon = mod_pocketDim.dungeonHelper.exportDungeon(player.worldObj, x, y, z, properties.CustomSchematicDirectory + "/" + var2[0] + ".schematic");
DungeonGenerator newDungeon = dungeonHelper.exportDungeon(player.worldObj, x, y, z, properties.CustomSchematicDirectory + "/" + var2[0] + ".schematic");
player.sendChatToPlayer("created dungeon schematic in " + properties.CustomSchematicDirectory + "/" + var2[0]+".schematic");
if (mod_pocketDim.dungeonHelper.customDungeonStatus.containsKey(player.worldObj.provider.dimensionId) && !player.worldObj.isRemote)
if (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);

View file

@ -53,7 +53,7 @@ public class CommandStartDungeonCreation extends CommandBase
// dimHelper.instance.teleportToPocket(player.worldObj, link, player);
mod_pocketDim.dungeonHelper.customDungeonStatus.put(link.destDimID, dimHelper.instance.getLinkDataFromCoords(link.destXCoord, link.destYCoord, link.destZCoord, link.destDimID));
DungeonHelper.instance().customDungeonStatus.put(link.destDimID, dimHelper.instance.getLinkDataFromCoords(link.destXCoord, link.destYCoord, link.destZCoord, link.destDimID));
this.getCommandSenderAsPlayer(var1).sendChatToPlayer("DimID = "+ link.destDimID);
}

View file

@ -25,6 +25,7 @@ import StevenDimDoors.mod_pocketDim.helpers.jnbt.Tag;
public class DungeonHelper
{
private static DungeonHelper instance = null;
private static DDProperties properties = null;
public static final Pattern NamePattern = Pattern.compile("[A-Za-z0-9_]+");
@ -76,7 +77,7 @@ public class DungeonHelper
private HashSet<String> dungeonTypeChecker;
private Hashtable<String, ArrayList<DungeonGenerator>> dungeonTypeMapping;
public DungeonHelper()
private DungeonHelper()
{
//Load the dungeon type checker with the list of all types in lowercase.
//Capitalization matters for matching in a hash set.
@ -100,6 +101,27 @@ public class DungeonHelper
properties = DDProperties.instance();
}
public static DungeonHelper create()
{
if (instance == null)
instance = new DungeonHelper();
else
throw new IllegalStateException("Cannot create DungeonHelper twice");
return instance;
}
public static DungeonHelper instance()
{
if (instance == null)
{
//This is to prevent some frustrating bugs that could arise when classes
//are loaded in the wrong order. Trust me, I had to squash a few...
throw new IllegalStateException("Instance of DungeonHelper requested before creation");
}
return instance;
}
public boolean validateSchematicName(String name)
{
String[] dungeonData = name.split("_");

View file

@ -978,7 +978,7 @@ public class dimHelper extends DimensionManager
if(isRandomRift)
{
mod_pocketDim.dungeonHelper.generateDungeonlink(link);
DungeonHelper.instance().generateDungeonlink(link);
}

View file

@ -96,7 +96,6 @@ public class mod_pocketDim
public static SchematicLoader loader;
public static pocketTeleporter teleporter;
public static DungeonHelper dungeonHelper;
public static ICommand printDimData;
public static ICommand removeRiftsCommand;
@ -158,7 +157,6 @@ public class mod_pocketDim
loader = new SchematicLoader();
teleporter = new pocketTeleporter();
dungeonHelper= new DungeonHelper();
printDimData = new CommandPrintDimData();
removeRiftsCommand = new CommandDeleteRifts();
@ -170,18 +168,6 @@ public class mod_pocketDim
startDungeonCreation = new CommandStartDungeonCreation();
tracker = new PlayerRespawnTracker();
riftGen = new RiftGenerator();
File file= new File(properties.CustomSchematicDirectory);
file.mkdir();
String helpFile = "/mods/DimDoors/How_to_add_dungeons.txt";
if(new File(helpFile).exists())
{
copyfile.copyFile(helpFile, file + "/How_to_add_dungeons.txt");
}
dungeonHelper.importCustomDungeons(properties.CustomSchematicDirectory);
dungeonHelper.registerBaseDungeons();
}
@Init
@ -249,7 +235,6 @@ public class mod_pocketDim
LanguageRegistry.addName(itemDimDoor, "Dimensional Door");
LanguageRegistry.addName(itemRiftBlade , "Rift Blade");
TickRegistry.registerTickHandler(new ClientTickHandler(), Side.CLIENT);
TickRegistry.registerTickHandler(new CommonTickHandler(), Side.SERVER);
@ -390,8 +375,8 @@ public class mod_pocketDim
mod_pocketDim.blocksImmuneToRift.add(Block.blockLapis.blockID);
mod_pocketDim.blocksImmuneToRift.add(Block.bedrock.blockID);
dungeonHelper.registerFlipBlocks();
DungeonHelper.create();
proxy.loadTextures();
proxy.registerRenderers();
}