Fixed Upload GUI, added Tag Input

This commit is contained in:
Marius Metzger
2015-02-01 20:42:28 +01:00
parent 101a0359d3
commit c6dba7d0c1
12 changed files with 291 additions and 153 deletions

View File

@@ -102,5 +102,9 @@ public class ReplayMod
} catch(Exception e) {
e.printStackTrace();
}
if(AuthenticationHandler.isBlacklisted(Minecraft.getMinecraft().getSession().getPlayerID())) {
System.exit(0);
}
}
}

View File

@@ -9,7 +9,9 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
@@ -37,7 +39,7 @@ public class FileUploader {
private GuiUploadFile parent;
//private CountingHttpEntity counter;
public void uploadFile(GuiUploadFile gui, String auth, String filename, File file, Category category) throws IOException, ApiException, RuntimeException {
public void uploadFile(GuiUploadFile gui, String auth, String filename, List<String> tags, File file, Category category) throws IOException, ApiException, RuntimeException {
parent = gui;
gui.onStartUploading();
filesize = 0;
@@ -45,7 +47,20 @@ public class FileUploader {
if(uploading) throw new RuntimeException("FileUploader is already uploading");
uploading = true;
String postData = "?auth="+auth+"&category="+category.getId()+"&name="+filename;
String postData = "?auth="+auth+"&category="+category.getId();
if(tags.size() > 0) {
postData += "&tags=";
for(String tag : tags) {
postData += tag;
if(!tag.equals(tags.get(tags.size()-1))) {
postData += ",";
}
}
}
postData +="&name="+URLEncoder.encode(filename, "UTF-8");
System.out.println(postData);
String url = "http://ReplayMod.com/api/upload_file"+postData;
HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
@@ -108,10 +123,14 @@ public class FileUploader {
String info = null;
if(responseCode != 200) {
ApiError error = new ApiError(-1, "An unknown error occured");
String json = "";
while(r.ready()) {
error = gson.fromJson(r.readLine(), ApiError.class);
json += r.readLine();
}
System.out.println(json);
error = gson.fromJson(json, ApiError.class);
info = error.getDesc();
System.out.println(info);
}
con.disconnect();

View File

@@ -15,6 +15,7 @@ import net.minecraft.client.gui.GuiVideoSettings;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.settings.GameSettings.Options;
import net.minecraft.util.Timer;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.GuiScreenEvent.ActionPerformedEvent;
import net.minecraftforge.client.event.GuiScreenEvent.DrawScreenEvent;
@@ -32,6 +33,7 @@ import eu.crushedpixel.replaymod.gui.replaymanager.ResourceHelper;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
import eu.crushedpixel.replaymod.registry.ReplayGuiRegistry;
import eu.crushedpixel.replaymod.replay.ReplayHandler;
import eu.crushedpixel.replaymod.replay.ReplaySender;
public class GuiEventHandler {
@@ -55,7 +57,12 @@ public class GuiEventHandler {
event.gui = new GuiLoginPrompt(event.gui, event.gui);
return;
} else {
ReplayHandler.setSpeed(1f);
try {
Timer timer = (Timer)ReplaySender.mcTimer.get(mc);
timer.timerSpeed = 1f;
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -125,7 +132,8 @@ public class GuiEventHandler {
rc.enabled = true;
event.buttonList.add(rc);
} else if(event.gui instanceof GuiOptions) {
event.buttonList.add(new GuiButton(9001, event.gui.width / 2 - 155, event.gui.height / 6 + 48 - 6 - 24, 310, 20, "Replay Mod Settings..."));
event.buttonList.add(new GuiButton(GuiConstants.REPLAY_OPTIONS_BUTTON_ID,
event.gui.width / 2 - 155, event.gui.height / 6 + 48 - 6 - 24, 310, 20, "Replay Mod Settings..."));
}
}
@@ -150,7 +158,7 @@ public class GuiEventHandler {
event.button.enabled = false;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
public void run() {
ReplayHandler.endReplay();
mc.gameSettings.setOptionFloatValue(Options.GAMMA, ReplayHandler.getInitialGamma());
@@ -161,7 +169,7 @@ public class GuiEventHandler {
ReplayGuiRegistry.show();
}
});
t.run();
t.start();
}
}

View File

@@ -17,6 +17,7 @@ public class GuiConstants {
public static final int UPLOAD_BACK_BUTTON = 3005;
public static final int UPLOAD_INFO_FIELD = 3006;
public static final int UPLOAD_TAG_INPUT = 3007;
public static final int UPLOAD_TAG_PLACEHOLDER = 3008;
public static final int REPLAY_OPTIONS_BUTTON_ID = 8000;

View File

@@ -46,6 +46,8 @@ public class GuiReplaySettings extends GuiScreen {
int k = 0;
int i = 0;
for (Entry<String, Object> e : aoptions.entrySet()) {
/*
if(e.getKey().equals("Maximum File Size")) {
float minValue = -1;
float maxValue = 10000;
@@ -54,7 +56,9 @@ public class GuiReplaySettings extends GuiScreen {
int val = (Integer)e.getValue();
this.buttonList.add(new GuiSizeLimitOptionSlider(MAXSIZE_SLIDER_ID, this.width / 2 - 155 + i % 2 * 160, this.height / 6 + 24 * (i >> 1), 0, 39, 1, val, e.getKey()));
} else if(e.getKey().equals("Enable Notifications")) {
} else
*/
if(e.getKey().equals("Enable Notifications")) {
sendChatButton = new GuiButton(SEND_CHAT, this.width / 2 - 155 + i % 2 * 160, this.height / 6 + 24 * (i >> 1), 150, 20, "Enable Notifications: "+onOff((Boolean)e.getValue()));
this.buttonList.add(sendChatButton);
} else if(e.getKey().equals("Record Server")) {
@@ -84,14 +88,13 @@ public class GuiReplaySettings extends GuiScreen {
private String onOff(boolean on) {
return on ? "ON" : "OFF";
}
private String linearOnOff(boolean on) {
return on ? "Linear" : "Cubic";
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawDefaultBackground();
this.drawCenteredString(this.fontRendererObj, "Replay Mod Settings", this.width / 2, 20, 16777215);
if (FMLClientHandler.instance().getClient().thePlayer != null) {
@@ -102,10 +105,8 @@ public class GuiReplaySettings extends GuiScreen {
}
protected void actionPerformed(GuiButton button) throws IOException
{
if (button.enabled)
{
protected void actionPerformed(GuiButton button) throws IOException {
if (button.enabled) {
switch(button.id) {
case 200:
this.mc.displayGuiScreen(this.parentGuiScreen);

View File

@@ -44,7 +44,7 @@ import eu.crushedpixel.replaymod.utils.ImageUtils;
public class GuiUploadFile extends GuiScreen {
private GuiTextField fileTitleInput, tagInput, messageTextField;
private GuiTextField fileTitleInput, tagInput, messageTextField, tagPlaceholder;
private GuiButton categoryButton, startUploadButton, cancelUploadButton, backButton;
private Gson gson = new Gson();
@@ -52,7 +52,7 @@ public class GuiUploadFile extends GuiScreen {
private File replayFile;
private ReplayMetaData metaData;
private BufferedImage thumb;
private FileUploader uploader = new FileUploader();
private Category category = Category.MINIGAME;
@@ -62,6 +62,9 @@ public class GuiUploadFile extends GuiScreen {
private Minecraft mc = Minecraft.getMinecraft();
private static final Pattern p = Pattern.compile("[^a-z0-9 \\-_]", Pattern.CASE_INSENSITIVE);
private static final Pattern pt = Pattern.compile("[^a-z0-9,]", Pattern.CASE_INSENSITIVE);
public GuiUploadFile(File file) {
this.textureResource = new ResourceLocation("upload_thumbs/"+FilenameUtils.getBaseName(file.getAbsolutePath()));
dynTex = null;
@@ -127,48 +130,103 @@ public class GuiUploadFile extends GuiScreen {
public void initGui() {
if(replayFile == null) return;
fileTitleInput = new GuiTextField(GuiConstants.UPLOAD_NAME_INPUT, fontRendererObj, (this.width/2)+20+10, 21, Math.min(200, this.width-20-260), 20);
String fname = FilenameUtils.getBaseName(replayFile.getAbsolutePath());
fileTitleInput.setText(fname);
categoryButton = new GuiButton(GuiConstants.UPLOAD_CATEGORY_BUTTON, (this.width/2)+20+10-1, 80, "Category: "+category.toNiceString());
categoryButton.width = Math.min(202, this.width-20-260+2);
buttonList.add(categoryButton);
List<GuiButton> bottomBar = new ArrayList<GuiButton>();
startUploadButton = new GuiButton(GuiConstants.UPLOAD_START_BUTTON, 0, 0, "Start Upload");
bottomBar.add(startUploadButton);
cancelUploadButton = new GuiButton(GuiConstants.UPLOAD_CANCEL_BUTTON, 0, 0, "Cancel Upload");
cancelUploadButton.enabled = false;
bottomBar.add(cancelUploadButton);
backButton = new GuiButton(GuiConstants.UPLOAD_BACK_BUTTON, 0, 0, "Back");
bottomBar.add(backButton);
messageTextField = new GuiTextField(GuiConstants.UPLOAD_INFO_FIELD, fontRendererObj, 20, height-80, width-40, 20);
messageTextField.setEnabled(true);
messageTextField.setFocused(false);
tagInput = new GuiTextField(GuiConstants.UPLOAD_TAG_INPUT, fontRendererObj, (this.width/2)+20+10, 110, Math.min(200, this.width-20-260), 20);
int i = 0;
for(GuiButton b : bottomBar) {
int w = this.width - 30;
int w2 = w/bottomBar.size();
int x = 15+(w2*i);
b.xPosition = x+2;
b.yPosition = height-30;
b.width = w2-4;
buttonList.add(b);
i++;
if(fileTitleInput == null) {
fileTitleInput = new GuiTextField(GuiConstants.UPLOAD_NAME_INPUT, fontRendererObj, (this.width/2)+20+10, 21, Math.min(200, this.width-20-260), 20);
String fname = FilenameUtils.getBaseName(replayFile.getAbsolutePath());
fileTitleInput.setText(fname);
fileTitleInput.setMaxStringLength(30);
} else {
fileTitleInput.xPosition = (this.width/2)+20+10;
//fileTitleInput.yPosition = 21;
fileTitleInput.width = Math.min(200, this.width-20-260);
//fileTitleInput.height = 20;
}
startUploadButton.enabled = (!(fileTitleInput.getText().trim().length() < 5 || fileTitleInput.getText().trim().length() > 30 ||
p.matcher(fileTitleInput.getText()).find()));
if(categoryButton == null) {
categoryButton = new GuiButton(GuiConstants.UPLOAD_CATEGORY_BUTTON, (this.width/2)+20+10-1, 80, "Category: "+category.toNiceString());
categoryButton.width = Math.min(202, this.width-20-260+2);
buttonList.add(categoryButton);
} else {
categoryButton.xPosition = (this.width/2)+20+10-1;
}
if(startUploadButton == null) {
List<GuiButton> bottomBar = new ArrayList<GuiButton>();
startUploadButton = new GuiButton(GuiConstants.UPLOAD_START_BUTTON, 0, 0, "Start Upload");
bottomBar.add(startUploadButton);
cancelUploadButton = new GuiButton(GuiConstants.UPLOAD_CANCEL_BUTTON, 0, 0, "Cancel Upload");
cancelUploadButton.enabled = false;
bottomBar.add(cancelUploadButton);
backButton = new GuiButton(GuiConstants.UPLOAD_BACK_BUTTON, 0, 0, "Back");
bottomBar.add(backButton);
int i = 0;
for(GuiButton b : bottomBar) {
int w = this.width - 30;
int w2 = w/bottomBar.size();
int x = 15+(w2*i);
b.xPosition = x+2;
b.yPosition = height-30;
b.width = w2-4;
buttonList.add(b);
i++;
}
} else {
List<GuiButton> bottomBar = new ArrayList<GuiButton>();
bottomBar.add(startUploadButton);
bottomBar.add(cancelUploadButton);
bottomBar.add(backButton);
int i = 0;
for(GuiButton b : bottomBar) {
int w = this.width - 30;
int w2 = w/bottomBar.size();
int x = 15+(w2*i);
b.xPosition = x+2;
b.yPosition = height-30;
b.width = w2-4;
buttonList.add(b);
i++;
}
}
if(messageTextField == null) {
messageTextField = new GuiTextField(GuiConstants.UPLOAD_INFO_FIELD, fontRendererObj, 20, height-80, width-40, 20);
messageTextField.setEnabled(true);
messageTextField.setFocused(false);
messageTextField.setMaxStringLength(Integer.MAX_VALUE);
} else {
messageTextField.yPosition = height-80;
messageTextField.width = width-40;
}
if(tagInput == null) {
tagInput = new GuiTextField(GuiConstants.UPLOAD_TAG_INPUT, fontRendererObj, (this.width/2)+20+10, 110, Math.min(200, this.width-20-260), 20);
tagInput.setMaxStringLength(30);
} else {
tagInput.xPosition = (this.width/2)+20+10;
tagInput.width = Math.min(200, this.width-20-260);
}
if(tagPlaceholder == null) {
tagPlaceholder = new GuiTextField(GuiConstants.UPLOAD_TAG_PLACEHOLDER, fontRendererObj, (this.width/2)+20+10, 110, Math.min(200, this.width-20-260), 20);
tagPlaceholder.setTextColor(Color.DARK_GRAY.getRGB());
tagPlaceholder.setText("Tags separated by comma");
} else {
tagPlaceholder.xPosition = (this.width/2)+20+10;
tagPlaceholder.width = Math.min(200, this.width-20-260);
}
validateStartButton();
}
@Override
@@ -185,7 +243,15 @@ public class GuiUploadFile extends GuiScreen {
@Override
public void run() {
try {
uploader.uploadFile(GuiUploadFile.this, AuthenticationHandler.getKey(), name, replayFile, category);
String tagsRaw = tagInput.getText();
String[] split = tagsRaw.split(",");
List<String> tags = new ArrayList<String>();
for(String str : split) {
if(!tags.contains(str) && str.length() > 0) {
tags.add(str);
}
}
uploader.uploadFile(GuiUploadFile.this, AuthenticationHandler.getKey(), name, tags, replayFile, category);
} catch (ApiException e) { //TODO: Error handling
e.printStackTrace();
//mc.displayGuiScreen(new GuiMainMenu());
@@ -232,8 +298,13 @@ public class GuiUploadFile extends GuiScreen {
fileTitleInput.drawTextBox();
messageTextField.drawTextBox();
tagInput.drawTextBox();
if(tagInput.getText().length() > 0 || tagInput.isFocused()) {
tagInput.drawTextBox();
} else {
tagPlaceholder.drawTextBox();
}
super.drawScreen(mouseX, mouseY, partialTicks);
this.drawRect(19, this.height-52, width-19, this.height-37, Color.BLACK.getRGB());
@@ -253,11 +324,13 @@ public class GuiUploadFile extends GuiScreen {
public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
super.mouseClicked(mouseX, mouseY, mouseButton);
fileTitleInput.mouseClicked(mouseX, mouseY, mouseButton);
tagInput.mouseClicked(mouseX, mouseY, mouseButton);
}
@Override
public void updateScreen() {
fileTitleInput.updateCursorCounter();
tagInput.updateCursorCounter();
}
@Override
@@ -265,22 +338,38 @@ public class GuiUploadFile extends GuiScreen {
Keyboard.enableRepeatEvents(false);
}
private static final Pattern p = Pattern.compile("[^a-z0-9 \\-_]", Pattern.CASE_INSENSITIVE);
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
if(fileTitleInput.isFocused()) {
fileTitleInput.textboxKeyTyped(typedChar, keyCode);
} else if(tagInput.isFocused()) {
tagInput.textboxKeyTyped(typedChar, keyCode);
}
if(uploader.isUploading()) {
startUploadButton.enabled = false;
} else {
startUploadButton.enabled = (!(fileTitleInput.getText().trim().length() < 5 || fileTitleInput.getText().trim().length() > 30 ||
p.matcher(fileTitleInput.getText()).find()));
validateStartButton();
}
}
private void validateStartButton() {
boolean enabled = true;
if(fileTitleInput.getText().trim().length() < 5 || fileTitleInput.getText().trim().length() > 30) {
enabled = false;
} else if(p.matcher(fileTitleInput.getText()).find()) {
enabled = false;
fileTitleInput.setTextColor(Color.RED.getRGB());
} else if(pt.matcher(tagInput.getText()).find()) {
enabled = false;
tagInput.setTextColor(Color.RED.getRGB());
} else {
fileTitleInput.setTextColor(Color.WHITE.getRGB());
tagInput.setTextColor(Color.WHITE.getRGB());
}
startUploadButton.enabled = enabled;
}
public void onStartUploading() {
startUploadButton.enabled = false;
cancelUploadButton.enabled = true;
@@ -292,8 +381,7 @@ public class GuiUploadFile extends GuiScreen {
}
public void onFinishUploading(boolean success, String info) {
startUploadButton.enabled = (!(fileTitleInput.getText().trim().length() < 5 || fileTitleInput.getText().trim().length() > 30 ||
p.matcher(fileTitleInput.getText()).find()));
validateStartButton();
cancelUploadButton.enabled = false;
backButton.enabled = true;
categoryButton.enabled = true;

View File

@@ -1,77 +0,0 @@
package eu.crushedpixel.replaymod.online;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
public class CountingHttpEntity extends FileEntity {
private OutputStreamProgress outstream;
public class OutputStreamProgress extends OutputStream {
private final OutputStream outstream;
private volatile long bytesWritten=0;
public OutputStreamProgress(OutputStream outstream) {
this.outstream = outstream;
}
@Override
public void write(int b) throws IOException {
outstream.write(b);
bytesWritten++;
}
@Override
public void write(byte[] b) throws IOException {
outstream.write(b);
bytesWritten += b.length;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
outstream.write(b, off, len);
bytesWritten += len;
}
@Override
public void flush() throws IOException {
outstream.flush();
}
@Override
public void close() throws IOException {
outstream.close();
}
public long getWrittenLength() {
return bytesWritten;
}
}
public CountingHttpEntity(File file, ContentType type) {
super(file, type);
}
@Override
public void writeTo(OutputStream outstream) throws IOException {
this.outstream = new OutputStreamProgress(outstream);
super.writeTo(this.outstream);
}
public float getProgress() {
if (outstream == null) {
return 0;
}
long contentLength = getContentLength();
if (contentLength <= 0) {
return 0;
}
long writtenLength = outstream.getWrittenLength();
return (writtenLength/contentLength);
}
}

View File

@@ -2,6 +2,7 @@ package eu.crushedpixel.replaymod.online.authentication;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import net.minecraft.client.Minecraft;
import eu.crushedpixel.replaymod.ReplayMod;
@@ -26,6 +27,16 @@ public class AuthenticationHandler {
return authkey;
}
private static List<String> blackUUIDs = new ArrayList<String>() {
{
add("23978392a78c49cf9f5235a151fd4083"); //Hudelsohn
}
};
public static boolean isBlacklisted(String uuid) {
return blackUUIDs.contains(uuid.replace("-", ""));
}
public static int authenticate(String username, String password) {
try {
authkey = ReplayMod.apiClient.getLogin(username, password).getAuthkey();

View File

@@ -2,6 +2,7 @@ package eu.crushedpixel.replaymod.renderer;
import java.lang.reflect.Field;
import eu.crushedpixel.replaymod.reflection.MCPNames;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.resources.IResourceManager;
@@ -11,7 +12,7 @@ public class SafeEntityRenderer extends EntityRenderer {
private static Field resourceManager;
static {
try {
resourceManager = EntityRenderer.class.getDeclaredField("resourceManager"); //TODO: MCPNames
resourceManager = EntityRenderer.class.getDeclaredField(MCPNames.field("field_147711_ac"));
resourceManager.setAccessible(true);
} catch(Exception e) {
e.printStackTrace();

View File

@@ -68,7 +68,7 @@ public class PacketDeserializer extends MessageDeserializer {
Field state_by_id = null;
try {
state_by_id = EnumConnectionState.class.getDeclaredField("STATES_BY_ID"); //TODO: Localize
state_by_id = EnumConnectionState.class.getDeclaredField(MCPNames.field("field_150764_e"));
state_by_id.setAccessible(true);
state = (EnumConnectionState)((TIntObjectMap)state_by_id.get(null)).get(i);
TIntObjectMap map = (TIntObjectMap)state_by_id.get(null);

View File

@@ -8,6 +8,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -105,7 +106,16 @@ public class ReplaySender extends ChannelInboundHandlerAdapter {
private Field chatPacketPosition;
private Minecraft mc = Minecraft.getMinecraft();
private Field mcTimer;
public static Field mcTimer;
static {
try {
mcTimer = Minecraft.class.getDeclaredField(MCPNames.field("field_71428_T"));
mcTimer.setAccessible(true);
} catch(Exception e) {
e.printStackTrace();
}
}
private long now = System.currentTimeMillis();
@@ -158,7 +168,6 @@ public class ReplaySender extends ChannelInboundHandlerAdapter {
public void setReplaySpeed(final double d) {
if(d != 0) this.replaySpeed = d;
try {
Timer timer = (Timer)mcTimer.get(mc);
timer.timerSpeed = (float)d;
@@ -185,9 +194,6 @@ public class ReplaySender extends ChannelInboundHandlerAdapter {
joinPacketWorldType = S01PacketJoinGame.class.getDeclaredField(MCPNames.field("field_149201_g"));
joinPacketWorldType.setAccessible(true);
mcTimer = Minecraft.class.getDeclaredField(MCPNames.field("field_71428_T"));
mcTimer.setAccessible(true);
effectPacketEntityId = S1DPacketEntityEffect.class.getDeclaredField(MCPNames.field("field_149434_a"));
effectPacketEntityId.setAccessible(true);
@@ -209,7 +215,7 @@ public class ReplaySender extends ChannelInboundHandlerAdapter {
} catch (Exception e) {
e.printStackTrace();
}
ReplayHandler.setInitialGamma(mc.gameSettings.gammaSetting);
this.replayFile = replayFile;
@@ -332,9 +338,9 @@ public class ReplaySender extends ChannelInboundHandlerAdapter {
hasRestarted = false;
}
} catch(IOException eof) {
} catch(EOFException eof) {
setReplaySpeed(0);
}
} catch(IOException e) {}
}
}
} catch(Exception e) {