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.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import StevenDimDoors.mod_pocketDim.DimData;
|
import StevenDimDoors.mod_pocketDim.DimData;
|
||||||
@@ -14,7 +16,7 @@ public class CommandPruneDimensions extends DDCommandBase
|
|||||||
|
|
||||||
private CommandPruneDimensions()
|
private CommandPruneDimensions()
|
||||||
{
|
{
|
||||||
super("dd-prune");
|
super("dd-prune", "['delete']");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CommandPruneDimensions instance()
|
public static CommandPruneDimensions instance()
|
||||||
@@ -28,36 +30,44 @@ public class CommandPruneDimensions extends DDCommandBase
|
|||||||
@Override
|
@Override
|
||||||
protected DDCommandResult processCommand(EntityPlayer sender, String[] command)
|
protected DDCommandResult processCommand(EntityPlayer sender, String[] command)
|
||||||
{
|
{
|
||||||
int numRemoved=0;
|
if (command.length > 1)
|
||||||
ArrayList<Integer> dimsWithLinks = new ArrayList<Integer>();
|
{
|
||||||
|
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>();
|
Collection<DimData> allDims = new ArrayList<DimData>();
|
||||||
allDims.addAll(dimHelper.dimList.values());
|
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);
|
||||||
{
|
|
||||||
dimsWithLinks.add(link.destDimID);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (LinkData link : dimHelper.instance.interDimLinkList.values())
|
||||||
|
{
|
||||||
|
linkedDimensions.add(link.destDimID);
|
||||||
}
|
}
|
||||||
for(LinkData link : dimHelper.instance.interDimLinkList.values())
|
for (DimData data : allDims)
|
||||||
{
|
{
|
||||||
if(!dimsWithLinks.contains(link.destDimID))
|
if (!linkedDimensions.contains(data.dimID))
|
||||||
{
|
{
|
||||||
dimsWithLinks.add(link.destDimID);
|
if (dimHelper.instance.pruneDimension(data, deleteFolders))
|
||||||
|
{
|
||||||
|
removedCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(DimData data : allDims)
|
|
||||||
{
|
|
||||||
if(!dimsWithLinks.contains(data.dimID))
|
|
||||||
{
|
|
||||||
dimHelper.dimList.remove(data.dimID);
|
|
||||||
numRemoved++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
dimHelper.instance.save();
|
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 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 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 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;
|
public static final int CUSTOM_ERROR_CODE = -1;
|
||||||
|
|
||||||
|
|||||||
@@ -816,7 +816,7 @@ public class dimHelper extends DimensionManager
|
|||||||
public boolean resetPocket(DimData dimData)
|
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
|
//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;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -836,6 +836,23 @@ public class dimHelper extends DimensionManager
|
|||||||
return true;
|
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.
|
* method called when the client disconnects/server stops to unregister dims.
|
||||||
* @Return
|
* @Return
|
||||||
|
|||||||
Reference in New Issue
Block a user