Fix all warnings

Add and make use of Lombok
Remove Mojang API
Remove ZipFileUtils
Remove StreamTools in favor of Apache IOUtils
Keyframe should be abstract all derivatives final
Replace clone in Keyframe with copy
Move some constants from GuiReplaySetttings to GuiConstants
This commit is contained in:
johni0702
2015-06-29 21:45:37 +02:00
parent 3122c0a71e
commit a058497727
110 changed files with 487 additions and 1921 deletions

View File

@@ -5,13 +5,10 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.mojang.authlib.exceptions.AuthenticationException;
import eu.crushedpixel.replaymod.api.mojang.MojangApiMethods;
import eu.crushedpixel.replaymod.api.mojang.holders.Profile;
import eu.crushedpixel.replaymod.api.replay.ReplayModApiMethods;
import eu.crushedpixel.replaymod.api.replay.SearchQuery;
import eu.crushedpixel.replaymod.api.replay.holders.*;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHash;
import eu.crushedpixel.replaymod.utils.StreamTools;
import net.minecraft.client.Minecraft;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
@@ -20,7 +17,6 @@ import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.UUID;
public class ApiClient {
@@ -33,8 +29,7 @@ public class ApiClient {
builder.put("user", username);
builder.put("pw", password);
builder.put("mod", true);
AuthKey auth = invokeAndReturn(builder, AuthKey.class);
return auth;
return invokeAndReturn(builder, AuthKey.class);
}
public AuthKey register(String username, String mail, String password, String uuid)
@@ -50,8 +45,7 @@ public class ApiClient {
builder.put("mcusername", authenticationHash.username);
builder.put("timelong", authenticationHash.currentTime);
builder.put("randomlong", authenticationHash.randomLong);
AuthKey auth = invokeAndReturn(builder, AuthKey.class);
return auth;
return invokeAndReturn(builder, AuthKey.class);
}
private AuthenticationHash sessionserverJoin() throws AuthenticationException {
@@ -67,8 +61,7 @@ public class ApiClient {
try {
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.check_authkey);
builder.put("auth", auth);
AuthConfirmation conf = invokeAndReturn(builder, AuthConfirmation.class);
return conf;
return invokeAndReturn(builder, AuthConfirmation.class);
} catch(Exception e) {
return null;
}
@@ -89,33 +82,21 @@ public class ApiClient {
return succ.hasDonated();
}
@Deprecated
public UserFiles getUserFiles(String auth, String user) throws IOException, ApiException {
//TODO if required
return null;
}
public FileInfo[] getFileInfo(List<Integer> ids) throws IOException, ApiException {
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.file_details);
builder.put("id", buildListString(ids));
FileInfo[] info = invokeAndReturn(builder, FileInfo[].class);
return info;
return invokeAndReturn(builder, FileInfo[].class);
}
public FileInfo[] searchFiles(SearchQuery query) throws IOException, ApiException {
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.search);
StringBuilder sb = new StringBuilder(builder.toString());
sb.append(query.buildQuery());
FileInfo[] info = invokeAndReturn(sb.toString(), SearchResult.class).getResults();
return info;
return invokeAndReturn(builder.toString() + query.buildQuery(), SearchResult.class).getResults();
}
public String getTranslation(String languageCode) throws IOException, ApiException {
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.get_language);
builder.put("language", languageCode);
String properties = SimpleApiClient.invokeUrl(builder.toString());
return properties;
return SimpleApiClient.invokeUrl(builder.toString());
}
public void downloadThumbnail(int file, File target) throws IOException {
@@ -142,14 +123,14 @@ public class ApiClient {
out.close();
}
} else {
JsonElement element = jsonParser.parse(StreamTools.readStreamtoString(is));
JsonElement element = jsonParser.parse(IOUtils.toString(is));
try {
ApiError err = gson.fromJson(element, ApiError.class);
if(err.getDesc() != null) {
throw new ApiException(err);
}
} catch(JsonParseException e) {
throw new ApiException(StreamTools.readStreamtoString(is));
throw new ApiException(IOUtils.toString(is));
}
}
}
@@ -191,15 +172,6 @@ public class ApiClient {
invokeAndReturn(builder, Success.class);
}
/*
MOJANG API CALLS
*/
public Profile getProfileFromUUID(UUID uuid) throws IOException, ApiException {
String url = String.format(MojangApiMethods.userprofile+"%s", uuid.toString().replace("-", ""));
return invokeAndReturn(url, Profile.class);
}
private <T> T invokeAndReturn(QueryBuilder builder, Class<T> classOfT) throws IOException, ApiException {
return invokeAndReturn(builder.toString(), classOfT);
}

View File

