Added Progress Bar and Cancel Button when downloading a Replay File from Replay Center (GuiReplayDownloading) | https://trello.com/c/1ZPhwmp8/
This commit is contained in:
@@ -8,6 +8,7 @@ import com.mojang.authlib.exceptions.AuthenticationException;
|
|||||||
import eu.crushedpixel.replaymod.api.replay.ReplayModApiMethods;
|
import eu.crushedpixel.replaymod.api.replay.ReplayModApiMethods;
|
||||||
import eu.crushedpixel.replaymod.api.replay.SearchQuery;
|
import eu.crushedpixel.replaymod.api.replay.SearchQuery;
|
||||||
import eu.crushedpixel.replaymod.api.replay.holders.*;
|
import eu.crushedpixel.replaymod.api.replay.holders.*;
|
||||||
|
import eu.crushedpixel.replaymod.gui.elements.listeners.ProgressUpdateListener;
|
||||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHash;
|
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHash;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
@@ -106,21 +107,46 @@ public class ApiClient {
|
|||||||
FileUtils.copyURLToFile(url, target);
|
FileUtils.copyURLToFile(url, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void downloadFile(String auth, int file, File target) throws IOException, ApiException {
|
private boolean cancelDownload = false;
|
||||||
|
|
||||||
|
public void downloadFile(String auth, int file, File target, ProgressUpdateListener listener) throws IOException, ApiException {
|
||||||
|
cancelDownload = false;
|
||||||
|
|
||||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.download_file);
|
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.download_file);
|
||||||
builder.put("auth", auth);
|
builder.put("auth", auth);
|
||||||
builder.put("id", file);
|
builder.put("id", file);
|
||||||
String url = builder.toString();
|
String url = builder.toString();
|
||||||
URL website = new URL(url);
|
URL website = new URL(url);
|
||||||
HttpURLConnection con = (HttpURLConnection) website.openConnection();
|
HttpURLConnection con = (HttpURLConnection) website.openConnection();
|
||||||
|
|
||||||
|
int fileSize = con.getContentLength();
|
||||||
|
|
||||||
InputStream is = con.getInputStream();
|
InputStream is = con.getInputStream();
|
||||||
|
|
||||||
if(con.getResponseCode() == 200) {
|
if(con.getResponseCode() == 200) {
|
||||||
OutputStream out = new FileOutputStream(target);
|
BufferedInputStream bin = new BufferedInputStream(is);
|
||||||
|
FileOutputStream fout = new FileOutputStream(target);
|
||||||
try {
|
try {
|
||||||
IOUtils.copy(is, out);
|
final byte data[] = new byte[1024];
|
||||||
|
int count;
|
||||||
|
int read = 0;
|
||||||
|
while ((count = bin.read(data, 0, 1024)) != -1) {
|
||||||
|
if(cancelDownload) {
|
||||||
|
bin.close();
|
||||||
|
fout.close();
|
||||||
|
|
||||||
|
FileUtils.deleteQuietly(target);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fout.write(data, 0, count);
|
||||||
|
read += count;
|
||||||
|
listener.onProgressChanged((float)(read)/fileSize);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
out.close();
|
bin.close();
|
||||||
|
fout.close();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
JsonElement element = jsonParser.parse(IOUtils.toString(is));
|
JsonElement element = jsonParser.parse(IOUtils.toString(is));
|
||||||
@@ -135,6 +161,10 @@ public class ApiClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void cancelDownload() {
|
||||||
|
this.cancelDownload = true;
|
||||||
|
}
|
||||||
|
|
||||||
public void rateFile(String auth, int file, Rating.RatingType rating) throws IOException, ApiException {
|
public void rateFile(String auth, int file, Rating.RatingType rating) throws IOException, ApiException {
|
||||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.rate_file);
|
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.rate_file);
|
||||||
builder.put("auth", auth);
|
builder.put("auth", auth);
|
||||||
|
|||||||
@@ -103,4 +103,5 @@ public class GuiConstants {
|
|||||||
public static final int REPLAY_SETTINGS_CLEARCALLBACK_ID = 9014;
|
public static final int REPLAY_SETTINGS_CLEARCALLBACK_ID = 9014;
|
||||||
|
|
||||||
public static final int REPLAY_EDITING_CANCEL_BUTTON = 1234;
|
public static final int REPLAY_EDITING_CANCEL_BUTTON = 1234;
|
||||||
|
public static final int REPLAY_DOWNLOADING_CANCEL_BUTTON = 2345;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,12 +233,13 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
|
|||||||
if(info != null) {
|
if(info != null) {
|
||||||
File f = ReplayMod.downloadedFileHandler.getFileForID(info.getId());
|
File f = ReplayMod.downloadedFileHandler.getFileForID(info.getId());
|
||||||
if(f == null) {
|
if(f == null) {
|
||||||
f = ReplayMod.downloadedFileHandler.downloadFileForID(info.getId());
|
mc.displayGuiScreen(new GuiReplayDownloading(info));
|
||||||
}
|
} else {
|
||||||
try {
|
try {
|
||||||
ReplayHandler.startReplay(f);
|
ReplayHandler.startReplay(f);
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(button.id == GuiConstants.CENTER_FAV_REPLAY_BUTTON) {
|
} else if(button.id == GuiConstants.CENTER_FAV_REPLAY_BUTTON) {
|
||||||
|
|||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package eu.crushedpixel.replaymod.gui.online;
|
||||||
|
|
||||||
|
import com.mojang.realmsclient.gui.ChatFormatting;
|
||||||
|
import eu.crushedpixel.replaymod.ReplayMod;
|
||||||
|
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
|
||||||
|
import eu.crushedpixel.replaymod.gui.GuiConstants;
|
||||||
|
import eu.crushedpixel.replaymod.gui.elements.GuiProgressBar;
|
||||||
|
import eu.crushedpixel.replaymod.gui.elements.listeners.ProgressUpdateListener;
|
||||||
|
import eu.crushedpixel.replaymod.replay.ReplayHandler;
|
||||||
|
import net.minecraft.client.gui.GuiButton;
|
||||||
|
import net.minecraft.client.gui.GuiScreen;
|
||||||
|
import net.minecraft.client.resources.I18n;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class GuiReplayDownloading extends GuiScreen implements ProgressUpdateListener {
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
private String pleaseWait;
|
||||||
|
private String cancelCallback;
|
||||||
|
|
||||||
|
private boolean callback = false;
|
||||||
|
|
||||||
|
private boolean initialized = false;
|
||||||
|
|
||||||
|
private GuiProgressBar progressBar;
|
||||||
|
private GuiButton cancelButton;
|
||||||
|
|
||||||
|
public GuiReplayDownloading(final FileInfo fileInfo) {
|
||||||
|
pleaseWait = I18n.format("replaymod.gui.viewer.download.message", ChatFormatting.UNDERLINE+fileInfo.getName()+ChatFormatting.RESET);
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
final File replayFile = ReplayMod.downloadedFileHandler.downloadFileForID(fileInfo.getId(), GuiReplayDownloading.this);
|
||||||
|
if(replayFile.exists()) {
|
||||||
|
mc.addScheduledTask(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
ReplayHandler.startReplay(replayFile);
|
||||||
|
} catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initGui() {
|
||||||
|
if(!initialized) {
|
||||||
|
title = I18n.format("replaymod.gui.viewer.download.title");
|
||||||
|
cancelCallback = I18n.format("replaymod.gui.rendering.cancel.callback");
|
||||||
|
|
||||||
|
progressBar = new GuiProgressBar();
|
||||||
|
cancelButton = new GuiButton(GuiConstants.REPLAY_DOWNLOADING_CANCEL_BUTTON, 0, 0, I18n.format("gui.cancel"));
|
||||||
|
}
|
||||||
|
|
||||||
|
progressBar.setBounds(10, this.height-30-20, this.width-20, 20);
|
||||||
|
|
||||||
|
cancelButton.xPosition = this.width - 5 - 150;
|
||||||
|
cancelButton.width = 150;
|
||||||
|
cancelButton.yPosition = this.height - 5 - 20;
|
||||||
|
|
||||||
|
buttonList.add(cancelButton);
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
|
||||||
|
this.drawDefaultBackground();
|
||||||
|
super.drawScreen(mouseX, mouseY, partialTicks);
|
||||||
|
this.drawCenteredString(mc.fontRendererObj, title, this.width / 2, 20, Color.WHITE.getRGB());
|
||||||
|
this.drawCenteredString(mc.fontRendererObj, pleaseWait, this.width / 2, 40, Color.WHITE.getRGB());
|
||||||
|
|
||||||
|
progressBar.drawProgressBar();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void actionPerformed(GuiButton button) throws IOException {
|
||||||
|
if(!button.enabled) return;
|
||||||
|
if(button.id == GuiConstants.REPLAY_DOWNLOADING_CANCEL_BUTTON) {
|
||||||
|
if(!callback) {
|
||||||
|
callback = true;
|
||||||
|
button.displayString = cancelCallback;
|
||||||
|
} else {
|
||||||
|
ReplayMod.apiClient.cancelDownload();
|
||||||
|
mc.displayGuiScreen(new GuiReplayCenter());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProgressChanged(float progress) {
|
||||||
|
if(progressBar != null) progressBar.setProgress(progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProgressChanged(float progress, String progressString) {
|
||||||
|
onProgressChanged(progress);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package eu.crushedpixel.replaymod.registry;
|
package eu.crushedpixel.replaymod.registry;
|
||||||
|
|
||||||
import eu.crushedpixel.replaymod.ReplayMod;
|
import eu.crushedpixel.replaymod.ReplayMod;
|
||||||
|
import eu.crushedpixel.replaymod.gui.elements.listeners.ProgressUpdateListener;
|
||||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||||
import eu.crushedpixel.replaymod.utils.ReplayFile;
|
import eu.crushedpixel.replaymod.utils.ReplayFile;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
@@ -51,15 +52,17 @@ public class DownloadedFileHandler {
|
|||||||
return downloadedFiles.get(id);
|
return downloadedFiles.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public File downloadFileForID(int id) {
|
public File downloadFileForID(int id, ProgressUpdateListener progressUpdateListener) {
|
||||||
File f = getFileForID(id);
|
File f = getFileForID(id);
|
||||||
if(f != null) return f;
|
if(f != null) return f;
|
||||||
|
|
||||||
f = generateFileForID(id);
|
f = generateFileForID(id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ReplayMod.apiClient.downloadFile(AuthenticationHandler.getKey(), id, f);
|
ReplayMod.apiClient.downloadFile(AuthenticationHandler.getKey(), id, f, progressUpdateListener);
|
||||||
addToIndex(id);
|
if(f.exists()) {
|
||||||
|
addToIndex(id);
|
||||||
|
}
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,6 +135,9 @@ replaymod.gui.viewer.delete.linea=Are you sure you want to delete this replay?
|
|||||||
#The Replay file name is inserted before the following string, e.g. "Some Replay will be lost forever!..."
|
#The Replay file name is inserted before the following string, e.g. "Some Replay will be lost forever!..."
|
||||||
replaymod.gui.viewer.delete.lineb=will be lost forever! (a long time!)
|
replaymod.gui.viewer.delete.lineb=will be lost forever! (a long time!)
|
||||||
|
|
||||||
|
replaymod.gui.viewer.download.title=Downloading Replay File...
|
||||||
|
replaymod.gui.viewer.download.message=Please wait while %1$s is being downloaded.
|
||||||
|
|
||||||
replaymod.gui.login.noacc=Don't have an account yet?
|
replaymod.gui.login.noacc=Don't have an account yet?
|
||||||
|
|
||||||
#Replay Center GUI
|
#Replay Center GUI
|
||||||
|
|||||||
Reference in New Issue
Block a user