Whenever a Replay File is being written or modified and the user is not in-game, a GuiReplaySaving is opened and asks him to wait
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package eu.crushedpixel.replaymod.registry;
|
||||
|
||||
import eu.crushedpixel.replaymod.gui.GuiReplaySaving;
|
||||
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.fml.client.FMLClientHandler;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
@@ -12,6 +16,31 @@ public class ReplayFileAppender extends Thread {
|
||||
|
||||
private Queue<Pair<Pair<File, String>, File>> filesToMove = new ConcurrentLinkedQueue<Pair<Pair<File, String>, File>>();
|
||||
private boolean shutdown = false;
|
||||
private List<GuiReplaySaving> listeners = new ArrayList<GuiReplaySaving>();
|
||||
|
||||
//this is true if the DataListener is currently busy saving a newly recorded Replay File
|
||||
private boolean newReplayFileWriting = false;
|
||||
|
||||
public void startNewReplayFileWriting() {
|
||||
newReplayFileWriting = true;
|
||||
|
||||
if(!FMLClientHandler.instance().isGUIOpen(GuiReplaySaving.class)) {
|
||||
Minecraft.getMinecraft().addScheduledTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final GuiReplaySaving savingScreen = new GuiReplaySaving(null);
|
||||
addFinishListener(savingScreen);
|
||||
|
||||
Minecraft.getMinecraft().displayGuiScreen(savingScreen);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void replayFileWritingFinished() {
|
||||
newReplayFileWriting = false;
|
||||
callListeners();
|
||||
}
|
||||
|
||||
public ReplayFileAppender() {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
|
||||
@@ -36,6 +65,14 @@ public class ReplayFileAppender extends Thread {
|
||||
shutdown = true;
|
||||
}
|
||||
|
||||
public boolean isBusy() {
|
||||
return !filesToMove.isEmpty() && !newReplayFileWriting;
|
||||
}
|
||||
|
||||
public void addFinishListener(GuiReplaySaving gui) {
|
||||
listeners.add(gui);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(!shutdown || !filesToMove.isEmpty()) {
|
||||
@@ -47,6 +84,8 @@ public class ReplayFileAppender extends Thread {
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
filesToMove.add(mv);
|
||||
} finally {
|
||||
callListeners();
|
||||
}
|
||||
} else {
|
||||
filesToMove.add(mv);
|
||||
@@ -58,4 +97,19 @@ public class ReplayFileAppender extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
private void callListeners() {
|
||||
if(filesToMove.isEmpty() && !newReplayFileWriting) {
|
||||
for(final GuiReplaySaving gui : listeners) {
|
||||
Minecraft.getMinecraft().addScheduledTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
gui.dispatch();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
listeners = new ArrayList<GuiReplaySaving>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user