From 196af5c2511bd5a826b0e8aec34ac53c4a6eea01 Mon Sep 17 00:00:00 2001 From: Emmanuel Bengio Date: Tue, 23 Aug 2016 13:14:19 -0400 Subject: [PATCH] add destroyAfterMission WorldGenerator flag --- .../DefaultWorldGeneratorImplementation.java | 8 ++- .../FlatWorldGeneratorImplementation.java | 8 +-- .../microsoft/Malmo/Utils/MapFileHelper.java | 53 +++++++++++++++++++ Schemas/MissionHandlers.xsd | 16 ++++++ 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/DefaultWorldGeneratorImplementation.java b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/DefaultWorldGeneratorImplementation.java index 165b504d5..4acf65d14 100755 --- a/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/DefaultWorldGeneratorImplementation.java +++ b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/DefaultWorldGeneratorImplementation.java @@ -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; @@ -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 { @@ -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 @@ -109,4 +106,5 @@ public String getErrorDetails() { return ""; // Don't currently have any error exit points. } + } \ No newline at end of file diff --git a/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/FlatWorldGeneratorImplementation.java b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/FlatWorldGeneratorImplementation.java index 62f382b57..b00a1a287 100755 --- a/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/FlatWorldGeneratorImplementation.java +++ b/Minecraft/src/main/java/com/microsoft/Malmo/MissionHandlers/FlatWorldGeneratorImplementation.java @@ -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; @@ -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 { @@ -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 diff --git a/Minecraft/src/main/java/com/microsoft/Malmo/Utils/MapFileHelper.java b/Minecraft/src/main/java/com/microsoft/Malmo/Utils/MapFileHelper.java index 76543908e..2338bed79 100755 --- a/Minecraft/src/main/java/com/microsoft/Malmo/Utils/MapFileHelper.java +++ b/Minecraft/src/main/java/com/microsoft/Malmo/Utils/MapFileHelper.java @@ -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; @@ -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 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); + } + } + } } \ No newline at end of file diff --git a/Schemas/MissionHandlers.xsd b/Schemas/MissionHandlers.xsd index 9b92493b2..97f71b782 100755 --- a/Schemas/MissionHandlers.xsd +++ b/Schemas/MissionHandlers.xsd @@ -94,6 +94,14 @@ + + + + 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. + + + @@ -119,6 +127,14 @@ + + + + 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. + + +