Added GuiRegister to allow users to create a new account on ReplayMod.com from the Mod itself

This commit is contained in:
CrushedPixel
2015-05-03 21:58:24 +02:00
parent 38ca7a0054
commit 5a3e3575c3
11 changed files with 327 additions and 27 deletions

View File

@@ -29,6 +29,15 @@ public class ApiClient {
return auth;
}
public AuthKey register(String username, String mail, String password) throws IOException, ApiException {
QueryBuilder builder = new QueryBuilder(ApiMethods.register);
builder.put("username", username);
builder.put("email", mail);
builder.put("password", password);
AuthKey auth = invokeAndReturn(builder, AuthKey.class);
return auth;
}
public boolean logout(String auth) throws IOException, ApiException {
QueryBuilder builder = new QueryBuilder(ApiMethods.logout);
builder.put("auth", auth);

View File

@@ -2,6 +2,7 @@ package eu.crushedpixel.replaymod.api.client;
public class ApiMethods {
public static final String register = "register";
public static final String login = "login";
public static final String logout = "logout";
public static final String replay_files = "replay_files";

View File

@@ -31,6 +31,16 @@ public class GuiConstants {
public static final int LOGIN_OKAY_BUTTON = 1100;
public static final int LOGIN_CANCEL_BUTTON = 1101;
public static final int LOGIN_REGISTER_BUTTON = 1102;
public static final int LOGIN_USERNAME_FIELD = 1103;
public static final int LOGIN_PASSWORD_FIELD = 1104;
public static final int REGISTER_USERNAME_FIELD = 2000;
public static final int REGISTER_PASSWORD_FIELD = 2001;
public static final int REGISTER_PASSWORD_CONFIRM_FIELD = 2002;
public static final int REGISTER_CANCEL_BUTTON = 2003;
public static final int REGISTER_OKAY_BUTTON = 2004;
public static final int REGISTER_MAIL_FIELD = 2005;
public static final int REPLAY_EDITOR_TRIM_TAB = 5000;
public static final int REPLAY_EDITOR_CONNECT_TAB = 5001;

View File

@@ -47,6 +47,8 @@ public class GuiKeyframeRepository extends GuiScreen {
@Override
public void initGui() {
Keyboard.enableRepeatEvents(true);
int h = (int)Math.floor(((double)this.height-(45+20+15))/14);
if(!initialized) {
@@ -226,4 +228,9 @@ public class GuiKeyframeRepository extends GuiScreen {
ReplayHandler.setKeyframeRepository(keyframeRepository, true);
ReplayMod.replaySender.setReplaySpeed(prevSpeed);
}
@Override
public void onGuiClosed() {
Keyboard.enableRepeatEvents(false);
}
}

View File

@@ -26,6 +26,16 @@ public class GuiLoginPrompt extends GuiScreen {
private PasswordTextField password;
private GuiButton loginButton;
private GuiButton cancelButton;
private GuiButton registerButton;
private String noacc;
private int strwidth;
private boolean initialized = false;
public GuiScreen getSuccessScreen() {
return successScreen;
}
public GuiLoginPrompt(GuiScreen parent, GuiScreen successScreen) {
this.parent = parent;
@@ -36,22 +46,44 @@ public class GuiLoginPrompt extends GuiScreen {
public void initGui() {
Keyboard.enableRepeatEvents(true);
username = new GuiTextField(GuiConstants.REPLAY_CENTER_LOGIN_TEXT_ID, fontRendererObj, this.width / 2 - 45, 30, 145, 20);
username.setEnabled(true);
username.setFocused(true);
if(!initialized) {
username = new GuiTextField(GuiConstants.LOGIN_USERNAME_FIELD, fontRendererObj, this.width / 2 - 45, 30, 145, 20);
username.setEnabled(true);
username.setFocused(true);
password = new PasswordTextField(GuiConstants.REPLAY_CENTER_PASSWORD_TEXT_ID, fontRendererObj, this.width / 2 - 45, 60, 145, 20);
password.setEnabled(true);
password.setFocused(false);
password = new PasswordTextField(GuiConstants.LOGIN_PASSWORD_FIELD, fontRendererObj, this.width / 2 - 45, 60, 145, 20);
password.setEnabled(true);
password.setFocused(false);
loginButton = new GuiButton(GuiConstants.LOGIN_OKAY_BUTTON, this.width / 2 - 150 - 2, 110, I18n.format("replaymod.gui.login"));
loginButton.enabled = false;
loginButton.width = 150;
cancelButton = new GuiButton(GuiConstants.LOGIN_CANCEL_BUTTON, this.width / 2 + 2, 110, I18n.format("replaymod.gui.cancel"));
cancelButton.width = 150;
registerButton = new GuiButton(GuiConstants.LOGIN_REGISTER_BUTTON, 0, this.height-30, I18n.format("replaymod.gui.register"));
registerButton.width = 150;
} else {
username.xPosition = password.xPosition = this.width / 2 - 45;
loginButton.xPosition = this.width / 2 - 150 - 2;
cancelButton.xPosition = this.width / 2 + 2;
registerButton.yPosition = this.height-30;
}
noacc = I18n.format("replaymod.gui.login.noacc");
strwidth = fontRendererObj.getStringWidth(noacc);
int tw = 150+5+strwidth;
registerButton.xPosition = (width/2) - (tw/2) + strwidth+5;
loginButton = new GuiButton(GuiConstants.LOGIN_OKAY_BUTTON, this.width / 2 - 150 - 2, 110, I18n.format("replaymod.gui.login"));
loginButton.width = 150;
loginButton.enabled = false;
buttonList.add(loginButton);
cancelButton = new GuiButton(GuiConstants.LOGIN_CANCEL_BUTTON, this.width / 2 + 2, 110, I18n.format("replaymod.gui.cancel"));
cancelButton.width = 150;
buttonList.add(cancelButton);
buttonList.add(registerButton);
strwidth2 = Math.max(fontRendererObj.getStringWidth(usernameLabel), fontRendererObj.getStringWidth(passwordLabel));
initialized = true;
}
@Override
@@ -86,18 +118,24 @@ public class GuiLoginPrompt extends GuiScreen {
}
} else if(button.id == GuiConstants.LOGIN_CANCEL_BUTTON) {
mc.displayGuiScreen(parent);
} else if(button.id == GuiConstants.LOGIN_REGISTER_BUTTON) {
mc.displayGuiScreen(new GuiRegister(this));
}
}
private String usernameLabel = I18n.format("replaymod.gui.username");
private String passwordLabel = I18n.format("replaymod.gui.password");
private int strwidth2 = 100;
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawDefaultBackground();
drawCenteredString(fontRendererObj, I18n.format("replaymod.gui.login.title"), this.width / 2, 10, Color.WHITE.getRGB());
drawString(fontRendererObj, I18n.format("replaymod.gui.username"), this.width / 2 - 100, 37, Color.WHITE.getRGB());
drawString(fontRendererObj, usernameLabel, this.width / 2 - (45+10+strwidth2), 37, Color.WHITE.getRGB());
username.drawTextBox();
drawString(fontRendererObj, I18n.format("replaymod.gui.password"), this.width / 2 - 100, 67, Color.WHITE.getRGB());
drawString(fontRendererObj, passwordLabel, this.width / 2 - (45+10+strwidth2), 67, Color.WHITE.getRGB());
password.drawTextBox();
switch(textState) {
@@ -111,6 +149,10 @@ public class GuiLoginPrompt extends GuiScreen {
drawCenteredString(fontRendererObj, I18n.format("replaymod.gui.login.connectionerror"), this.width / 2, 92, Color.RED.getRGB());
break;
}
int tw = 150+5+strwidth;
drawString(fontRendererObj, noacc, this.width / 2 - (tw/2), this.height-22, Color.WHITE.getRGB());
super.drawScreen(mouseX, mouseY, partialTicks);
}
@@ -143,8 +185,7 @@ public class GuiLoginPrompt extends GuiScreen {
password.setFocused(true);
}
return;
}
if(keyCode == 28) { //Enter key
} else if(keyCode == Keyboard.KEY_RETURN) {
actionPerformed(loginButton);
return;
}
@@ -154,5 +195,7 @@ public class GuiLoginPrompt extends GuiScreen {
password.textboxKeyTyped(typedChar, keyCode);
}
loginButton.enabled = username.getText().length() > 0 && password.getText().length() > 0;
super.keyTyped(typedChar, keyCode);
}
}

View File

@@ -0,0 +1,202 @@
package eu.crushedpixel.replaymod.gui.online;
import eu.crushedpixel.replaymod.api.client.ApiException;
import eu.crushedpixel.replaymod.gui.GuiConstants;
import eu.crushedpixel.replaymod.gui.PasswordTextField;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
import eu.crushedpixel.replaymod.utils.EmailAddressUtils;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.resources.I18n;
import org.lwjgl.input.Keyboard;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class GuiRegister extends GuiScreen {
private String message = null;
private GuiTextField usernameInput, mailInput;
private PasswordTextField passwordInput, passwordConfirmation;
private GuiButton registerButton, cancelButton;
private boolean initialized = false;
private int strwidth = 0;
private String[] labels = new String[]{I18n.format("replaymod.gui.username"), I18n.format("replaymod.gui.mail"), I18n.format("replaymod.gui.password"),
I18n.format("replaymod.gui.register.confirmpw")};
private List<GuiTextField> inputFields;
private GuiLoginPrompt parent = null;
public GuiRegister(GuiLoginPrompt parent) {
this.parent = parent;
}
@Override
public void initGui() {
Keyboard.enableRepeatEvents(true);
if(!initialized) {
usernameInput = new GuiTextField(GuiConstants.REGISTER_USERNAME_FIELD, fontRendererObj, this.width / 2 - 45, 30, 145, 20);
mailInput = new GuiTextField(GuiConstants.REGISTER_MAIL_FIELD, fontRendererObj, this.width / 2 - 45, 60, 145, 20);
passwordInput = new PasswordTextField(GuiConstants.REGISTER_PASSWORD_FIELD, fontRendererObj, this.width / 2 - 45, 90, 145, 20);
passwordConfirmation = new PasswordTextField(GuiConstants.REGISTER_PASSWORD_CONFIRM_FIELD, fontRendererObj, this.width / 2 - 45, 120, 145, 20);
registerButton = new GuiButton(GuiConstants.REGISTER_OKAY_BUTTON, this.width / 2 - 150 - 2, 170, I18n.format("replaymod.gui.register"));
registerButton.enabled = false;
registerButton.width = 150;
cancelButton = new GuiButton(GuiConstants.REGISTER_CANCEL_BUTTON, this.width / 2 + 2, 170, I18n.format("replaymod.gui.cancel"));
cancelButton.width = 150;
} else {
usernameInput.xPosition = mailInput.xPosition = passwordInput.xPosition = passwordConfirmation.xPosition = this.width / 2 - 45;
registerButton.xPosition = this.width / 2 - 150 - 2;
cancelButton.xPosition = this.width / 2 + 2;
}
inputFields = new ArrayList<GuiTextField>();
inputFields.add(usernameInput);
inputFields.add(mailInput);
inputFields.add(passwordInput);
inputFields.add(passwordConfirmation);
buttonList.add(registerButton);
buttonList.add(cancelButton);
strwidth = Math.max(Math.max(fontRendererObj.getStringWidth(labels[0]), fontRendererObj.getStringWidth(labels[1])),
Math.max(fontRendererObj.getStringWidth(labels[2]), fontRendererObj.getStringWidth(labels[3])));
initialized = true;
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawDefaultBackground();
for(GuiTextField f : inputFields) {
f.drawTextBox();
}
int i = 0;
for(String label : labels) {
drawString(fontRendererObj, label, this.width / 2 - (45 + 10 + strwidth), 37+(i*30), Color.WHITE.getRGB());
i++;
}
if(message != null) {
drawCenteredString(fontRendererObj, message, this.width / 2, 152, Color.RED.getRGB());
}
super.drawScreen(mouseX, mouseY, partialTicks);
}
@Override
public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
super.mouseClicked(mouseX, mouseY, mouseButton);
for(GuiTextField f : inputFields) {
f.mouseClicked(mouseX, mouseY, mouseButton);
}
}
@Override
public void updateScreen() {
for(GuiTextField f : inputFields) {
f.updateCursorCounter();
}
}
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
if(keyCode == Keyboard.KEY_TAB) {
int i = 0;
for(GuiTextField f : inputFields) {
if(f.isFocused()) {
i = inputFields.indexOf(f);
f.setFocused(false);
}
}
if(i == inputFields.size()-1) {
i = 0;
} else {
i++;
}
inputFields.get(i).setFocused(true);
} else if(keyCode == Keyboard.KEY_RETURN) {
actionPerformed(registerButton);
return;
}
for(GuiTextField f : inputFields) {
if(f.isFocused()) f.textboxKeyTyped(typedChar, keyCode);
}
registerButton.enabled = usernameInput.getText().length() >= 5 && EmailAddressUtils.isValidEmailAddress(mailInput.getText())
&& passwordInput.getText().length() >= 5 && passwordConfirmation.getText().equals(passwordInput.getText());
//only show message if user has filled out every text field
if(!registerButton.enabled && usernameInput.getText().length() > 0 && mailInput.getText().length() > 0
&& passwordInput.getText().length() > 0 && passwordConfirmation.getText().length() > 0) {
if(usernameInput.getText().length() < 5) {
message = I18n.format("replaymod.gui.register.error.shortusername");
} else if(!EmailAddressUtils.isValidEmailAddress(mailInput.getText())) {
message = I18n.format("replaymod.gui.register.error.invalidmail");
} else if(!(passwordInput.getText().length() >= 5)) {
message = I18n.format("replaymod.gui.register.error.shortpw");
} else if(!passwordConfirmation.getText().equals(passwordInput.getText())) {
message = I18n.format("replaymod.gui.register.error.nomatch");
}
} else {
message = null;
}
super.keyTyped(typedChar, keyCode);
}
@Override
public void onGuiClosed() {
Keyboard.enableRepeatEvents(false);
}
@Override
protected void actionPerformed(GuiButton button) {
if(!button.enabled) return;
switch(button.id) {
case GuiConstants.REGISTER_CANCEL_BUTTON:
mc.displayGuiScreen(parent);
break;
case GuiConstants.REGISTER_OKAY_BUTTON:
message = I18n.format("replaymod.gui.login.logging");
new Thread(new Runnable() {
@Override
public void run() {
try {
AuthenticationHandler.register(usernameInput.getText().trim(),
mailInput.getText().trim(), passwordInput.getText());
mc.addScheduledTask(new Runnable() {
@Override
public void run() {
mc.displayGuiScreen(parent.getSuccessScreen());
}
});
} catch(ApiException ae) {
message = ae.getLocalizedMessage();
} catch(Exception e) {
message = I18n.format("replaymod.gui.login.connectionerror");
}
}
}).start();
break;
}
}
}

View File

@@ -44,16 +44,10 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
private static final int SETTINGS_BUTTON_ID = 9006;
private static final int CANCEL_BUTTON_ID = 9007;
private static Gson gson = new Gson();
private GuiScreen parentScreen;
private GuiButton btnEditServer;
private GuiButton btnSelectServer;
private GuiButton btnDeleteServer;
private String hoveringText;
private boolean initialized;
private GuiReplayListExtended replayGuiList;
private List<Pair<Pair<File, ReplayMetaData>, File>> replayFileList = new ArrayList<Pair<Pair<File, ReplayMetaData>, File>>();
private GuiButton loadButton, uploadButton, folderButton, renameButton, deleteButton, cancelButton, settingsButton;
private boolean replaying = false;
private boolean delete_file = false;
public static GuiYesNo getYesNoGui(GuiYesNoCallback p_152129_0_, String file, int p_152129_2_) {
@@ -72,7 +66,6 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
for(File file : ReplayFileIO.getAllReplayFiles()) {
try {
ZipFile archive = new ZipFile(file);
ZipArchiveEntry recfile = archive.getEntry("recording" + ConnectionEventHandler.TEMP_FILE_EXTENSION);
ZipArchiveEntry metadata = archive.getEntry("metaData" + ConnectionEventHandler.JSON_FILE_EXTENSION);
ZipArchiveEntry image = archive.getEntry("thumb");
@@ -164,7 +157,6 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.hoveringText = null;
this.drawDefaultBackground();
this.replayGuiList.drawScreen(mouseX, mouseY, partialTicks);
this.drawCenteredString(this.fontRendererObj, I18n.format("replaymod.gui.replayviewer"), this.width / 2, 20, 16777215);
@@ -188,7 +180,7 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
if(button.id == LOAD_BUTTON_ID) {
loadReplay(replayGuiList.selected);
} else if(button.id == CANCEL_BUTTON_ID) {
mc.displayGuiScreen(parentScreen);
mc.displayGuiScreen(null);
} else if(button.id == DELETE_BUTTON_ID) {
String s = replayGuiList.getListEntry(replayGuiList.selected).getFileInfo().getName();

View File

@@ -3,6 +3,7 @@ package eu.crushedpixel.replaymod.online.authentication;
import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.client.ApiClient;
import eu.crushedpixel.replaymod.api.client.ApiException;
import eu.crushedpixel.replaymod.api.client.holders.AuthKey;
import java.io.IOException;
@@ -28,6 +29,12 @@ public class AuthenticationHandler {
return apiClient.hasDonated(uuid);
}
public static void register(String username, String mail, String password) throws IOException, ApiException {
AuthKey auth = apiClient.register(username, mail, password);
authkey = auth.getAuthkey();
}
public static int authenticate(String username, String password) {
try {
authkey = ReplayMod.apiClient.getLogin(username, password).getAuthkey();

View File

@@ -1,7 +1,6 @@
package eu.crushedpixel.replaymod.registry;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import org.apache.commons.io.FileUtils;
import java.io.File;
@@ -21,7 +20,7 @@ public class UploadedFileHandler {
configuration = new Configuration(confFile);
configuration.load();
Property uploaded = configuration.get("uploaded", "hashes", new String[0]);
configuration.get("uploaded", "hashes", new String[0]);
configuration.save();
} catch(Exception e) {

View File

@@ -0,0 +1,17 @@
package eu.crushedpixel.replaymod.utils;
import java.net.URLEncoder;
public class EmailAddressUtils {
public static boolean isValidEmailAddress(String mail) {
try {
String[] spl1 = mail.split("@");
String[] spl2 = spl1[1].split("\\.");
String suffix = spl2[1];
return spl1[0].equals(URLEncoder.encode(spl1[0], "UTF-8")) && spl1[1].equals(URLEncoder.encode(spl1[1], "UTF-8"));
} catch(Exception e) {
return false;
}
}
}

View File

@@ -64,6 +64,8 @@ replaymod.gui.end=End
replaymod.gui.delete=Delete
replaymod.gui.recording=Recording
replaymod.gui.speed=Speed
replaymod.gui.register=Register
replaymod.gui.mail=e-Mail Address
replaymod.gui.unknownerror=An unknown error occured
@@ -89,6 +91,15 @@ replaymod.gui.login.logging=Logging in...
replaymod.gui.login.incorrect=Incorrect username or password
replaymod.gui.login.connectionerror=Could not connect to ReplayMod.com
replaymod.gui.register.title=Register on ReplayMod.com
replaymod.gui.register.confirmpw=Confirm Password
replaymod.gui.register.error.nomatch=Passwords don't match
replaymod.gui.register.error.shortusername=Username has to be at least 5 characters long
replaymod.gui.register.error.shortpw=Password has to be at least 5 characters long
replaymod.gui.register.error.invalidmail=Invalid eMail Address
#Replay Viewer GUI
replaymod.gui.viewer.rename.title=Rename Replay
replaymod.gui.viewer.rename.name=Replay Name
@@ -101,6 +112,8 @@ 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!..."
replaymod.gui.viewer.delete.lineb=will be lost forever! (a long time!)
replaymod.gui.login.noacc=Don't have an account yet?
#Replay Center GUI
replaymod.gui.center.logoutcallback=Do you really want to log out?
replaymod.gui.center.newest=Newest Replays