Skip to content

Commit

Permalink
add destroyAfterMission WorldGenerator flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bengioe committed Aug 23, 2016
1 parent c76ce40 commit 196af5c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package com.microsoft.Malmo.MissionHandlers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import net.minecraft.client.Minecraft;
Expand All @@ -33,6 +31,7 @@
import com.microsoft.Malmo.MissionHandlerInterfaces.IWorldGenerator;
import com.microsoft.Malmo.Schemas.DefaultWorldGenerator;
import com.microsoft.Malmo.Schemas.MissionInit;
import com.microsoft.Malmo.Utils.MapFileHelper;

public class DefaultWorldGeneratorImplementation extends HandlerBase implements IWorldGenerator
{
Expand Down Expand Up @@ -78,9 +77,7 @@ public boolean createWorld(MissionInit missionInit)
worldsettings.enableCommands();
// Create a filename for this map - we use the time stamp to make sure it is different from other worlds, otherwise no new world
// will be created, it will simply load the old one.
String s = SimpleDateFormat.getDateTimeInstance().format(new Date()).replace(":", "_");
Minecraft.getMinecraft().launchIntegratedServer(s, s, worldsettings);
return true;
return MapFileHelper.createAndLaunchWorld(worldsettings, this.dwparams.isDestroyAfterMission());
}

@Override
Expand Down Expand Up @@ -109,4 +106,5 @@ public String getErrorDetails()
{
return ""; // Don't currently have any error exit points.
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

package com.microsoft.Malmo.MissionHandlers;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
Expand All @@ -32,6 +29,7 @@
import com.microsoft.Malmo.MissionHandlerInterfaces.IWorldGenerator;
import com.microsoft.Malmo.Schemas.FlatWorldGenerator;
import com.microsoft.Malmo.Schemas.MissionInit;
import com.microsoft.Malmo.Utils.MapFileHelper;

public class FlatWorldGeneratorImplementation extends HandlerBase implements IWorldGenerator
{
Expand All @@ -58,9 +56,7 @@ public boolean createWorld(MissionInit missionInit)
worldsettings.enableCommands(); // Enables cheat commands.
// Create a filename for this map - we use the time stamp to make sure it is different from other worlds, otherwise no new world
// will be created, it will simply load the old one.
String s = SimpleDateFormat.getDateTimeInstance().format(new Date()).replace(":", "_");
Minecraft.getMinecraft().launchIntegratedServer(s, s, worldsettings);
return true;
return MapFileHelper.createAndLaunchWorld(worldsettings, this.fwparams.isDestroyAfterMission());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.minecraft.client.AnvilConverterException;
import net.minecraft.client.Minecraft;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.storage.ISaveFormat;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraft.world.storage.SaveFormatComparator;
import net.minecraft.world.storage.SaveHandler;
import net.minecraft.world.storage.WorldInfo;

import org.apache.commons.io.FileUtils;

Expand Down Expand Up @@ -75,4 +87,45 @@ static public File copyMapFiles(File mapFile, boolean overwriteOldFiles)

return dst;
}

/**
* Creates and launches a unique world according to the settings.
* @param worldsettings the world's settings
* @param isTemporary if true, the world will be deleted whenever newer worlds are created
* @return
*/
public static boolean createAndLaunchWorld(WorldSettings worldsettings, boolean isTemporary) {

String s = SimpleDateFormat.getDateTimeInstance().format(new Date()).replace(":", "_");
if (isTemporary){
s = "TEMP_"+s;
}
Minecraft.getMinecraft().launchIntegratedServer(s, s, worldsettings);
cleanupTemporaryWorlds(s);
return true;
}

/**
* Attempts to delete all Minecraft Worlds with "TEMP_" in front of the name
* @param currentWorld excludes this world from deletion, can be null
*/
public static void cleanupTemporaryWorlds(String currentWorld){
List<SaveFormatComparator> saveList;
ISaveFormat isaveformat = Minecraft.getMinecraft().getSaveLoader();
isaveformat.flushCache();

try{
saveList = isaveformat.getSaveList();
} catch (AnvilConverterException e){
e.printStackTrace();
return;
}

for (SaveFormatComparator s: saveList){
String folderName = s.getFileName();
if (folderName.startsWith("TEMP_") && !folderName.equals(currentWorld)){
isaveformat.deleteWorldDirectory(folderName);
}
}
}
}
16 changes: 16 additions & 0 deletions Schemas/MissionHandlers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="destroyAfterMission" use="optional" type="xs:boolean" default="false">
<xs:annotation>
<xs:documentation>
Set this to true to force the world data files to be deleted after the mission is done.
Enabling this setting prevents the disk being filled with old worlds.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>

Expand All @@ -119,6 +127,14 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="destroyAfterMission" use="optional" type="xs:boolean" default="false">
<xs:annotation>
<xs:documentation>
Set this to true to force the world data files to be deleted after the mission is done.
Enabling this setting prevents the disk being filled with old worlds.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>

Expand Down

0 comments on commit 196af5c

Please sign in to comment.