@@ -1,31 +1,28 @@
package eu.crushedpixel.replaymod.api;
import eu.crushedpixel.replaymod.api.replay.holders.ApiError;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
public class ApiException extends Exception {
private static final long serialVersionUID = 349073390504232810L;
private ApiError error;
private String errorMsg;
public ApiException(ApiError error) {
super(error.getTranslatedDesc());
this.error = error;
this.errorMsg = error.getTranslatedDesc();
}
public ApiException(String error) {
super(error);
this.errorMsg = error;
}
public ApiError getError() {
return error;
}
public String getErrorMsg() {
return errorMsg;
}
}

View File

@@ -4,7 +4,6 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.Map;
public class GsonApiClient {
@@ -20,18 +19,7 @@ public class GsonApiClient {
return wrapWithJson(apiResult);
}
public static JsonElement invokeJson(String apiKey, String method, Map<String, Object> paramMap) throws IOException, ApiException {
String apiResult = SimpleApiClient.invoke(method, paramMap);
return wrapWithJson(apiResult);
}
public static JsonElement invokeJson(String apiKey, String method) throws IOException, ApiException {
String apiResult = SimpleApiClient.invoke(method, null);
return wrapWithJson(apiResult);
}
private static JsonElement wrapWithJson(String apiResult) {
JsonElement element = parser.parse(apiResult);
return element;
return parser.parse(apiResult);
}
}

View File

