The Replay Viewer now loads its entries asynchronously to prevent Minecraft from freezing whenever the Replay Viewer is opened
This commit is contained in:
@@ -10,13 +10,8 @@ public class GuiLoadingListEntry implements IGuiListEntry {
|
|||||||
|
|
||||||
boolean registered = false;
|
boolean registered = false;
|
||||||
private final Minecraft mc = Minecraft.getMinecraft();
|
private final Minecraft mc = Minecraft.getMinecraft();
|
||||||
private GuiReplayListExtended parent;
|
|
||||||
private final String message = I18n.format("replaymod.gui.loading")+"...";
|
private final String message = I18n.format("replaymod.gui.loading")+"...";
|
||||||
|
|
||||||
public GuiLoadingListEntry(GuiReplayListExtended parent) {
|
|
||||||
this.parent = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawEntry(int slotIndex, int x, int y, int listWidth, int slotHeight, int mouseX, int mouseY, boolean isSelected) {
|
public void drawEntry(int slotIndex, int x, int y, int listWidth, int slotHeight, int mouseX, int mouseY, boolean isSelected) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -355,7 +355,7 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
|
|||||||
private void updateCurrentList(Pagination pagination) {
|
private void updateCurrentList(Pagination pagination) {
|
||||||
elementSelected(-1);
|
elementSelected(-1);
|
||||||
currentList = new ReplayFileList(mc, width, height, 50, height - 60, this);
|
currentList = new ReplayFileList(mc, width, height, 50, height - 60, this);
|
||||||
loadingListEntry = new GuiLoadingListEntry(currentList);
|
loadingListEntry = new GuiLoadingListEntry();
|
||||||
currentList.addEntry(loadingListEntry);
|
currentList.addEntry(loadingListEntry);
|
||||||
|
|
||||||
if(pagination.getLoadedPages() < 0) {
|
if(pagination.getLoadedPages() < 0) {
|
||||||
|
|||||||
@@ -121,7 +121,11 @@ public class GuiUploadFile extends GuiScreen {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initGui() {
|
public void initGui() {
|
||||||
if(replayFile == null) return;
|
if(replayFile == null) {
|
||||||
|
mc.displayGuiScreen(parent);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(!AuthenticationHandler.isAuthenticated()) {
|
if(!AuthenticationHandler.isAuthenticated()) {
|
||||||
mc.displayGuiScreen(new GuiLoginPrompt(parent, this));
|
mc.displayGuiScreen(new GuiLoginPrompt(parent, this));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.mojang.realmsclient.util.Pair;
|
|||||||
import eu.crushedpixel.replaymod.ReplayMod;
|
import eu.crushedpixel.replaymod.ReplayMod;
|
||||||
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
|
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
|
||||||
import eu.crushedpixel.replaymod.gui.GuiReplaySettings;
|
import eu.crushedpixel.replaymod.gui.GuiReplaySettings;
|
||||||
|
import eu.crushedpixel.replaymod.gui.elements.GuiLoadingListEntry;
|
||||||
import eu.crushedpixel.replaymod.gui.elements.GuiReplayListEntry;
|
import eu.crushedpixel.replaymod.gui.elements.GuiReplayListEntry;
|
||||||
import eu.crushedpixel.replaymod.gui.elements.GuiReplayListExtended;
|
import eu.crushedpixel.replaymod.gui.elements.GuiReplayListExtended;
|
||||||
import eu.crushedpixel.replaymod.gui.online.GuiUploadFile;
|
import eu.crushedpixel.replaymod.gui.online.GuiUploadFile;
|
||||||
@@ -11,7 +12,6 @@ import eu.crushedpixel.replaymod.recording.ReplayMetaData;
|
|||||||
import eu.crushedpixel.replaymod.registry.ResourceHelper;
|
import eu.crushedpixel.replaymod.registry.ResourceHelper;
|
||||||
import eu.crushedpixel.replaymod.replay.ReplayHandler;
|
import eu.crushedpixel.replaymod.replay.ReplayHandler;
|
||||||
import eu.crushedpixel.replaymod.utils.ImageUtils;
|
import eu.crushedpixel.replaymod.utils.ImageUtils;
|
||||||
import eu.crushedpixel.replaymod.utils.MouseUtils;
|
|
||||||
import eu.crushedpixel.replaymod.utils.ReplayFile;
|
import eu.crushedpixel.replaymod.utils.ReplayFile;
|
||||||
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
||||||
import net.minecraft.client.gui.GuiButton;
|
import net.minecraft.client.gui.GuiButton;
|
||||||
@@ -23,7 +23,6 @@ import net.minecraft.util.Util;
|
|||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.lwjgl.Sys;
|
import org.lwjgl.Sys;
|
||||||
import org.lwjgl.input.Keyboard;
|
import org.lwjgl.input.Keyboard;
|
||||||
import org.lwjgl.util.Point;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@@ -59,7 +58,11 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void reloadFiles() {
|
private void reloadFiles() {
|
||||||
|
GuiLoadingListEntry loadingListEntry = new GuiLoadingListEntry();
|
||||||
|
|
||||||
replayGuiList.clearEntries();
|
replayGuiList.clearEntries();
|
||||||
|
replayGuiList.addEntry(loadingListEntry);
|
||||||
|
|
||||||
replayFileList = new ArrayList<Pair<Pair<File, ReplayMetaData>, File>>();
|
replayFileList = new ArrayList<Pair<Pair<File, ReplayMetaData>, File>>();
|
||||||
|
|
||||||
for(File file : ReplayFileIO.getAllReplayFiles()) {
|
for(File file : ReplayFileIO.getAllReplayFiles()) {
|
||||||
@@ -75,20 +78,29 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
|
|||||||
|
|
||||||
ImageIO.write(img, "jpg", tmp);
|
ImageIO.write(img, "jpg", tmp);
|
||||||
}
|
}
|
||||||
replayFileList.add(Pair.of(Pair.of(file, metaData), tmp));
|
|
||||||
|
final Pair<Pair<File, ReplayMetaData>, File> p = Pair.of(Pair.of(file, metaData), tmp);
|
||||||
|
final int index = getInsertionIndex(p, replayFileList);
|
||||||
|
|
||||||
|
replayFileList.add(index, p);
|
||||||
|
|
||||||
|
final FileInfo fileInfo = new FileInfo(-1, p.first().second(), null, null,
|
||||||
|
-1, -1, -1, FilenameUtils.getBaseName(p.first().first().getName()), true, -1);
|
||||||
|
|
||||||
|
mc.addScheduledTask(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
replayGuiList.addEntry(index, new GuiReplayListEntry(replayGuiList, fileInfo, p.second()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
replayFile.close();
|
replayFile.close();
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
replayGuiList.removeEntry(loadingListEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.sort(replayFileList, new FileAgeComparator());
|
|
||||||
|
|
||||||
for(Pair<Pair<File, ReplayMetaData>, File> p : replayFileList) {
|
|
||||||
FileInfo fileInfo = new FileInfo(-1, p.first().second(), null, null,
|
|
||||||
-1, -1, -1, FilenameUtils.getBaseName(p.first().first().getName()), true, -1);
|
|
||||||
replayGuiList.addEntry(new GuiReplayListEntry(replayGuiList, fileInfo, p.second()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -109,7 +121,13 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
|
|||||||
this.replayGuiList.setDimensions(this.width, this.height, 32, this.height - 64);
|
this.replayGuiList.setDimensions(this.width, this.height, 32, this.height - 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
reloadFiles();
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
reloadFiles();
|
||||||
|
}
|
||||||
|
}, "replay-viewer-file-reloader").start();
|
||||||
|
|
||||||
this.createButtons();
|
this.createButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +170,6 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
|
|||||||
|
|
||||||
if(uploadButton.isMouseOver() && !uploadButton.enabled && loadButton.enabled) {
|
if(uploadButton.isMouseOver() && !uploadButton.enabled && loadButton.enabled) {
|
||||||
if(currentFileUploaded) {
|
if(currentFileUploaded) {
|
||||||
Point mouse = MouseUtils.getMousePos();
|
|
||||||
ReplayMod.tooltipRenderer.drawTooltip(mouseX, mouseY, I18n.format("replaymod.gui.viewer.alreadyuploaded"), this, Color.RED.getRGB());
|
ReplayMod.tooltipRenderer.drawTooltip(mouseX, mouseY, I18n.format("replaymod.gui.viewer.alreadyuploaded"), this, Color.RED.getRGB());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,17 +275,27 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FileAgeComparator implements Comparator<Pair<Pair<File, ReplayMetaData>, File>> {
|
private static FileAgeComparator fileAgeComparator = new FileAgeComparator();
|
||||||
|
|
||||||
|
public static class FileAgeComparator implements Comparator<Pair<Pair<File, ReplayMetaData>, File>> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(Pair<Pair<File, ReplayMetaData>, File> o1, Pair<Pair<File, ReplayMetaData>, File> o2) {
|
public int compare(Pair<Pair<File, ReplayMetaData>, File> o1, Pair<Pair<File, ReplayMetaData>, File> o2) {
|
||||||
try {
|
try {
|
||||||
return (int) (new Date(o2.first().second().getDate()).compareTo(new Date(o1.first().second().getDate())));
|
return new Date(o2.first().second().getDate()).compareTo(new Date(o1.first().second().getDate()));
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getInsertionIndex(Pair<Pair<File, ReplayMetaData>, File> p, List<Pair<Pair<File, ReplayMetaData>, File>> list) {
|
||||||
|
List<Pair<Pair<File, ReplayMetaData>, File>> nl = new ArrayList<Pair<Pair<File, ReplayMetaData>, File>>(list);
|
||||||
|
|
||||||
|
nl.add(p);
|
||||||
|
Collections.sort(nl, fileAgeComparator);
|
||||||
|
|
||||||
|
return nl.indexOf(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user