Skip to content

Commit

Permalink
Code Quality & Version dependency
Browse files Browse the repository at this point in the history
- Switched from Apache's HTTP client to my own HttpLib
- Switched from org.json to Google's gson
- Added LOG4J for JDA's Logging needs
- Switched to reflection for applying the skin, this means we're less version dependant (It works until Mojang changes its code)
- Added several Util functions
- Added a Triple, this is sortof to emulate Rust's Result
- Renamed and moved some things around to comform to Google's naming standards and to make the codebase more organized
  • Loading branch information
Tobias de Bruijn committed Mar 18, 2021
1 parent 0cda9f6 commit 024c94a
Show file tree
Hide file tree
Showing 25 changed files with 383 additions and 420 deletions.
5 changes: 0 additions & 5 deletions Spigot_1_16_R1/build.gradle

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions Spigot_1_16_R2/build.gradle

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions Spigot_1_16_R3/build.gradle

This file was deleted.

This file was deleted.

25 changes: 13 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ allprojects {

repositories {
jcenter()
mavenLocal()
mavenCentral()
maven{ url "https://hub.spigotmc.org/nexus/content/repositories/snapshots" }
maven{ url "https://oss.sonatype.org/content/repositories/snapshots" }
maven{ url 'https://jitpack.io' }
}

processResources {
Expand All @@ -36,30 +36,31 @@ allprojects {
}

dependencies {
//Apache HTTP Client
compile 'org.apache.httpcomponents:httpclient:4.5.12'
//HTTP client
compile 'com.github.TheDutchMC:HttpLib:1.1'

//Logging framework
compile 'org.slf4j:slf4j-log4j12:1.7.29'

//JSON
compile 'org.json:json:20200518'
compile 'com.google.code.gson:gson:2.8.6'

//JDA
compile ('net.dv8tion:JDA:4.2.0_168') {
compile('net.dv8tion:JDA:4.2.0_168') {
exclude module: 'opus-java'
}

//Spigot 1.16.1
//Spigot API
compileOnly 'org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT'

//NMS classes
//compile project('Spigot_1_16_R1')
//compile project('Spigot_1_16_R2')
compile project('Spigot_1_16_R3')

}

shadowJar() {
classifier = ''
relocate 'net.dv8tion.jda', 'nl.thedutchmc.libs.jda'
relocate 'net.dv8tion.jda', 'nl.thedutchmc.skinfixer.libs.net.dv8tion.jda'
relocate 'com.google.code.gson', 'nl.thedutchmc.skinfixer.libs.com.google.code.gson'
relocate 'nl.thedutchmc.httplib', 'nl.thedutchmc.skinfixer.libs.nl.thedutchmc.httplib'
relocate 'org.slf4j', 'nl.thedutchmc.skinfixer.libs.org.slf4j'
}

task testJar(type: ShadowJar) {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/nl/thedutchmc/SkinFixer/SkinFixer.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package nl.thedutchmc.SkinFixer;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import nl.thedutchmc.SkinFixer.commandHandlers.GetCodeCommandExecutor;
import nl.thedutchmc.SkinFixer.commandHandlers.SetSkinCommandExecutor;
import nl.thedutchmc.SkinFixer.commandHandlers.SkinFixerCommandExecutor;
import nl.thedutchmc.SkinFixer.commandexecutors.GetCodeCommandExecutor;
import nl.thedutchmc.SkinFixer.commandexecutors.SetSkinCommandExecutor;
import nl.thedutchmc.SkinFixer.commandexecutors.SkinFixerCommandExecutor;
import nl.thedutchmc.SkinFixer.fileHandlers.ConfigurationHandler;
import nl.thedutchmc.SkinFixer.fileHandlers.StorageHandler;
import nl.thedutchmc.SkinFixer.minecraftEvents.PlayerJoinEventListener;
import nl.thedutchmc.SkinFixer.minecraftevents.PlayerJoinEventListener;

public class SkinFixer extends JavaPlugin {

Expand All @@ -18,6 +20,8 @@ public class SkinFixer extends JavaPlugin {
public static final String NMS_VERSION = Bukkit.getServer().getClass().getPackage().getName().substring(23);
public static String PLUGIN_VERSION;

public static final Logger LOGGER = LogManager.getLogger(SkinFixer.class);

@Override
public void onEnable() {
INSTANCE = this;
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/nl/thedutchmc/SkinFixer/apis/MineskinApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package nl.thedutchmc.SkinFixer.apis;

import java.io.IOException;
import java.util.HashMap;

import com.google.gson.Gson;

import nl.thedutchmc.SkinFixer.SkinFixer;
import nl.thedutchmc.SkinFixer.gson.GetSkinResponse;
import nl.thedutchmc.SkinFixer.util.Triple;
import nl.thedutchmc.SkinFixer.util.Utils;
import nl.thedutchmc.httplib.Http;
import nl.thedutchmc.httplib.Http.RequestMethod;
import nl.thedutchmc.httplib.Http.ResponseObject;


public class MineskinApi {

public Triple<Boolean, GetSkinResponse, String> getSkin(String skinUrl, boolean slim) {
HashMap<String, String> urlParameters = new HashMap<>();
urlParameters.put("User-Agent", "SkinFixer");
urlParameters.put("url", skinUrl);

if(slim) {
urlParameters.put("model", "slim");
}

ResponseObject apiResponse;
try {
apiResponse = new Http().makeRequest(RequestMethod.POST, "https://api.mineskin.org/generate/url", urlParameters, null, null, null);
} catch(IOException e) {
SkinFixer.logWarn("An IOException occured while fetching a skin.");
//TODO logDebug

return new Triple<Boolean, GetSkinResponse, String>(false, null, Utils.getStackTrace(e));
}

if(apiResponse.getResponseCode() != 200) {
SkinFixer.logWarn("The MineSkin API returned an unexpected result: " + apiResponse.getConnectionMessage());
return new Triple<Boolean, GetSkinResponse, String>(false, null, apiResponse.getConnectionMessage());
}

final Gson gson = new Gson();
return new Triple<Boolean, GetSkinResponse, String>(true, gson.fromJson(apiResponse.getMessage(), GetSkinResponse.class), null);
}

public Triple<Boolean, GetSkinResponse, String> getSkinOfPremiumPlayer(String uuid) {

ResponseObject apiResponse;
try {
apiResponse = new Http().makeRequest(RequestMethod.GET, "https://api.mineskin.org/generate/user/" + uuid, null, null, null, null);
} catch(IOException e) {
SkinFixer.logWarn("An IOException occured while fetching a skin.");
//TODO logDebug

return new Triple<Boolean, GetSkinResponse, String>(false, null, Utils.getStackTrace(e));
}

if(apiResponse.getResponseCode() != 200) {
SkinFixer.logWarn("The MineSkin API returned an unexpected result: " + apiResponse.getConnectionMessage());
return new Triple<Boolean, GetSkinResponse, String>(false, null, apiResponse.getConnectionMessage());
}

final Gson gson = new Gson();
return new Triple<Boolean, GetSkinResponse, String>(true, gson.fromJson(apiResponse.getMessage(), GetSkinResponse.class), null);
}
}
46 changes: 46 additions & 0 deletions src/main/java/nl/thedutchmc/SkinFixer/apis/MojangApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package nl.thedutchmc.SkinFixer.apis;

import java.io.IOException;

import com.google.gson.Gson;

import nl.thedutchmc.SkinFixer.SkinFixer;
import nl.thedutchmc.SkinFixer.gson.MojangAuthResponse;
import nl.thedutchmc.SkinFixer.util.Triple;
import nl.thedutchmc.SkinFixer.util.Utils;
import nl.thedutchmc.httplib.Http;
import nl.thedutchmc.httplib.Http.MediaFormat;
import nl.thedutchmc.httplib.Http.RequestMethod;
import nl.thedutchmc.httplib.Http.ResponseObject;

public class MojangApi {

public Triple<Boolean, MojangAuthResponse, String> getUuidFromMojang(String playername) {
String requestBody = "[\"" + playername + "\"]";

ResponseObject apiResponse;
try {
apiResponse = new Http().makeRequest(RequestMethod.POST, "https://api.mojang.com/profiles/minecraft", null, MediaFormat.JSON, requestBody, null);
} catch(IOException e) {
SkinFixer.logWarn("An IOException occured while fetching a UUID from Mojang.");
//TODO logDebug

return new Triple<Boolean, MojangAuthResponse, String>(false, null, Utils.getStackTrace(e));
}

if(apiResponse.getResponseCode() != 200) {
SkinFixer.logWarn("The Mojang API returned an unexpected result: " + apiResponse.getConnectionMessage());
return new Triple<Boolean, MojangAuthResponse, String>(false, null, apiResponse.getConnectionMessage());
}


final Gson gson = new Gson();
MojangAuthResponse[] authResponse = gson.fromJson(apiResponse.getMessage(), MojangAuthResponse[].class);

if(authResponse.length == 0) {
return new Triple<Boolean, MojangAuthResponse, String>(true, null, null);
}

return new Triple<Boolean, MojangAuthResponse, String>(true, authResponse[0], null);
}
}
Loading

0 comments on commit 024c94a

Please sign in to comment.