diff --git a/src/main/java/serverutils/ServerUtilitiesConfig.java b/src/main/java/serverutils/ServerUtilitiesConfig.java index a32955b9..761e1ef4 100644 --- a/src/main/java/serverutils/ServerUtilitiesConfig.java +++ b/src/main/java/serverutils/ServerUtilitiesConfig.java @@ -276,7 +276,7 @@ public static class Commands { @Config.DefaultBoolean(true) public boolean dump_stats; - + @Config.DefaultBoolean(true) public boolean pregen; } diff --git a/src/main/java/serverutils/command/pregen/CmdPregen.java b/src/main/java/serverutils/command/pregen/CmdPregen.java index 0041470a..4bcfb26a 100644 --- a/src/main/java/serverutils/command/pregen/CmdPregen.java +++ b/src/main/java/serverutils/command/pregen/CmdPregen.java @@ -4,6 +4,7 @@ import serverutils.lib.command.CmdTreeHelp; public class CmdPregen extends CmdTreeBase { + public CmdPregen() { super("pregen"); addSubcommand(new CmdProgress()); diff --git a/src/main/java/serverutils/command/pregen/CmdProgress.java b/src/main/java/serverutils/command/pregen/CmdProgress.java index 0b3d1160..3abb2d78 100644 --- a/src/main/java/serverutils/command/pregen/CmdProgress.java +++ b/src/main/java/serverutils/command/pregen/CmdProgress.java @@ -2,23 +2,21 @@ import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; + import serverutils.lib.command.CmdBase; import serverutils.pregenerator.ChunkLoaderManager; public class CmdProgress extends CmdBase { + public CmdProgress() { super("progress", Level.OP); } @Override - public void processCommand(ICommandSender sender, String[] args) - { - if (ChunkLoaderManager.instance.isGenerating()) - { + public void processCommand(ICommandSender sender, String[] args) { + if (ChunkLoaderManager.instance.isGenerating()) { sender.addChatMessage(new ChatComponentText(ChunkLoaderManager.instance.progressString())); - } - else - { + } else { sender.addChatMessage(new ChatComponentText("No generator running.")); } } diff --git a/src/main/java/serverutils/command/pregen/CmdStart.java b/src/main/java/serverutils/command/pregen/CmdStart.java index 88a077f0..cbd51535 100644 --- a/src/main/java/serverutils/command/pregen/CmdStart.java +++ b/src/main/java/serverutils/command/pregen/CmdStart.java @@ -1,86 +1,77 @@ package serverutils.command.pregen; +import java.io.IOException; +import java.util.Objects; + import net.minecraft.command.ICommandSender; import net.minecraft.command.WrongUsageException; import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChatComponentText; + import serverutils.lib.command.CmdBase; import serverutils.lib.util.misc.PregeneratorCommandInfo; import serverutils.pregenerator.ChunkLoaderManager; -import java.io.IOException; -import java.util.Objects; - public class CmdStart extends CmdBase { + public CmdStart() { super("start", Level.OP); } - + @Override - public void processCommand(ICommandSender sender, String[] args) - { + public void processCommand(ICommandSender sender, String[] args) { checkArgs(sender, args, 3); - if (args.length > 2) - { + if (args.length > 2) { double xLoc, zLoc; - if (args[0].equals("~")) - { + if (args[0].equals("~")) { xLoc = sender.getPlayerCoordinates().posX; - } - else - { + } else { xLoc = parseDoubleBounded(sender, args[0], -30000000.0D, 30000000.0D); } - if (args[1].equals("~")) - { + if (args[1].equals("~")) { zLoc = sender.getPlayerCoordinates().posZ; - } - else - { + } else { zLoc = parseDoubleBounded(sender, args[1], -30000000.0D, 30000000.0D); } int radius = parseInt(sender, args[2]); - if (radius > 2000) - { - sender.addChatMessage(new ChatComponentText("Radii larger than 2000 are not permitted. World sizes will be 100's of gbs")); + if (radius > 2000) { + sender.addChatMessage( + new ChatComponentText( + "Radii larger than 2000 are not permitted. World sizes will be 100's of gbs")); return; } int dimensionID = sender.getEntityWorld().provider.dimensionId; PregeneratorCommandInfo commandInfo = new PregeneratorCommandInfo(xLoc, zLoc, radius, dimensionID); - if (!ChunkLoaderManager.instance.isGenerating()) - { - try - { + if (!ChunkLoaderManager.instance.isGenerating()) { + try { + sender.addChatMessage( + new ChatComponentText( + "Initializing pregenerator. Check progress with '/pregen progress'.")); ChunkLoaderManager.instance.initializePregenerator(commandInfo, MinecraftServer.getServer()); - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); - sender.addChatMessage(new ChatComponentText("Cannot start a pregenerator! File exception when starting pregenerator!")); + sender.addChatMessage( + new ChatComponentText( + "Cannot start a pregenerator! File exception when starting pregenerator!")); } - } - else - { - sender.addChatMessage(new ChatComponentText("Cannot start a pregenerator! There's already generation in progress!")); + } else { + sender.addChatMessage( + new ChatComponentText("Cannot start a pregenerator! There's already generation in progress!")); } } } - + @Override - public void checkArgs(ICommandSender sender, String[] args, int i) - { + public void checkArgs(ICommandSender sender, String[] args, int i) { if (args.length < i) { throw new WrongUsageException(getCommandUsage(sender)); } - if (sender instanceof MinecraftServer) - { - for (int j = 0; i < args.length; i++) - { - if (Objects.equals(args[i], "~")) - { + if (sender instanceof MinecraftServer) { + for (int j = 0; j < args.length; j++) { + if (Objects.equals(args[i], "~")) { throw new WrongUsageException(getCommandUsage(sender)); } } diff --git a/src/main/java/serverutils/command/pregen/CmdStop.java b/src/main/java/serverutils/command/pregen/CmdStop.java index 69bf64f3..efe1086d 100644 --- a/src/main/java/serverutils/command/pregen/CmdStop.java +++ b/src/main/java/serverutils/command/pregen/CmdStop.java @@ -2,24 +2,22 @@ import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; + import serverutils.lib.command.CmdBase; import serverutils.pregenerator.ChunkLoaderManager; public class CmdStop extends CmdBase { + public CmdStop() { super("stop", Level.OP); } @Override - public void processCommand(ICommandSender sender, String[] args) - { - if (ChunkLoaderManager.instance.isGenerating()) - { + public void processCommand(ICommandSender sender, String[] args) { + if (ChunkLoaderManager.instance.isGenerating()) { ChunkLoaderManager.instance.reset(true); sender.addChatMessage(new ChatComponentText("Cancelling pregeneration.")); - } - else - { + } else { sender.addChatMessage(new ChatComponentText("No generator running.")); } } diff --git a/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java b/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java index 3f422865..4724c451 100644 --- a/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java +++ b/src/main/java/serverutils/handlers/ServerUtilitiesServerEventHandler.java @@ -229,8 +229,7 @@ public void onServerTick(TickEvent.ServerTickEvent event) { .onDisconnect(new ChatComponentTranslation("multiplayer.disconnect.idling")); } - if (ChunkLoaderManager.instance.isGenerating()) - { + if (ChunkLoaderManager.instance.isGenerating()) { ChunkLoaderManager.instance.queueChunks(1); } } diff --git a/src/main/java/serverutils/handlers/ServerUtilitiesWorldEventHandler.java b/src/main/java/serverutils/handlers/ServerUtilitiesWorldEventHandler.java index 3bc081cc..d5d00039 100644 --- a/src/main/java/serverutils/handlers/ServerUtilitiesWorldEventHandler.java +++ b/src/main/java/serverutils/handlers/ServerUtilitiesWorldEventHandler.java @@ -99,17 +99,14 @@ public void onExplosionDetonate(ExplosionEvent.Detonate event) { @SubscribeEvent public void onWorldLoad(WorldEvent.Load event) { - if (!event.world.isRemote) - { + if (!event.world.isRemote) { int dimensionId = event.world.provider.dimensionId; MinecraftServer server = MinecraftServer.getServer(); - if (!ChunkLoaderManager.instance.isGenerating() && !ChunkLoaderManager.instance.initializeFromPregeneratorFiles(server, dimensionId)) - { + if (!ChunkLoaderManager.instance.isGenerating() + && !ChunkLoaderManager.instance.initializeFromPregeneratorFiles(server, dimensionId)) { ChunkLoaderManager.instance.reset(false); System.out.println("No pregenerator to load for dimension Id: " + dimensionId); - } - else - { + } else { System.out.println("Pregenerator loaded and running for dimension Id: " + dimensionId); } } @@ -117,10 +114,9 @@ public void onWorldLoad(WorldEvent.Load event) { @SubscribeEvent public void onWorldUnload(WorldEvent.Unload event) { - if (!event.world.isRemote) - { - if (event.world.provider.dimensionId == ChunkLoaderManager.instance.getDimensionID() && ChunkLoaderManager.instance.isGenerating()) - { + if (!event.world.isRemote) { + if (event.world.provider.dimensionId == ChunkLoaderManager.instance.getDimensionID() + && ChunkLoaderManager.instance.isGenerating()) { ChunkLoaderManager.instance.reset(false); } } diff --git a/src/main/java/serverutils/lib/util/misc/PregeneratorCommandInfo.java b/src/main/java/serverutils/lib/util/misc/PregeneratorCommandInfo.java index 47ca5d2a..343dc0ac 100644 --- a/src/main/java/serverutils/lib/util/misc/PregeneratorCommandInfo.java +++ b/src/main/java/serverutils/lib/util/misc/PregeneratorCommandInfo.java @@ -1,19 +1,14 @@ package serverutils.lib.util.misc; -import scala.Int; +public class PregeneratorCommandInfo { -import java.util.Optional; - -public class PregeneratorCommandInfo -{ private final double xLoc; private final double zLoc; private final int radius; private final int dimensionID; private final int iteration; - public PregeneratorCommandInfo(double xLoc, double zLoc, int radius, int dimensionID, int iteration) - { + public PregeneratorCommandInfo(double xLoc, double zLoc, int radius, int dimensionID, int iteration) { this.xLoc = xLoc; this.zLoc = zLoc; this.radius = radius; @@ -21,8 +16,7 @@ public PregeneratorCommandInfo(double xLoc, double zLoc, int radius, int dimensi this.dimensionID = dimensionID; } - public PregeneratorCommandInfo(double xLoc, double zLoc, int radius, int dimensionID) - { + public PregeneratorCommandInfo(double xLoc, double zLoc, int radius, int dimensionID) { this(xLoc, zLoc, radius, dimensionID, -1); } @@ -38,13 +32,11 @@ public int getRadius() { return radius; } - public int getIteration() - { + public int getIteration() { return this.iteration; } - public int getDimensionID() - { + public int getDimensionID() { return this.dimensionID; } } diff --git a/src/main/java/serverutils/pregenerator/ChunkLoader.java b/src/main/java/serverutils/pregenerator/ChunkLoader.java index 48465290..ea4848b7 100644 --- a/src/main/java/serverutils/pregenerator/ChunkLoader.java +++ b/src/main/java/serverutils/pregenerator/ChunkLoader.java @@ -2,16 +2,18 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.world.gen.ChunkProviderServer; + import org.apache.commons.lang3.tuple.Pair; -import serverutils.pregenerator.filemanager.PregeneratorFileManager; + import serverutils.ServerUtilities; +import serverutils.pregenerator.filemanager.PregeneratorFileManager; + +public class ChunkLoader { -public class ChunkLoader -{ private PregeneratorFileManager fileManager; private int loadIteration = 0; - public ChunkLoader(PregeneratorFileManager fileManager) - { + + public ChunkLoader(PregeneratorFileManager fileManager) { this.fileManager = fileManager; } @@ -24,11 +26,9 @@ public void processLoadChunk(MinecraftServer server, int dimensionId, Pair commandInfoOptional = this.fileManager.getCommandInfo(); - if (commandInfoOptional.isPresent()) - { + if (commandInfoOptional.isPresent()) { PregeneratorCommandInfo commandInfo = commandInfoOptional.get(); - if (commandInfo.getDimensionID() != dimensionToCheck) - { + if (commandInfo.getDimensionID() != dimensionToCheck) { return false; } findChunksToLoadCircle(commandInfo.getRadius(), commandInfo.getXLoc(), commandInfo.getZLoc()); @@ -57,77 +60,63 @@ public boolean initializeFromPregeneratorFiles(MinecraftServer server, int dimen return this.fileManager.isReady(); } } - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); } return false; } - public boolean isGenerating() - { + public boolean isGenerating() { return this.isGenerating; } // Passed in xCenter and passed in zCenter are both in block coordinates. Be sure to transform to chunk coordinates // I've done a ton of testing with this. It works without duplicates and holes in the raster. - public void findChunksToLoadCircle(int radius, double xCenter, double zCenter) - { - // This is a solved problem. I'll use the wikipedia entry on this: https://en.wikipedia.org/wiki/Midpoint_circle_algorithm + public void findChunksToLoadCircle(int radius, double xCenter, double zCenter) { + // This is a solved problem. I'll use the wikipedia entry on this: + // https://en.wikipedia.org/wiki/Midpoint_circle_algorithm int chunkXCenter = (int) Math.floor(xCenter / 16); int chunkZCenter = (int) Math.floor(zCenter / 16); double decisionTracker = 1 - radius; // This is used to tell if we need to step X down. int x = radius; int z = 0; int previousX = radius; - while (x >= z) - { + while (x >= z) { // Add all symmetrical points addChunk(chunkXCenter + x, chunkZCenter + z); addChunk(chunkXCenter - x, chunkZCenter + z); - if (z != x) - { + if (z != x) { addChunk(chunkXCenter + z, chunkZCenter + x); addChunk(chunkXCenter + z, chunkZCenter - x); } - - if (z != 0) - { + if (z != 0) { addChunk(chunkXCenter + x, chunkZCenter - z); addChunk(chunkXCenter - x, chunkZCenter - z); - if (z != x) - { + if (z != x) { addChunk(chunkXCenter - z, chunkZCenter + x); addChunk(chunkXCenter - z, chunkZCenter - x); } } - if(x != previousX) - { + if (x != previousX) { addChunksBetween(chunkXCenter + x, chunkZCenter - z, chunkZCenter + z); addChunksBetween(chunkXCenter - x, chunkZCenter - z, chunkZCenter + z); } previousX = x; - if (x != z) - { - addChunksBetween( chunkXCenter + z, chunkZCenter - x, chunkZCenter + x); - if (z != 0) - { + if (x != z) { + addChunksBetween(chunkXCenter + z, chunkZCenter - x, chunkZCenter + x); + if (z != 0) { addChunksBetween(chunkXCenter - z, chunkZCenter - x, chunkZCenter + x); } } z++; - if (decisionTracker < 0) - { + if (decisionTracker < 0) { decisionTracker += 2 * z + 1; - } - else - { + } else { x--; decisionTracker += 2 * (z - x) + 1; } @@ -135,74 +124,61 @@ public void findChunksToLoadCircle(int radius, double xCenter, double zCenter) System.out.printf("Found %s chunks to load", chunksToLoad.size()); } - public void removeChunkFromList() - { + public void removeChunkFromList() { this.chunksToLoad.remove(this.chunksToLoad.size() - 1); } - public int getChunkToLoadSize() - { + public int getChunkToLoadSize() { return this.chunksToLoad.size(); } - public int getTotalChunksToLoad() - { + public int getTotalChunksToLoad() { return this.totalChunksToLoad; } - public int getDimensionID() - { + public int getDimensionID() { return dimensionID; } - public void queueChunks(int numChunksToQueue) - { - for (int i = 0; i < numChunksToQueue; i++) - { - if (!chunksToLoad.isEmpty()) - { + + public void queueChunks(int numChunksToQueue) { + for (int i = 0; i < numChunksToQueue; i++) { + if (!chunksToLoad.isEmpty()) { loader.processLoadChunk(this.serverType, this.dimensionID, chunksToLoad.get(chunkToLoadIndex)); chunkToLoadIndex--; - } - else - { + } else { fileManager.closeAndRemoveAllFiles(); isGenerating = false; } } } - public String progressString() - { + + public String progressString() { int chunksLoaded = totalChunksToLoad - chunksToLoad.size();; double percentage = (double) chunksLoaded / totalChunksToLoad * 100; - return String.format("Loaded %d chunks of a total of %d. %.1f%% done.", chunksLoaded, totalChunksToLoad, percentage); + return String + .format("Loaded %d chunks of a total of %d. %.1f%% done.", chunksLoaded, totalChunksToLoad, percentage); } - public void reset(boolean hardReset) - { + public void reset(boolean hardReset) { this.isGenerating = false; this.chunksToLoad.clear(); this.chunkToLoadIndex = -1; this.serverType = null; this.loader = null; this.dimensionID = Integer.MIN_VALUE; - if (hardReset) - { + if (hardReset) { fileManager.closeAndRemoveAllFiles(); - } - else - { + } else { fileManager.closeAllFiles(); } this.fileManager = null; } - private void addChunk(int chunkX, int chunkZ) - { + private void addChunk(int chunkX, int chunkZ) { chunksToLoad.add(Pair.of(chunkX, chunkZ)); } - private void addChunksBetween(int xLine, int zMin, int zMax) - { + private void addChunksBetween(int xLine, int zMin, int zMax) { for (int z = zMin + 1; z <= zMax - 1; z++) { addChunk(xLine, z); } diff --git a/src/main/java/serverutils/pregenerator/filemanager/PregeneratorFileManager.java b/src/main/java/serverutils/pregenerator/filemanager/PregeneratorFileManager.java index 1e490d8d..367be917 100644 --- a/src/main/java/serverutils/pregenerator/filemanager/PregeneratorFileManager.java +++ b/src/main/java/serverutils/pregenerator/filemanager/PregeneratorFileManager.java @@ -1,25 +1,27 @@ package serverutils.pregenerator.filemanager; -import net.minecraft.server.MinecraftServer; -import serverutils.lib.util.misc.PregeneratorCommandInfo; -import serverutils.pregenerator.filemanager.readwriters.FileReadWriter; -import serverutils.pregenerator.filemanager.readwriters.SafeFileReadWriter; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; -public class PregeneratorFileManager -{ +import net.minecraft.server.MinecraftServer; + +import serverutils.lib.util.misc.PregeneratorCommandInfo; +import serverutils.pregenerator.filemanager.readwriters.FileReadWriter; +import serverutils.pregenerator.filemanager.readwriters.SafeFileReadWriter; + +public class PregeneratorFileManager { + private final FileReadWriter commandReadWriter; private final SafeFileReadWriter iterationReadWriter; private final String COMMAND_FOLDER = "pregenerationFiles"; private final String COMMAND_FILE = "fileCommand"; private final String COMMAND_ITERATION = "fileIteration"; - public PregeneratorFileManager(MinecraftServer server, double xLoc, double zLoc, int radius, int dimensionID) throws IOException - { + + public PregeneratorFileManager(MinecraftServer server, double xLoc, double zLoc, int radius, int dimensionID) + throws IOException { Path temporaryFileSaveFolder = Paths.get("saves").resolve(getWorldFolderPath(server).resolve(COMMAND_FOLDER)); if (!Files.exists(temporaryFileSaveFolder)) { Files.createDirectories(temporaryFileSaveFolder); @@ -45,65 +47,54 @@ public PregeneratorFileManager(MinecraftServer server) throws IOException { this.commandReadWriter = new FileReadWriter(temporaryFileSaveFolder.resolve(COMMAND_FILE)); } - public Optional getCommandInfo() - { - try - { + public Optional getCommandInfo() { + try { commandReadWriter.openForReading(); iterationReadWriter.openForReading(); - return Optional.of(new PregeneratorCommandInfo(commandReadWriter.readDouble(), commandReadWriter.readDouble(), commandReadWriter.readInt(), commandReadWriter.readInt(), iterationReadWriter.readInt())); - } - catch (IOException ignored) {} // Ignoring this because often there's just nothing in the file if you're loading a world + return Optional.of( + new PregeneratorCommandInfo( + commandReadWriter.readDouble(), + commandReadWriter.readDouble(), + commandReadWriter.readInt(), + commandReadWriter.readInt(), + iterationReadWriter.readInt())); + } catch (IOException ignored) {} // Ignoring this because often there's just nothing in the file if you're + // loading a world return Optional.empty(); } - public void saveIteration(int iteration) - { - try - { + public void saveIteration(int iteration) { + try { iterationReadWriter.writeAndCommitIntAfterIterations(iteration); - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); } } - public void closeAndRemoveAllFiles() - { - try - { + public void closeAndRemoveAllFiles() { + try { iterationReadWriter.close(); iterationReadWriter.deleteFile(); commandReadWriter.close(); commandReadWriter.deleteFile(); - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); } } - public void closeAllFiles() - { - try - { + public void closeAllFiles() { + try { iterationReadWriter.close(); commandReadWriter.close(); - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); } } - public boolean isReady() - { + public boolean isReady() { return true; } - - private Path getWorldFolderPath(MinecraftServer server) { return Paths.get(server.getFolderName()); } diff --git a/src/main/java/serverutils/pregenerator/filemanager/readwriters/FileReadWriter.java b/src/main/java/serverutils/pregenerator/filemanager/readwriters/FileReadWriter.java index e24a447f..505f4400 100644 --- a/src/main/java/serverutils/pregenerator/filemanager/readwriters/FileReadWriter.java +++ b/src/main/java/serverutils/pregenerator/filemanager/readwriters/FileReadWriter.java @@ -5,8 +5,8 @@ import java.nio.file.Files; import java.nio.file.Path; -public class FileReadWriter -{ +public class FileReadWriter { + protected RandomAccessFile randomAccessFile; protected boolean randomAccessFileIsClosed = false; protected final Path filePath; @@ -15,8 +15,8 @@ public FileReadWriter(Path path) throws IOException { this.filePath = path; randomAccessFile = new RandomAccessFile(this.filePath.toFile(), "rw"); } - public void close() throws IOException - { + + public void close() throws IOException { randomAccessFileIsClosed = true; randomAccessFile.close(); } @@ -25,39 +25,33 @@ public void clearFile() throws IOException { randomAccessFile.setLength(0); } - public void writeDouble(double value) throws IOException - { + public void writeDouble(double value) throws IOException { randomAccessFile.writeDouble(value); } - public void writeInt(int value) throws IOException - { + public void writeInt(int value) throws IOException { randomAccessFile.writeInt(value); } - public void openForWriting() throws IOException - { + public void openForWriting() throws IOException { if (randomAccessFile == null || !filePath.toFile().exists() || randomAccessFileIsClosed) { randomAccessFileIsClosed = false; randomAccessFile = new RandomAccessFile(filePath.toFile(), "rw"); } } - public void openForReading() throws IOException - { + public void openForReading() throws IOException { if (randomAccessFile == null || !filePath.toFile().exists() || randomAccessFileIsClosed) { randomAccessFileIsClosed = false; randomAccessFile = new RandomAccessFile(filePath.toFile(), "r"); } } - public int readInt() throws IOException - { + public int readInt() throws IOException { return randomAccessFile.readInt(); } - public double readDouble() throws IOException - { + public double readDouble() throws IOException { return randomAccessFile.readDouble(); } diff --git a/src/main/java/serverutils/pregenerator/filemanager/readwriters/SafeFileReadWriter.java b/src/main/java/serverutils/pregenerator/filemanager/readwriters/SafeFileReadWriter.java index ddbf6dd8..3e1fdd18 100644 --- a/src/main/java/serverutils/pregenerator/filemanager/readwriters/SafeFileReadWriter.java +++ b/src/main/java/serverutils/pregenerator/filemanager/readwriters/SafeFileReadWriter.java @@ -1,6 +1,5 @@ package serverutils.pregenerator.filemanager.readwriters; - import java.io.IOException; import java.io.RandomAccessFile; import java.nio.file.Files; @@ -8,8 +7,9 @@ import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -public class SafeFileReadWriter extends FileReadWriter -{ +// TODO : MAKE THIS BOUNCE BETWEEN 2 FILES +public class SafeFileReadWriter extends FileReadWriter { + private final Path tempFile; private RandomAccessFile randomAccessFileTemp; private boolean randomAccessFileTempIsClosed = false; @@ -23,25 +23,19 @@ public SafeFileReadWriter(Path path, int iterationsBetweenWrites) throws IOExcep randomAccessFileTemp = new RandomAccessFile(this.tempFile.toFile(), "rw"); } - public void writeAndCommitIntAfterIterations(int value) throws IOException - { - if (writeIteration >= iterationsBetweenWrites) - { + public void writeAndCommitIntAfterIterations(int value) throws IOException { + if (writeIteration >= iterationsBetweenWrites) { writeIteration = 0; this.writeInt(value); this.commit(); - } - else - { + } else { writeIteration++; } } @Override - public void writeInt(int value) throws IOException - { - if (this.randomAccessFileTempIsClosed) - { + public void writeInt(int value) throws IOException { + if (this.randomAccessFileTempIsClosed) { this.openForWriting(); } randomAccessFileTemp.seek(0); @@ -75,8 +69,7 @@ public void close() throws IOException { } @Override - public void openForWriting() throws IOException - { + public void openForWriting() throws IOException { if (randomAccessFileTemp == null || !tempFile.toFile().exists() || randomAccessFileTempIsClosed) { randomAccessFileTempIsClosed = false; randomAccessFileTemp = new RandomAccessFile(tempFile.toFile(), "rw");