Skip to content

Commit

Permalink
Added options to pause chunk saving, delete old chunks, save & exit
Browse files Browse the repository at this point in the history
  • Loading branch information
mircokroon committed Oct 8, 2020
1 parent ed45312 commit 2ac3dae
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 4 deletions.
46 changes: 45 additions & 1 deletion src/main/java/game/data/WorldManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import game.data.region.McaFile;
import game.data.region.Region;
import gui.GuiManager;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import proxy.CompressionManager;
import se.llbit.nbt.ByteTag;
Expand All @@ -37,6 +38,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -66,6 +68,10 @@ public class WorldManager extends Thread {

private static boolean writeChunks;

private static boolean isPaused;

private static boolean isSaving;

private static ContainerManager containerManager;

private WorldManager() {
Expand Down Expand Up @@ -338,10 +344,18 @@ public void run() {
/**
* Save the world. Will tell all regions to save their chunks.
*/
private static void save() {
public static void save() {
if (!writeChunks) {
return;
}

// make sure we can't have two saving calls at once (due to save & exit)
if (isSaving) {
return;
}
isSaving = true;


if (!regions.isEmpty()) {
// convert the values to an array first to prevent blocking any threads
Region[] r = regions.values().toArray(new Region[regions.size()]);
Expand All @@ -367,6 +381,8 @@ private static void save() {

// remove empty regions
regions.entrySet().removeIf(el -> el.getValue().isEmpty());

isSaving = false;
}

public static ContainerManager getContainerManager() {
Expand All @@ -376,6 +392,34 @@ public static ContainerManager getContainerManager() {
return containerManager;
}

public static void pauseSaving() {
isPaused = true;
}

public static void resumeSaving() {
isPaused = false;
}

public static void deleteAllExisting() {
regions = new HashMap<>();
ChunkFactory.getInstance().clear();

try {
File dir = Paths.get(Game.getExportDirectory(), Game.getDimension().getPath(), "region").toFile();

if (dir.isDirectory()) {
FileUtils.cleanDirectory(dir);
}
} catch (IOException ex) {
System.out.println("Could not delete region files. Reason: " + ex.getMessage());
}

GuiManager.clearChunks();
}

public static boolean isPaused() {
return isPaused;
}


}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/game/data/chunk/ChunkFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static ChunkFactory getInstance() {
}

private ChunkFactory() {
clear();
}

public void clear() {
this.tileEntities = new ConcurrentHashMap<>();
this.chunkEntities = new ConcurrentHashMap<>();
this.entities = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -126,6 +130,11 @@ public void updateTileEntity(Coordinate3D position, SpecificTag entityData) {
* Need a non-static method to do this as we cannot otherwise call notify
*/
public synchronized void addChunk(DataTypeProvider provider) {
// if the world manager is currently paused, discard this chunk
if (WorldManager.isPaused()) {
return;
}

unparsedChunks.add(new ChunkParserPair(provider, Game.getDimension()));
notify();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/game/data/region/McaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
public class McaFile {
public final static int SECTOR_SIZE = 4096;
private Map<Integer, ChunkBinary> chunkMap;
private Path filePath;
private Coordinate2D regionLocation;
private final Path filePath;
private final Coordinate2D regionLocation;

/**
* Parse MCA from a given file location.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/gui/CanvasHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public class CanvasHandler extends JPanel implements ActionListener {
bindScroll();
}

public void clearChunks() {
chunkMap.clear();
drawableChunks.clear();

hasChanged = true;
}

private void bindScroll() {
this.addMouseWheelListener(mouseWheelEvent -> {
this.renderDistance = this.renderDistance + (mouseWheelEvent.getWheelRotation() * 2);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,11 @@ public static void drawExistingChunks(List<CoordinateDim2D> existing) {
existing.forEach(chunkGraphicsHandler::setChunkExists);
}
}

public static void clearChunks() {
if (chunkGraphicsHandler != null) {
chunkGraphicsHandler.clearChunks();
}
}
}

35 changes: 34 additions & 1 deletion src/main/java/gui/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,44 @@
import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class RightClickMenu extends JPopupMenu {
final static String PROMPT_PAUSE = "Pause chunk saving";
final static String PROMPT_RESUME = "Resume chunk saving";

public RightClickMenu(CanvasHandler handler) {

JMenuItem togglePause = new JMenuItem(PROMPT_PAUSE);

togglePause.addActionListener(new AbstractAction("Pause chunk saving") {
@Override
public void actionPerformed(ActionEvent e) {
if (WorldManager.isPaused()) {
System.out.println("Resuming");
WorldManager.resumeSaving();
togglePause.setText(PROMPT_PAUSE);
} else {
System.out.println("Pausing");
WorldManager.pauseSaving();
togglePause.setText(PROMPT_RESUME);
}
}
});
add(togglePause);

add(new JMenuItem(new AbstractAction("Delete all downloaded chunks") {
@Override
public void actionPerformed(ActionEvent e) {
WorldManager.deleteAllExisting();
}
}));


add(new Separator());

add(new JMenuItem(new AbstractAction("Save overview to file") {
@Override
public void actionPerformed(ActionEvent e) {
Expand All @@ -30,9 +62,10 @@ public void actionPerformed(ActionEvent e) {
}
}));

add(new JMenuItem(new AbstractAction("Exit") {
add(new JMenuItem(new AbstractAction("Save & Exit") {
@Override
public void actionPerformed(ActionEvent e) {
WorldManager.save();
System.exit(0);
}
}));
Expand Down

0 comments on commit 2ac3dae

Please sign in to comment.