Finished Loading blacklist & forced chunks
This commit is contained in:
76
StevenDimDoors/mod_pocketDim/saving/BlacklistProcessor.java
Normal file
76
StevenDimDoors/mod_pocketDim/saving/BlacklistProcessor.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package StevenDimDoors.mod_pocketDim.saving;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
|
||||
import StevenDimDoors.mod_pocketDim.util.BaseConfigurationProcessor;
|
||||
import StevenDimDoors.mod_pocketDim.util.ConfigurationProcessingException;
|
||||
|
||||
public class BlacklistProcessor extends BaseConfigurationProcessor<List<Integer>>
|
||||
{
|
||||
|
||||
@Override
|
||||
public List<Integer> readFromStream(InputStream inputStream) throws ConfigurationProcessingException
|
||||
{
|
||||
try
|
||||
{
|
||||
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
|
||||
List<Integer> data = this.createBlacklistFromJson(reader);
|
||||
return data;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
throw new ConfigurationProcessingException("Could not read blacklist");
|
||||
}
|
||||
}
|
||||
|
||||
private List<Integer> createBlacklistFromJson(JsonReader reader) throws IOException
|
||||
{
|
||||
List<Integer> blacklist;
|
||||
blacklist = this.createIntListFromJson(reader);
|
||||
return blacklist;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(OutputStream outputStream, List<Integer> data) throws ConfigurationProcessingException
|
||||
{
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
Gson gson = gsonBuilder.setPrettyPrinting().create();
|
||||
|
||||
try
|
||||
{
|
||||
outputStream.write(gson.toJson(data).getBytes("UTF-8"));
|
||||
outputStream.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// not sure if this is kosher, we need it to explode, but not by throwing the IO exception.
|
||||
throw new ConfigurationProcessingException("Incorrectly formatted save data");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<Integer> createIntListFromJson(JsonReader reader) throws IOException
|
||||
{
|
||||
List<Integer> list = new ArrayList<Integer>();
|
||||
reader.beginArray();
|
||||
|
||||
while(reader.peek()!= JsonToken.END_ARRAY)
|
||||
{
|
||||
list.add(reader.nextInt());
|
||||
}
|
||||
reader.endArray();
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package StevenDimDoors.mod_pocketDim.saving;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -16,6 +17,7 @@ import StevenDimDoors.mod_pocketDim.dungeon.DungeonData;
|
||||
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack;
|
||||
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType;
|
||||
import StevenDimDoors.mod_pocketDim.helpers.DungeonHelper;
|
||||
import StevenDimDoors.mod_pocketDim.util.ConfigurationProcessingException;
|
||||
import StevenDimDoors.mod_pocketDim.util.FileFilters;
|
||||
import StevenDimDoors.mod_pocketDim.util.Point4D;
|
||||
|
||||
@@ -46,7 +48,14 @@ public class DDSaveHandler
|
||||
}
|
||||
|
||||
// Load the dimension blacklist
|
||||
// --insert code here--
|
||||
File blacklistFile = new File(basePath+"blacklist.txt");
|
||||
|
||||
if(blacklistFile.exists())
|
||||
{
|
||||
BlacklistProcessor blacklistReader = new BlacklistProcessor();
|
||||
List<Integer> blacklist = readBlacklist(blacklistFile,blacklistReader);
|
||||
PocketManager.createAndRegisterBlacklist(blacklist);
|
||||
}
|
||||
|
||||
// List any dimension data files and read each dimension
|
||||
DimDataProcessor reader = new DimDataProcessor();
|
||||
@@ -179,7 +188,7 @@ public class DDSaveHandler
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean saveAll(Iterable<? extends IPackable<PackedDimData>> dimensions) throws IOException
|
||||
public static boolean saveAll(Iterable<? extends IPackable<PackedDimData>> dimensions, List<Integer> blacklist) throws IOException
|
||||
{
|
||||
// Create the data directory for our dimensions
|
||||
// Don't catch exceptions here. If we can't create this folder,
|
||||
@@ -189,6 +198,9 @@ public class DDSaveHandler
|
||||
File basePathFile = new File(basePath);
|
||||
Files.createParentDirs(basePathFile);
|
||||
basePathFile.mkdir();
|
||||
|
||||
BlacklistProcessor blacklistReader = new BlacklistProcessor();
|
||||
writeBlacklist(blacklist, blacklistReader,basePath);
|
||||
|
||||
FileFilter dataFileFilter = new FileFilters.RegexFileFilter("dim_-?\\d+\\.txt");
|
||||
|
||||
@@ -212,6 +224,25 @@ public class DDSaveHandler
|
||||
return succeeded;
|
||||
}
|
||||
|
||||
private static boolean writeBlacklist(List<Integer> blacklist, BlacklistProcessor writer, String basePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
File tempFile = new File(basePath + "blacklist.tmp");
|
||||
File saveFile = new File(basePath + "blacklist.txt");
|
||||
writer.writeToFile(tempFile, blacklist);
|
||||
saveFile.delete();
|
||||
tempFile.renameTo(saveFile);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("Could not save blacklist. The following error occurred:");
|
||||
printException(e, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
private static boolean writeDimension(IPackable<PackedDimData> dimension, DimDataProcessor writer, String basePath)
|
||||
{
|
||||
try
|
||||
@@ -261,17 +292,29 @@ public class DDSaveHandler
|
||||
|
||||
//TODO - make this more robust
|
||||
public static DungeonData unpackDungeonData(PackedDungeonData packedDungeon)
|
||||
{
|
||||
DungeonPack pack;
|
||||
DungeonType type;
|
||||
|
||||
{
|
||||
for(DungeonData data : DungeonHelper.instance().getRegisteredDungeons())
|
||||
{
|
||||
if(data.schematicName().equals(packedDungeon.SchematicName))
|
||||
{
|
||||
//return data;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<Integer> readBlacklist(File blacklistFile, BlacklistProcessor reader)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
return reader.readFromFile(blacklistFile);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,9 +67,7 @@ public class DimDataProcessor extends BaseConfigurationProcessor<PackedDimData>
|
||||
{
|
||||
// not sure if this is kosher, we need it to explode, but not by throwing the IO exception.
|
||||
throw new ConfigurationProcessingException("Incorrectly formatted save data");
|
||||
}
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Nightmare method that takes a JsonReader pointed at a serialized instance of PackedDimData
|
||||
|
||||
Reference in New Issue
Block a user