From e3df3cad0852a54fd834dcfb86bbc54cc6334d32 Mon Sep 17 00:00:00 2001 From: StevenRS11 Date: Wed, 6 Nov 2013 14:27:55 -0500 Subject: [PATCH] Finished Loading blacklist & forced chunks --- StevenDimDoors/mod_pocketDim/DimData.java | 2 +- .../mod_pocketDim/DungeonGenerator.java | 1 + StevenDimDoors/mod_pocketDim/LinkData.java | 2 +- .../mod_pocketDim/core/DimLink.java | 2 +- .../mod_pocketDim/core/PocketManager.java | 27 ++++++- .../helpers/ChunkLoaderHelper.java | 29 +++++++ .../mod_pocketDim/mod_pocketDim.java | 2 + .../saving/BlacklistProcessor.java | 76 +++++++++++++++++++ .../mod_pocketDim/saving/DDSaveHandler.java | 57 ++++++++++++-- .../saving/DimDataProcessor.java | 4 +- .../mod_pocketDim/world/ItemWorldThread.java | 6 ++ 11 files changed, 192 insertions(+), 16 deletions(-) create mode 100644 StevenDimDoors/mod_pocketDim/saving/BlacklistProcessor.java create mode 100644 StevenDimDoors/mod_pocketDim/world/ItemWorldThread.java diff --git a/StevenDimDoors/mod_pocketDim/DimData.java b/StevenDimDoors/mod_pocketDim/DimData.java index 7bf4ed06..76481618 100644 --- a/StevenDimDoors/mod_pocketDim/DimData.java +++ b/StevenDimDoors/mod_pocketDim/DimData.java @@ -10,7 +10,7 @@ import java.util.Iterator; import net.minecraft.util.MathHelper; import net.minecraft.world.World; - +@Deprecated public class DimData implements Serializable { public int dimID; diff --git a/StevenDimDoors/mod_pocketDim/DungeonGenerator.java b/StevenDimDoors/mod_pocketDim/DungeonGenerator.java index e8ec50c7..021f6799 100644 --- a/StevenDimDoors/mod_pocketDim/DungeonGenerator.java +++ b/StevenDimDoors/mod_pocketDim/DungeonGenerator.java @@ -9,6 +9,7 @@ import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack; import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType; import StevenDimDoors.mod_pocketDim.helpers.DungeonHelper; +@Deprecated public class DungeonGenerator implements Serializable { //This static field is hax so that I don't have to add an instance field to DungeonGenerator to support DungeonType. diff --git a/StevenDimDoors/mod_pocketDim/LinkData.java b/StevenDimDoors/mod_pocketDim/LinkData.java index 62130292..04a0bd60 100644 --- a/StevenDimDoors/mod_pocketDim/LinkData.java +++ b/StevenDimDoors/mod_pocketDim/LinkData.java @@ -1,7 +1,7 @@ package StevenDimDoors.mod_pocketDim; import java.io.Serializable; - +@Deprecated public class LinkData implements Serializable { diff --git a/StevenDimDoors/mod_pocketDim/core/DimLink.java b/StevenDimDoors/mod_pocketDim/core/DimLink.java index 8c11bb7a..9f1369d8 100644 --- a/StevenDimDoors/mod_pocketDim/core/DimLink.java +++ b/StevenDimDoors/mod_pocketDim/core/DimLink.java @@ -4,7 +4,7 @@ import java.util.LinkedList; import java.util.List; import StevenDimDoors.mod_pocketDim.util.Point4D; - +@Deprecated public abstract class DimLink { protected Point4D source; diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index 79b11304..63eec3b5 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -372,6 +372,18 @@ public class PocketManager } } } + for(Integer dimID : dimensionIDBlackList) + { + try + { + DimensionManager.unregisterDimension(dimID); + } + catch (Exception e) + { + System.err.println("An unexpected error occurred while unregistering blacklisted dim #" + dimID + ":"); + e.printStackTrace(); + } + } } /** @@ -384,7 +396,8 @@ public class PocketManager File saveDir = DimensionManager.getCurrentSaveRootDirectory(); if (saveDir != null) { - // Load and register blacklisted dimension IDs + //Try to import data from old DD versions + //TODO - remove this code in a few versions File oldSaveData = new File(saveDir+"/DimensionalDoorsData"); if(oldSaveData.exists()) { @@ -395,7 +408,6 @@ public class PocketManager oldSaveData.delete(); System.out.println("Import Succesful!"); - } catch (Exception e) { @@ -436,7 +448,7 @@ public class PocketManager try { System.out.println("Writing Dimensional Doors save data..."); - if ( DDSaveHandler.saveAll(dimensionData.values()) ) + if ( DDSaveHandler.saveAll(dimensionData.values(),dimensionIDBlackList) ) { System.out.println("Saved successfully!"); } @@ -648,6 +660,15 @@ public class PocketManager return dimensionData.containsKey(dimensionID); } + public static void createAndRegisterBlacklist(List blacklist) + { + //TODO - create a special blacklist provider + for(Integer dimID : blacklist) + { + PocketManager.dimensionIDBlackList.add(dimID); + DimensionManager.registerDimension(dimID, DDProperties.instance().PocketProviderID); + } + } public static void readPacket(DataInputStream input) throws IOException { if (isLoaded) diff --git a/StevenDimDoors/mod_pocketDim/helpers/ChunkLoaderHelper.java b/StevenDimDoors/mod_pocketDim/helpers/ChunkLoaderHelper.java index 825f13ef..9c00f58a 100644 --- a/StevenDimDoors/mod_pocketDim/helpers/ChunkLoaderHelper.java +++ b/StevenDimDoors/mod_pocketDim/helpers/ChunkLoaderHelper.java @@ -1,17 +1,23 @@ package StevenDimDoors.mod_pocketDim.helpers; +import java.io.File; import java.util.List; import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.ForgeChunkManager; import net.minecraftforge.common.ForgeChunkManager.LoadingCallback; import net.minecraftforge.common.ForgeChunkManager.Ticket; import StevenDimDoors.mod_pocketDim.IChunkLoader; import StevenDimDoors.mod_pocketDim.mod_pocketDim; +import StevenDimDoors.mod_pocketDim.core.NewDimData; +import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityDimDoorGold; import com.google.common.collect.Lists; +import cpw.mods.fml.common.event.FMLServerStartingEvent; + public class ChunkLoaderHelper implements LoadingCallback { @@ -29,4 +35,27 @@ public class ChunkLoaderHelper implements LoadingCallback } } + + public static void loadChunkForcedWorlds(FMLServerStartingEvent event) + { + for(NewDimData data : PocketManager.getDimensions()) + { + if(data.isPocketDimension()) + { + String chunkDir = DimensionManager.getCurrentSaveRootDirectory()+"/DimensionalDoors/pocketDimID" + data.id(); + + File file = new File(chunkDir); + + if(file.exists()) + { + if(ForgeChunkManager.savedWorldHasForcedChunkTickets(file)) + { + PocketManager.loadDimension(data.id()); + } + } + + } + } + + } } \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/mod_pocketDim.java b/StevenDimDoors/mod_pocketDim/mod_pocketDim.java index bb9d173e..2ee71b90 100644 --- a/StevenDimDoors/mod_pocketDim/mod_pocketDim.java +++ b/StevenDimDoors/mod_pocketDim/mod_pocketDim.java @@ -424,5 +424,7 @@ public class mod_pocketDim CommandCreatePocket.instance().register(event); CommandTeleportPlayer.instance().register(event); + + ChunkLoaderHelper.loadChunkForcedWorlds(event); } } diff --git a/StevenDimDoors/mod_pocketDim/saving/BlacklistProcessor.java b/StevenDimDoors/mod_pocketDim/saving/BlacklistProcessor.java new file mode 100644 index 00000000..da1cb3b6 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/saving/BlacklistProcessor.java @@ -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> +{ + + @Override + public List readFromStream(InputStream inputStream) throws ConfigurationProcessingException + { + try + { + JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8")); + List data = this.createBlacklistFromJson(reader); + return data; + } + catch (IOException e) + { + e.printStackTrace(); + throw new ConfigurationProcessingException("Could not read blacklist"); + } + } + + private List createBlacklistFromJson(JsonReader reader) throws IOException + { + List blacklist; + blacklist = this.createIntListFromJson(reader); + return blacklist; + } + + @Override + public void writeToStream(OutputStream outputStream, List 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 createIntListFromJson(JsonReader reader) throws IOException + { + List list = new ArrayList(); + reader.beginArray(); + + while(reader.peek()!= JsonToken.END_ARRAY) + { + list.add(reader.nextInt()); + } + reader.endArray(); + return list; + } + +} diff --git a/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java b/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java index d6c1e5ac..e821b57b 100644 --- a/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java +++ b/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java @@ -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 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> dimensions) throws IOException + public static boolean saveAll(Iterable> dimensions, List 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 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 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 readBlacklist(File blacklistFile, BlacklistProcessor reader) + { + + try + { + return reader.readFromFile(blacklistFile); + } + catch (Exception e) + { + e.printStackTrace(); + return null; + } + + } } diff --git a/StevenDimDoors/mod_pocketDim/saving/DimDataProcessor.java b/StevenDimDoors/mod_pocketDim/saving/DimDataProcessor.java index 7be35fa3..55a01d9a 100644 --- a/StevenDimDoors/mod_pocketDim/saving/DimDataProcessor.java +++ b/StevenDimDoors/mod_pocketDim/saving/DimDataProcessor.java @@ -67,9 +67,7 @@ public class DimDataProcessor extends BaseConfigurationProcessor { // 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 diff --git a/StevenDimDoors/mod_pocketDim/world/ItemWorldThread.java b/StevenDimDoors/mod_pocketDim/world/ItemWorldThread.java new file mode 100644 index 00000000..8c4a55fd --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/world/ItemWorldThread.java @@ -0,0 +1,6 @@ +package StevenDimDoors.mod_pocketDim.world; + +public class ItemWorldThread +{ + +}