diff --git a/src/main/java/eu/crushedpixel/replaymod/ReplayMod.java b/src/main/java/eu/crushedpixel/replaymod/ReplayMod.java index 1b067d66..cbf25d8d 100755 --- a/src/main/java/eu/crushedpixel/replaymod/ReplayMod.java +++ b/src/main/java/eu/crushedpixel/replaymod/ReplayMod.java @@ -7,6 +7,7 @@ import eu.crushedpixel.replaymod.localization.LocalizedResourcePack; import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler; import eu.crushedpixel.replaymod.recording.ConnectionEventHandler; import eu.crushedpixel.replaymod.reflection.MCPNames; +import eu.crushedpixel.replaymod.registry.DownloadedFileHandler; import eu.crushedpixel.replaymod.registry.KeybindRegistry; import eu.crushedpixel.replaymod.registry.ReplayFileAppender; import eu.crushedpixel.replaymod.registry.UploadedFileHandler; @@ -66,6 +67,7 @@ public class ReplayMod { public static int TP_DISTANCE_LIMIT = 128; public static ReplayFileAppender replayFileAppender; public static UploadedFileHandler uploadedFileHandler; + public static DownloadedFileHandler downloadedFileHandler; private static Field defaultResourcePacksField; static { @@ -92,6 +94,8 @@ public class ReplayMod { replaySettings = new ReplaySettings(); replaySettings.readValues(); + downloadedFileHandler = new DownloadedFileHandler(); + replayFileAppender = new ReplayFileAppender(); replayFileAppender.start(); } diff --git a/src/main/java/eu/crushedpixel/replaymod/gui/online/GuiReplayCenter.java b/src/main/java/eu/crushedpixel/replaymod/gui/online/GuiReplayCenter.java index 08aee344..6503f041 100755 --- a/src/main/java/eu/crushedpixel/replaymod/gui/online/GuiReplayCenter.java +++ b/src/main/java/eu/crushedpixel/replaymod/gui/online/GuiReplayCenter.java @@ -8,6 +8,7 @@ import eu.crushedpixel.replaymod.gui.GuiConstants; import eu.crushedpixel.replaymod.gui.elements.GuiReplayListEntry; import eu.crushedpixel.replaymod.gui.replayviewer.GuiReplayViewer; import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler; +import eu.crushedpixel.replaymod.replay.ReplayHandler; import net.minecraft.client.gui.*; import net.minecraft.client.resources.I18n; import org.lwjgl.input.Keyboard; @@ -149,7 +150,7 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback { GuiReplayListEntry entry = currentList.getListEntry(index); FileInfo info = entry.getFileInfo(); if(info != null) { - boolean downloaded = false; + boolean downloaded = ReplayMod.downloadedFileHandler.getFileForID(info.getId()) != null; loadButton.displayString = downloaded ? I18n.format("replaymod.gui.load") : I18n.format("replaymod.gui.download"); loadButton.enabled = true; } else { @@ -177,7 +178,19 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback { } else if(button.id == GuiConstants.CENTER_SEARCH_BUTTON) { } else if(button.id == GuiConstants.CENTER_LOAD_REPLAY_BUTTON) { - + GuiReplayListEntry entry = currentList.getListEntry(currentList.selected); + FileInfo info = entry.getFileInfo(); + if(info != null) { + File f = ReplayMod.downloadedFileHandler.getFileForID(info.getId()); + if(f == null) { + f = ReplayMod.downloadedFileHandler.downloadFileForID(info.getId()); + } + try { + ReplayHandler.startReplay(f); + } catch(Exception e) { + e.printStackTrace(); + } + } } } diff --git a/src/main/java/eu/crushedpixel/replaymod/registry/DownloadedFileHandler.java b/src/main/java/eu/crushedpixel/replaymod/registry/DownloadedFileHandler.java index fc293155..98bd3348 100644 --- a/src/main/java/eu/crushedpixel/replaymod/registry/DownloadedFileHandler.java +++ b/src/main/java/eu/crushedpixel/replaymod/registry/DownloadedFileHandler.java @@ -1,7 +1,8 @@ package eu.crushedpixel.replaymod.registry; import eu.crushedpixel.replaymod.ReplayMod; -import eu.crushedpixel.replaymod.recording.ConnectionEventHandler; +import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler; +import eu.crushedpixel.replaymod.utils.ReplayFile; import org.apache.commons.io.FilenameUtils; import java.io.File; @@ -29,9 +30,14 @@ public class DownloadedFileHandler { } } + private File generateFileForID(int id) { + return new File(downloadFolder, id+"."+ ReplayFile.ZIP_FILE_EXTENSION); + } + public void addToIndex(int id) { try { - File f = new File(downloadFolder, id+"."+ConnectionEventHandler.ZIP_FILE_EXTENSION); + File f = generateFileForID(id); + if(f.exists()) downloadedFiles.put(id, f); } catch(Exception e) { e.printStackTrace(); } @@ -40,4 +46,20 @@ public class DownloadedFileHandler { public File getFileForID(int id) { return downloadedFiles.get(id); } + + public File downloadFileForID(int id) { + File f = getFileForID(id); + if(f != null) return f; + + f = generateFileForID(id); + + try { + ReplayMod.apiClient.downloadFile(AuthenticationHandler.getKey(), id, f); + addToIndex(id); + } catch(Exception e) { + e.printStackTrace(); + } + + return f; + } }