From ad0e2cbe61b03b7ffc500cac2541920f1760ce2c Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Tue, 25 Jun 2013 20:54:58 -0400 Subject: [PATCH] Improved CommandPruneDimensions Improved CommandPruneDimensions. Cleaned up the code and improved performance by using a HashSet instead of an ArrayList to list dimension reachability. Added an optional argument that sets whether we should delete the folders of pruned dimensions. --- .../commands/CommandPruneDimensions.java | 56 +++++++++++-------- .../commands/DDCommandResult.java | 1 + .../mod_pocketDim/helpers/dimHelper.java | 19 ++++++- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/StevenDimDoors/mod_pocketDim/commands/CommandPruneDimensions.java b/StevenDimDoors/mod_pocketDim/commands/CommandPruneDimensions.java index d58f0b4..b130dbf 100644 --- a/StevenDimDoors/mod_pocketDim/commands/CommandPruneDimensions.java +++ b/StevenDimDoors/mod_pocketDim/commands/CommandPruneDimensions.java @@ -2,6 +2,8 @@ package StevenDimDoors.mod_pocketDim.commands; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; import net.minecraft.entity.player.EntityPlayer; import StevenDimDoors.mod_pocketDim.DimData; @@ -14,7 +16,7 @@ public class CommandPruneDimensions extends DDCommandBase private CommandPruneDimensions() { - super("dd-prune"); + super("dd-prune", "['delete']"); } public static CommandPruneDimensions instance() @@ -28,36 +30,44 @@ public class CommandPruneDimensions extends DDCommandBase @Override protected DDCommandResult processCommand(EntityPlayer sender, String[] command) { - int numRemoved=0; - ArrayList dimsWithLinks = new ArrayList(); + if (command.length > 1) + { + return DDCommandResult.TOO_MANY_ARGUMENTS; + } + if (command.length == 1 && !command[0].equalsIgnoreCase("delete")) + { + return DDCommandResult.INVALID_ARGUMENTS; + } + + int removedCount = 0; + boolean deleteFolders = (command.length == 1); + Set linkedDimensions = new HashSet(); Collection allDims = new ArrayList(); allDims.addAll(dimHelper.dimList.values()); - for(DimData data: allDims) + + for (DimData data : allDims) { - for(LinkData link:data.printAllLinkData()) + for (LinkData link : data.printAllLinkData()) { - if(!dimsWithLinks.contains(link.destDimID)) + linkedDimensions.add(link.destDimID); + } + } + for (LinkData link : dimHelper.instance.interDimLinkList.values()) + { + linkedDimensions.add(link.destDimID); + } + for (DimData data : allDims) + { + if (!linkedDimensions.contains(data.dimID)) + { + if (dimHelper.instance.pruneDimension(data, deleteFolders)) { - dimsWithLinks.add(link.destDimID); + removedCount++; } } } - for(LinkData link : dimHelper.instance.interDimLinkList.values()) - { - if(!dimsWithLinks.contains(link.destDimID)) - { - dimsWithLinks.add(link.destDimID); - } - } - for(DimData data : allDims) - { - if(!dimsWithLinks.contains(data.dimID)) - { - dimHelper.dimList.remove(data.dimID); - numRemoved++; - } - } dimHelper.instance.save(); - sender.sendChatToPlayer("Removed " + numRemoved + " unreachable pocket dims."); + sender.sendChatToPlayer("Removed " + removedCount + " unreachable pocket dims."); + return DDCommandResult.SUCCESS; } } \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/commands/DDCommandResult.java b/StevenDimDoors/mod_pocketDim/commands/DDCommandResult.java index c40a99e..9ec0cf7 100644 --- a/StevenDimDoors/mod_pocketDim/commands/DDCommandResult.java +++ b/StevenDimDoors/mod_pocketDim/commands/DDCommandResult.java @@ -8,6 +8,7 @@ public class DDCommandResult { public static final DDCommandResult TOO_MANY_ARGUMENTS = new DDCommandResult(2, "Error: Too many arguments passed to the command", true); public static final DDCommandResult INVALID_DIMENSION_ID = new DDCommandResult(3, "Error: Invalid dimension ID", true); public static final DDCommandResult UNREGISTERED_DIMENSION = new DDCommandResult(4, "Error: Dimension is not registered", false); + public static final DDCommandResult INVALID_ARGUMENTS = new DDCommandResult(5, "Error: Invalid arguments passed to the command.", true); public static final int CUSTOM_ERROR_CODE = -1; diff --git a/StevenDimDoors/mod_pocketDim/helpers/dimHelper.java b/StevenDimDoors/mod_pocketDim/helpers/dimHelper.java index bf30fa1..e5e357a 100644 --- a/StevenDimDoors/mod_pocketDim/helpers/dimHelper.java +++ b/StevenDimDoors/mod_pocketDim/helpers/dimHelper.java @@ -816,7 +816,7 @@ public class dimHelper extends DimensionManager public boolean resetPocket(DimData dimData) { //TODO: Should we add a check to see if the dimension is currently loaded? How could we check that? ~SenseiKiwi - if (getWorld(dimData.dimID) != null || !dimData.isPocket) + if (!dimData.isPocket || getWorld(dimData.dimID) != null) { return false; } @@ -836,6 +836,23 @@ public class dimHelper extends DimensionManager return true; } + public boolean pruneDimension(DimData dimData, boolean deleteFolder) + { + //TODO: Should we add a check to see if the dimension is currently loaded? How could we check that? ~SenseiKiwi + //TODO: All the logic for checking that this is an isolated pocket should be moved in here. + if (!dimData.isPocket || getWorld(dimData.dimID) != null) + { + return false; + } + dimList.remove(dimData.dimID); + if (deleteFolder) + { + File save = new File(getCurrentSaveRootDirectory() + "/DimensionalDoors/pocketDimID" + dimData.dimID); + DeleteFolder.deleteFolder(save); + } + return true; + } + /** * method called when the client disconnects/server stops to unregister dims. * @Return