diff --git a/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java b/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java
index b4ed0bb3c..fef560f41 100644
--- a/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java
+++ b/src/main/java/com/seibel/lod/handlers/LodDimensionFileHandler.java
@@ -17,29 +17,30 @@
*/
package com.seibel.lod.handlers;
-import com.seibel.lod.objects.*;
-import com.seibel.lod.proxy.ClientProxy;
-import com.seibel.lod.util.LodThreadFactory;
-import com.seibel.lod.util.LodUtil;
-import net.minecraft.util.FileUtil;
-import org.apache.commons.io.FileUtils;
-
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import com.seibel.lod.objects.LevelContainer;
+import com.seibel.lod.objects.LodDimension;
+import com.seibel.lod.objects.LodRegion;
+import com.seibel.lod.objects.RegionPos;
+import com.seibel.lod.proxy.ClientProxy;
+import com.seibel.lod.util.LodThreadFactory;
+
/**
* This object handles creating LodRegions
* from files and saving LodRegion objects
* to file.
*
* @author James Seibel
- * @version 8-14-2021
+ * @version 8-22-2021
*/
public class LodDimensionFileHandler
{
@@ -116,83 +117,93 @@ public class LodDimensionFileHandler
* (null if the file doesn't exist)
*/
public LodRegion loadRegionFromFile(RegionPos regionPos)
- {
- int regionX = regionPos.x;
- int regionZ = regionPos.z;
- String fileName = getFileNameAndPathForRegion(regionX, regionZ);
-
- File f = new File(fileName);
-
- if (!f.exists())
- {
- // there wasn't a file, don't
- // return anything
- return null;
- }
- String data = "";
- try
- {
- BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
- data = bufferedReader.readLine();
- int fileVersion = -1;
-
- if (data != null && !data.isEmpty())
- {
- // try to get the file version
- try
- {
- fileVersion = Integer.parseInt(data.substring(data.indexOf(' ')).trim());
- } catch (NumberFormatException | StringIndexOutOfBoundsException e)
- {
- // this file doesn't have a version
- // keep the version as -1
- fileVersion = -1;
- }
-
- // check if this file can be read by this file handler
- if (fileVersion < LOD_SAVE_FILE_VERSION)
- {
- // the file we are reading is an older version,
- // close the reader and delete the file.
- bufferedReader.close();
- f.delete();
- ClientProxy.LOGGER.info("Outdated LOD region file for region: (" + regionX + "," + regionZ + ") version: " + fileVersion +
- ", version requested: " + LOD_SAVE_FILE_VERSION +
- " File was been deleted.");
-
- return null;
- } else if (fileVersion > LOD_SAVE_FILE_VERSION)
- {
- // the file we are reading is a newer version,
- // close the reader and ignore the file, we don't
- // want to accidently delete anything the user may want.
- bufferedReader.close();
- ClientProxy.LOGGER.info("Newer LOD region file for region: (" + regionX + "," + regionZ + ") version: " + fileVersion +
- ", version requested: " + LOD_SAVE_FILE_VERSION +
- " this region will not be written to in order to protect the newer file.");
-
- return null;
- }
- } else
- {
- // there is no data in this file
- bufferedReader.close();
- return null;
- }
-
-
- // this file is a readable version, begin reading the file
- data = bufferedReader.readLine();
-
- bufferedReader.close();
- } catch (IOException e)
- {
- // the buffered reader encountered a
- // problem reading the file
- return null;
- }
- return new LodRegion(new LevelContainer(data), regionPos);
- }
+ {
+ int regionX = regionPos.x;
+ int regionZ = regionPos.z;
+ String fileName = getFileNameAndPathForRegion(regionX, regionZ);
+
+ // if the fileName was null that means the folder is inaccessible
+ // for some reason
+ if (fileName == null)
+ return null;
+
+
+ File f = new File(fileName);
+
+ if (!f.exists())
+ {
+ // there wasn't a file, don't
+ // return anything
+ return null;
+ }
+ String data = "";
+ try
+ {
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
+ data = bufferedReader.readLine();
+ int fileVersion = -1;
+
+ if (data != null && !data.isEmpty())
+ {
+ // try to get the file version
+ try
+ {
+ fileVersion = Integer.parseInt(data.substring(data.indexOf(' ')).trim());
+ }
+ catch (NumberFormatException | StringIndexOutOfBoundsException e)
+ {
+ // this file doesn't have a version
+ // keep the version as -1
+ fileVersion = -1;
+ }
+
+ // check if this file can be read by this file handler
+ if (fileVersion < LOD_SAVE_FILE_VERSION)
+ {
+ // the file we are reading is an older version,
+ // close the reader and delete the file.
+ bufferedReader.close();
+ f.delete();
+ ClientProxy.LOGGER.info("Outdated LOD region file for region: (" + regionX + "," + regionZ + ") version: " + fileVersion +
+ ", version requested: " + LOD_SAVE_FILE_VERSION +
+ " File was been deleted.");
+
+ return null;
+ }
+ else if (fileVersion > LOD_SAVE_FILE_VERSION)
+ {
+ // the file we are reading is a newer version,
+ // close the reader and ignore the file, we don't
+ // want to accidently delete anything the user may want.
+ bufferedReader.close();
+ ClientProxy.LOGGER.info("Newer LOD region file for region: (" + regionX + "," + regionZ + ") version: " + fileVersion +
+ ", version requested: " + LOD_SAVE_FILE_VERSION +
+ " this region will not be written to in order to protect the newer file.");
+
+ return null;
+ }
+ }
+ else
+ {
+ // there is no data in this file
+ bufferedReader.close();
+ return null;
+ }
+
+ // this file is a readable version, begin reading the file
+ data = bufferedReader.readLine();
+
+ bufferedReader.close();
+ }
+ catch (IOException e)
+ {
+ // the buffered reader encountered a
+ // problem reading the file
+ return null;
+ }
+
+ return new LodRegion(new LevelContainer(data), regionPos);
+ }
//==============//
@@ -232,82 +243,91 @@ public class LodDimensionFileHandler
*/
private void saveRegionToFile(LodRegion region)
{
- // convert to region coordinates
- int x = region.regionPosX;
- int z = region.regionPosZ;
-
- // get minimum level
-
- byte minDetailLevel = region.getMinDetailLevel();
-
- File oldFile = new File(getFileNameAndPathForRegion(x, z));
- try
- {
- // make sure the file and folder exists
- if (!oldFile.exists())
- {
- // the file doesn't exist,
- // create it and the folder if need be
- if (!oldFile.getParentFile().exists())
- oldFile.getParentFile().mkdirs();
- oldFile.createNewFile();
- } else
- {
- // the file exists, make sure it
- // is the correct version.
- // (to make sure we don't overwrite a newer
- // version file if it exists)
-
- BufferedReader br = new BufferedReader(new FileReader(oldFile));
- String s = br.readLine();
- int fileVersion = LOD_SAVE_FILE_VERSION;
-
- if (s != null && !s.isEmpty())
- {
- // try to get the file version
- try
- {
- fileVersion = Integer.parseInt(s.substring(s.indexOf(' ')).trim());
- } catch (NumberFormatException | StringIndexOutOfBoundsException e)
- {
- // this file doesn't have a correctly formated version
- // just overwrite the file
- }
- }
- br.close();
-
- // check if this file can be written to by the file handler
- if (fileVersion <= LOD_SAVE_FILE_VERSION)
- {
- // we are good to continue and overwrite the old file
- } else //if(fileVersion > LOD_SAVE_FILE_VERSION)
- {
- // the file we are reading is a newer version,
- // don't write anything, we don't want to accidently
- // delete anything the user may want.
- return;
- }
- }
-
- // the old file is good, now create a new save file
- File newFile = new File(getFileNameAndPathForRegion(x, z) + TMP_FILE_EXTENSION);
-
- FileWriter fw = new FileWriter(newFile);
-
- // add the version of this file
- fw.write(LOD_FILE_VERSION_PREFIX + " " + LOD_SAVE_FILE_VERSION + "\n");
-
- // add each LodChunk to the file
- fw.write(region.getLevel((byte) 0).toString());
- fw.close();
-
- // overwrite the old file with the new one
- Files.move(newFile.toPath(), oldFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
- } catch (Exception e)
- {
- ClientProxy.LOGGER.error("LOD file write error: ");
- e.printStackTrace();
- }
+ // convert to region coordinates
+ int x = region.regionPosX;
+ int z = region.regionPosZ;
+ String fileName = getFileNameAndPathForRegion(x, z);
+ File oldFile = new File(fileName);
+
+ // if the fileName was null that means the folder is inaccessible
+ // for some reason
+ if (fileName == null)
+ {
+ ClientProxy.LOGGER.warn("Unable to save region [" + x + ", " + z + "] to file." );
+ return;
+ }
+
+
+ try
+ {
+ // make sure the file and folder exists
+ if (!oldFile.exists())
+ {
+ // the file doesn't exist,
+ // create it and the folder if need be
+ if (!oldFile.getParentFile().exists())
+ oldFile.getParentFile().mkdirs();
+ oldFile.createNewFile();
+ }
+ else
+ {
+ // the file exists, make sure it
+ // is the correct version.
+ // (to make sure we don't overwrite a newer
+ // version file if it exists)
+
+ BufferedReader br = new BufferedReader(new FileReader(oldFile));
+ String s = br.readLine();
+ int fileVersion = LOD_SAVE_FILE_VERSION;
+
+ if (s != null && !s.isEmpty())
+ {
+ // try to get the file version
+ try
+ {
+ fileVersion = Integer.parseInt(s.substring(s.indexOf(' ')).trim());
+ }
+ catch (NumberFormatException | StringIndexOutOfBoundsException e)
+ {
+ // this file doesn't have a correctly formated version
+ // just overwrite the file
+ }
+ }
+ br.close();
+
+ // check if this file can be written to by the file handler
+ if (fileVersion <= LOD_SAVE_FILE_VERSION)
+ {
+ // we are good to continue and overwrite the old file
+ }
+ else // if(fileVersion > LOD_SAVE_FILE_VERSION)
+ {
+ // the file we are reading is a newer version,
+ // don't write anything, we don't want to accidently
+ // delete anything the user may want.
+ return;
+ }
+ }
+
+ // the old file is good, now create a new save file
+ File newFile = new File(fileName + TMP_FILE_EXTENSION);
+ FileWriter fw = new FileWriter(newFile);
+
+ // add the version of this file
+ fw.write(LOD_FILE_VERSION_PREFIX + " " + LOD_SAVE_FILE_VERSION + "\n");
+
+ // add each LodChunk to the file
+ fw.write(region.getLevel((byte) 0).toString());
+ fw.close();
+
+ // overwrite the old file with the new one
+ Files.move(newFile.toPath(), oldFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
+ }
+ catch (Exception e)
+ {
+ ClientProxy.LOGGER.error("LOD file write error: ");
+ e.printStackTrace();
+ }
}
@@ -320,8 +340,10 @@ public class LodDimensionFileHandler
* Return the name of the file that should contain the
* region at the given x and z.
* Returns null if this object isn't ready to read and write.
- *
- * example: "lod.0.0.txt"
+ *
+ * example: "lod.0.0.txt"
+ *
+ * Returns null if there is an IO Exception.
*/
private String getFileNameAndPathForRegion(int regionX, int regionZ)
{
@@ -333,8 +355,11 @@ public class LodDimensionFileHandler
// ".\Super Flat\data"
return dimensionDataSaveFolder.getCanonicalPath() + File.separatorChar +
FILE_NAME_PREFIX + "." + regionX + "." + regionZ + FILE_EXTENSION;
- } catch (IOException e)
+ }
+ catch (IOException | SecurityException e)
{
+ ClientProxy.LOGGER.warn("Unable to get the filename for the region [" + regionX + ", " + regionZ + "], error: [" + e.getMessage() + "], stacktrace: ");
+ e.printStackTrace();
return null;
}
}