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.
This commit is contained in:
@@ -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<Integer> dimsWithLinks = new ArrayList<Integer>();
|
||||
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<Integer> linkedDimensions = new HashSet<Integer>();
|
||||
Collection<DimData> allDims = new ArrayList<DimData>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user