@@ -10,32 +10,10 @@ public class QueryBuilder {
public String apiMethod;
public Map<String, String> paramMap;
public QueryBuilder() {
this(null);
}
public QueryBuilder(String apiMethod) {
this.apiMethod = apiMethod;
}
public QueryBuilder(String apiMethod, String key, String value) {
this(apiMethod);
put(key, value);
}
public QueryBuilder(String apiMethod, String key1, String value1, String key2, String value2) {
this(apiMethod);
put(key1, value1);
put(key2, value2);
}
public QueryBuilder(String apiMethod, String key1, String value1, String key2, String value2, String key3, String value3) {
this(apiMethod);
put(key1, value1);
put(key2, value2);
put(key3, value3);
}
public void put(String key, Object value) {
if(key != null && value != null) {
if(paramMap == null) {
@@ -45,17 +23,6 @@ public class QueryBuilder {
}
}
public void put(String key1, Object value1, String key2, Object value2) {
put(key1, value1);
put(key2, value2);
}
public void put(String key1, Object value1, String key2, Object value2, String key3, Object value3) {
put(key1, value1);
put(key2, value2);
put(key3, value3);
}
public void put(Map<String, Object> paraMap) {
if(paraMap == null) return;
for(String key : paraMap.keySet()) {

View File

@@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import eu.crushedpixel.replaymod.api.replay.holders.ApiError;
import eu.crushedpixel.replaymod.utils.StreamTools;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
@@ -88,7 +88,7 @@ public class SimpleApiClient {
if(responseCode != 200) {
is = httpUrlConnection.getErrorStream();
if(is != null) {
responseContent = StreamTools.readStreamtoString(is, "UTF-8");
responseContent = IOUtils.toString(is, "UTF-8");
} else {
responseContent = "";
}
@@ -102,10 +102,8 @@ public class SimpleApiClient {
is = httpUrlConnection.getInputStream();
responseContent = StreamTools.readStreamtoString(is, "UTF-8");
responseContent = IOUtils.toString(is, "UTF-8");
} catch(IOException e) {
throw e;
} finally {
if(is != null) {
is.close();

View File

@@ -1,8 +0,0 @@
package eu.crushedpixel.replaymod.api.mojang;
public class MojangApiMethods {
public static final String userprofile = "https://sessionserver.mojang.com/session/minecraft/profile/";
public static final String joinServer = "https://sessionserver.mojang.com/session/minecraft/join";
}

View File

@@ -1,57 +0,0 @@
package eu.crushedpixel.replaymod.api.mojang;
import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.ApiException;
import eu.crushedpixel.replaymod.api.mojang.holders.Profile;
import eu.crushedpixel.replaymod.api.mojang.holders.Properties;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ImageBufferDownload;
import net.minecraft.client.renderer.ThreadDownloadImageData;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.util.ResourceLocation;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
public class SkinDownloader {
public static ITextureObject getDownloadImageSkin(ResourceLocation resourceLocationIn, UUID uuid) throws IOException, ApiException {
TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager();
ITextureObject object = texturemanager.getTexture(resourceLocationIn);
try {
if(object == null) {
Profile profile = ReplayMod.apiClient.getProfileFromUUID(uuid);
if(profile.getProperties() != null && profile.getProperties().length > 0) {
List<Properties> plist = Arrays.asList(profile.getProperties());
Collections.reverse(plist);
for(Properties p : plist) {
if(p.getName().equals("textures")) {
object = new ThreadDownloadImageData((File) null,
p.getTextureValue().getTextures().getSKIN().getUrl(),
DefaultPlayerSkin.getDefaultSkin(uuid), new ImageBufferDownload());
}
}
}
}
} catch(Exception e) {
e.printStackTrace();
object = new ThreadDownloadImageData((File)null,
null,
DefaultPlayerSkin.getDefaultSkin(uuid), new ImageBufferDownload());
}
return object;
}
}

View File

@@ -1,32 +0,0 @@
package eu.crushedpixel.replaymod.api.mojang.holders;
public class Profile {
private String id;
private String name;
private Properties[] properties;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Properties[] getProperties() {
return properties;
}
public void setProperties(Properties[] properties) {
this.properties = properties;
}
}

View File

@@ -1,42 +0,0 @@
package eu.crushedpixel.replaymod.api.mojang.holders;
import com.google.gson.Gson;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
public class Properties {
private String name;
private String value;
private String signature;
public String getName() {
return name;
}
public TextureValue getTextureValue() {
Gson gson = new Gson();
return gson.fromJson(StringUtils.newStringUtf8(Base64.decodeBase64(value)), TextureValue.class);
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
}

View File

@@ -1,49 +0,0 @@
package eu.crushedpixel.replaymod.api.mojang.holders;
public class TextureValue {
private long timestamp;
private String profileId;
private String profileName;
private boolean isPublic;
private Textures textures;
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getProfileId() {
return profileId;
}
public void setProfileId(String profileId) {
this.profileId = profileId;
}
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}
public boolean isPublic() {
return isPublic;
}
public void setIsPublic(boolean isPublic) {
this.isPublic = isPublic;
}
public Textures getTextures() {
return textures;
}
public void setTextures(Textures textures) {
this.textures = textures;
}
}

View File

@@ -1,22 +0,0 @@
package eu.crushedpixel.replaymod.api.mojang.holders;
public class Textures {
private UrlHolder SKIN;
private UrlHolder CAPE;
public UrlHolder getSKIN() {
return SKIN;
}
public void setSKIN(UrlHolder SKIN) {
this.SKIN = SKIN;
}
public UrlHolder getCAPE() {
return CAPE;
}
public void setCAPE(UrlHolder CAPE) {
this.CAPE = CAPE;
}
}

View File

@@ -1,13 +0,0 @@
package eu.crushedpixel.replaymod.api.mojang.holders;
public class UrlHolder {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@@ -11,7 +11,6 @@ import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
public class FileUploader {
@@ -21,14 +20,8 @@ public class FileUploader {
private long filesize;
private long current;
private String attachmentName = "file";
private String attachmentFileName = "file.mcpr";
private String crlf = "\r\n";
private String twoHyphens = "--";
private boolean cancel = false;
private String boundary = "*****";
private GuiUploadFile parent;
public void uploadFile(GuiUploadFile gui, String auth, String filename, List<String> tags, File file, Category category, String description)
@@ -62,7 +55,7 @@ public class FileUploader {
postData += "&name=" + URLEncoder.encode(filename, "UTF-8");
String url = "http://ReplayMod.com/api/upload_file" + postData;
String url = ReplayModApiMethods.upload_file + postData;
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
@@ -70,18 +63,18 @@ public class FileUploader {
con.setChunkedStreamingMode(1024);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + this.boundary);
HashMap<String, String> params = new HashMap<String, String>();
params.put("auth", auth);
params.put("name", filename);
params.put("category", category.getId() + "");
String boundary = "*****";
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream request = new DataOutputStream(con.getOutputStream());
request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + this.attachmentName + "\";filename=\"" + this.attachmentFileName + "\"" + this.crlf);
request.writeBytes(this.crlf);
String crlf = "\r\n";
String twoHyphens = "--";
request.writeBytes(twoHyphens + boundary + crlf);
String attachmentName = "file";
String attachmentFileName = "file.mcpr";
request.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\"" + attachmentFileName + "\"" + crlf);
request.writeBytes(crlf);
byte[] buf = new byte[1024];
FileInputStream fis = new FileInputStream(file);
@@ -102,8 +95,8 @@ public class FileUploader {
}
fis.close();
request.writeBytes(this.crlf);
request.writeBytes(this.twoHyphens + this.boundary + this.twoHyphens + this.crlf);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
request.flush();
request.close();

View File

@@ -1,32 +1,17 @@
package eu.crushedpixel.replaymod.api.replay;
import lombok.AllArgsConstructor;
import java.lang.reflect.Field;
import java.net.URLEncoder;
@AllArgsConstructor
public class SearchQuery {
public Boolean order, singleplayer;
public String player, tag, version, server, name, auth;
public Integer category, offset;
public SearchQuery() {
}
public SearchQuery(Boolean order, Boolean singleplayer, String player,
String tag, String version, String server, String name,
String auth, Integer category, Integer offset) {
this.order = order;
this.singleplayer = singleplayer;
this.player = player;
this.tag = tag;
this.version = version;
this.server = server;
this.name = name;
this.auth = auth;
this.category = category;
this.offset = offset;
}
public String buildQuery() {
String query = "";
boolean first = true;

View File

@@ -1,7 +1,9 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
import net.minecraft.client.resources.I18n;
@Data
public class ApiError {
private int id;
@@ -9,27 +11,6 @@ public class ApiError {
private String key;
private String[] objects;
public ApiError(int id, String desc) {
this.id = id;
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTranslatedDesc() {
try {
return I18n.format(key, (Object[]) objects);

View File

@@ -1,10 +1,9 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
@Data
public class AuthConfirmation {
private String username;
private int id;
public String getUsername() {
return username;
}
}

View File

@@ -1,14 +1,12 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AuthKey {
private String auth;
public AuthKey(String authkey) {
this.auth = authkey;
}
public String getAuthkey() {
return auth;
}
}

View File

@@ -1,7 +1,13 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
@Data
public class Donated {
@Getter(AccessLevel.NONE)
private boolean donated = false;
public boolean hasDonated() {

View File

@@ -1,7 +1,8 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
@Data
public class Favorites {
private int[] favorited;
public int[] getFavorited() { return favorited; }
}

View File

@@ -1,9 +1,14 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
@Data
@AllArgsConstructor
public class FileInfo {
private int id;
private ReplayMetaData metadata;
private String owner;
@@ -11,67 +16,12 @@ public class FileInfo {
private int size;
private int category;
private int downloads;
private int favorites;
private String name;
@Getter(AccessLevel.NONE)
private boolean thumbnail;
public FileInfo(int id, ReplayMetaData metadata, String owner,
Rating ratings, int size, int category, int downloads, String name,
boolean thumbnail, int favorites) {
this.id = id;
this.metadata = metadata;
this.owner = owner;
this.ratings = ratings;
this.size = size;
this.category = category;
this.downloads = downloads;
this.name = name;
this.thumbnail = thumbnail;
this.favorites = favorites;
}
public int getId() {
return id;
}
public ReplayMetaData getMetadata() {
return metadata;
}
public String getOwner() {
return owner;
}
public Rating getRatings() {
return ratings;
}
public int getSize() {
return size;
}
public int getCategory() {
return category;
}
public int getDownloads() {
return downloads;
}
public int getFavorites() { return favorites; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int favorites;
public boolean hasThumbnail() {
return thumbnail;
}
}

View File

@@ -1,5 +1,8 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
@Data
public class FileRating {
private int file;
@@ -9,6 +12,7 @@ public class FileRating {
return file;
}
// TODO Will be changed later
public boolean getRating() {
return rating;
}

View File

@@ -1,10 +1,8 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
@Data
public class RatedFiles {
private FileRating[] rated;
public FileRating[] getRated() {
return rated;
}
}

View File

@@ -1,17 +1,11 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
@Data
public class Rating {
private int negative, positive;
public int getNegative() {
return negative;
}
public int getPositive() {
return positive;
}
public enum RatingType {
LIKE("like"), DISLIKE("dislike"), NEUTRAL("neutral");

View File

@@ -1,10 +1,8 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
@Data
public class SearchResult {
private FileInfo[] results;
public FileInfo[] getResults() {
return results;
}
}

View File

@@ -1,10 +1,8 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import lombok.Data;
@Data
public class Success {
private boolean success = false;
public boolean isSuccess() {
return success;
}
}

View File

@@ -1,22 +1,10 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class UserFiles {
import lombok.Data;
@Data
public class UserFiles {
private String user;
private FileInfo[] files;
private int total_size;
public String getUser() {
return user;
}
public FileInfo[] getFiles() {
return files;
}
public int getTotal_size() {
return total_size;
}
}

View File

@@ -5,11 +5,11 @@ import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
import java.util.List;
public interface Pagination {
public List<FileInfo> getFiles();
List<FileInfo> getFiles();
public int getLoadedPages();
int getLoadedPages();
public boolean fetchPage();
boolean fetchPage();
public static final int PAGE_SIZE = 30; //defined by the Website API
int PAGE_SIZE = 30; //defined by the Website API
}