Skip to content

Commit

Permalink
v1.7.8
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasDeBruijn committed Jan 19, 2023
1 parent ff1c8c6 commit ebe9792
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 59 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {

implementation 'dev.array21:httplib:1.2.2'
implementation 'dev.array21:classvalidator:1.0.0'
implementation 'dev.array21:bukkit-reflection-util:1.2.1'
implementation 'dev.array21:bukkit-reflection-util:1.3.0'

// JDA and related dependencies
implementation 'org.slf4j:slf4j-log4j12:1.7.29'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pluginVersion = 1.7.7
pluginVersion = 1.7.8
6 changes: 1 addition & 5 deletions plugininfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
"servers": [
"https://skinfixer.k8s.array21.dev"
],
"messages": [
{
"text": "Hi Mom!"
}
]
"messages": []
}
6 changes: 3 additions & 3 deletions src/main/java/dev/array21/skinfixer/SkinChangeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void run() {
//Fetch the skin from Mineskin.org's API
Triple<Boolean, GetSkinResponse, String> apiResponse;
if(isPremium) {
apiResponse = new SkinFixerApi().getSkinOfPremiumPlayer(externalUuid.toString());
apiResponse = new SkinFixerApi(SkinChangeHandler.this.plugin.remoteInfoManifest).getSkinOfPremiumPlayer(externalUuid.toString());
} else {
apiResponse = new SkinFixerApi().getSkin(skinUrl, slim);
apiResponse = new SkinFixerApi(SkinChangeHandler.this.plugin.remoteInfoManifest).getSkin(skinUrl, slim);
}

if(!apiResponse.getA()) {
Expand All @@ -65,7 +65,7 @@ public void changeSkinFromUuid(UUID mojangUuid, UUID localPlayerUuid, boolean sl
@Override
public void run() {
Player p = Bukkit.getPlayer(localPlayerUuid);
Triple<Boolean, GetSkinResponse, String> apiResponse = new SkinFixerApi().getSkinOfPremiumPlayer(mojangUuid.toString());
Triple<Boolean, GetSkinResponse, String> apiResponse = new SkinFixerApi(SkinChangeHandler.this.plugin.remoteInfoManifest).getSkinOfPremiumPlayer(mojangUuid.toString());
if(!apiResponse.getA()) {
p.sendMessage(ChatColor.RED + LangHandler.model.skinApplyFailed.replaceAll("%ERROR%", ChatColor.GRAY + apiResponse.getC() + ChatColor.RED));
return;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/dev/array21/skinfixer/SkinFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import java.util.HashMap;
import java.util.logging.Logger;

import org.apache.log4j.LogManager;
import dev.array21.skinfixer.apis.RemotePluginInfo;
import dev.array21.skinfixer.apis.gson.RemoteInfoManifest;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -27,6 +28,8 @@ public class SkinFixer extends JavaPlugin {
private ConfigHandler configHandler;
private JdaHandler jdaHandler;
private LibWrapper libWrapper;

public RemoteInfoManifest remoteInfoManifest;

@Override
public void onEnable() {
Expand All @@ -40,6 +43,8 @@ public void onEnable() {
this.configHandler = new ConfigHandler(this);
ConfigManifest configManifest = configHandler.read();

this.remoteInfoManifest = RemotePluginInfo.getManifest(this);

this.libWrapper = new LibWrapper(this);
this.libWrapper.init();

Expand Down Expand Up @@ -69,14 +74,18 @@ public void onEnable() {

//Register event listeners
Bukkit.getPluginManager().registerEvents(new PlayerJoinEventListener(this), this);

for(RemoteInfoManifest.Message remoteMessage : this.remoteInfoManifest.messages) {
logInfo(remoteMessage.text);
}
}

public static void logInfo(Object log) {
LOGGER.info("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log.toString());
LOGGER.info(log.toString());
}

public static void logWarn(Object log) {
LOGGER.warning("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log.toString());
LOGGER.warning(log.toString());
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/dev/array21/skinfixer/apis/RemotePluginInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.array21.skinfixer.apis;

import com.google.gson.Gson;
import dev.array21.httplib.Http;
import dev.array21.skinfixer.SkinFixer;
import dev.array21.skinfixer.apis.gson.RemoteInfoManifest;
import dev.array21.skinfixer.util.Utils;
import org.bukkit.Bukkit;

import java.io.IOException;

public class RemotePluginInfo {

public static RemoteInfoManifest getManifest(SkinFixer plugin) {
Http.ResponseObject response;
try {
response = new Http().makeRequest(Http.RequestMethod.GET, plugin.getConfigManifest().remotePluginInfoUrl, null, null, null, null);
} catch (IOException e) {
SkinFixer.logWarn("Failed to retrieve remote plugin info. This is fatal");
SkinFixer.logWarn(Utils.getStackTrace(e));
Bukkit.getPluginManager().disablePlugin(plugin);
return null;
}

return new Gson().fromJson(response.getMessage(), RemoteInfoManifest.class);
}
}
115 changes: 72 additions & 43 deletions src/main/java/dev/array21/skinfixer/apis/SkinFixerApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,101 @@
import dev.array21.skinfixer.SkinFixer;
import dev.array21.skinfixer.apis.gson.GetSkinResponse;
import dev.array21.skinfixer.apis.gson.GetUuidResponse;
import dev.array21.skinfixer.apis.gson.RemoteInfoManifest;
import dev.array21.skinfixer.util.Triple;
import dev.array21.skinfixer.util.Utils;
import dev.array21.httplib.Http;
import dev.array21.httplib.Http.RequestMethod;
import dev.array21.httplib.Http.ResponseObject;
import org.jetbrains.annotations.NotNull;

public class SkinFixerApi {


final String[] servers;

public SkinFixerApi(RemoteInfoManifest infoManifest) {
this.servers = infoManifest.servers;
}

public Triple<Boolean, String, String> getUuid(String playerName) {
ResponseObject apiResponse;
try {
apiResponse = new Http().makeRequest(RequestMethod.GET, "https://skinfixer.k8s.array21.dev/player/" + playerName, null, null, null, null);
} catch(IOException e) {
SkinFixer.logWarn("An IOException occured while fetching a UUID from the SkinFixer API");
SkinFixer.logWarn(Utils.getStackTrace(e));
return new Triple<Boolean, String, String>(false, null, Utils.getStackTrace(e));
ResponseObject apiResponse = null;
IOException exception = null;
for(String server : this.servers) {
try {
apiResponse = new Http().makeRequest(
RequestMethod.GET,
String.format("%s/player/%s", server, playerName),
null,
null,
null,
null
);
} catch(IOException e) {
exception = e;
SkinFixer.logWarn(String.format("An IOException occured while fetching a UUID from the SkinFixer API (%s). Trying other servers (if there are any)", server));
}
}

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

Triple<Boolean, GetUuidResponse, String> responseTriple = this.checkResponseAndParse(apiResponse, exception, GetUuidResponse.class);

if(responseTriple.getB() != null) {
return new Triple<>(responseTriple.getA(), responseTriple.getB().uuid, responseTriple.getC());
} else {
return new Triple<>(responseTriple.getA(), null, responseTriple.getC());
}

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

public Triple<Boolean, GetSkinResponse, String> getSkin(String skinUrl, boolean slim) {
String skinUrlBase64 = Base64.getEncoder().encodeToString(skinUrl.getBytes());

ResponseObject apiResponse;
try {
apiResponse = new Http().makeRequest(RequestMethod.GET, "https://skinfixer.k8s.array21.dev/generate/url/" + skinUrlBase64, new HashMap<>(), null, null, null);
} catch(IOException e) {
SkinFixer.logWarn("An IOException occured while fetching a skin from the SkinFixer API");
SkinFixer.logWarn(Utils.getStackTrace(e));
return new Triple<Boolean, GetSkinResponse, String>(false, null, Utils.getStackTrace(e));

ResponseObject apiResponse = null;
IOException exception = null;
for(String server : this.servers) {
try {
apiResponse = new Http().makeRequest(RequestMethod.GET, String.format("%s/generate/url/%s", server, skinUrlBase64), new HashMap<>(), null, null, null);
break;
} catch(IOException e) {
exception = e;
SkinFixer.logWarn(String.format("An IOException occured while fetching a skin from the SkinFixer API (%s). Trying other servers (if there are any)", server));
}
}


return checkResponseAndParse(apiResponse, exception, GetSkinResponse.class);
}

@NotNull
private <T> Triple<Boolean, T, String> checkResponseAndParse(ResponseObject apiResponse, IOException exception, Class<T> parseToClass) {
if(apiResponse == null) {
assert exception != null;

SkinFixer.logWarn(Utils.getStackTrace(exception));
return new Triple<>(false, null, Utils.getStackTrace(exception));
}

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

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

public Triple<Boolean, GetSkinResponse, String> getSkinOfPremiumPlayer(String uuid) {
String uuidBase64 = Base64.getEncoder().encodeToString(uuid.getBytes());

ResponseObject apiResponse;
try {
apiResponse = new Http().makeRequest(RequestMethod.GET, "https://skinfixer.k8s.array21.dev/generate/uuid/" + uuidBase64, new HashMap<>(), null, null, null);
} catch(IOException e) {
SkinFixer.logWarn("An IOException occured while fetching a skin from the SkinFixer API.");
SkinFixer.logWarn(Utils.getStackTrace(e));
return new Triple<Boolean, GetSkinResponse, String>(false, null, Utils.getStackTrace(e));
ResponseObject apiResponse = null;
IOException exception = null;
for(String server : servers) {
try {
apiResponse = new Http().makeRequest(RequestMethod.GET, String.format("%s/generate/uuid/%s", server, uuidBase64), new HashMap<>(), null, null, null);
break;
} catch(IOException e) {
exception = e;
SkinFixer.logWarn(String.format("An IOException occured while fetching a skin from the SkinFixer API (%s). Trying other servers (if there are any)", server));
}
}

if(apiResponse.getResponseCode() != 200) {
SkinFixer.logWarn("The SkinFixer 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);

return checkResponseAndParse(apiResponse, exception, GetSkinResponse.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.array21.skinfixer.apis.gson;

public class RemoteInfoManifest {

public String[] servers;

public Message[] messages;

public static class Message {
public String text;
}
}
2 changes: 1 addition & 1 deletion src/main/java/dev/array21/skinfixer/common/AddNewSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Pair<Integer, String> addByNickname(String url) {
code = generateRandomInt();
}

Triple<Boolean, String, String> apiResponse = new SkinFixerApi().getUuid(url);
Triple<Boolean, String, String> apiResponse = new SkinFixerApi(this.plugin.remoteInfoManifest).getUuid(url);
if(!apiResponse.getA()) {
return new Pair<Integer, String>(null, apiResponse.getC());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public ConfigManifest read() {
if(manifest.applySkinOnJoin == null) {
manifest.applySkinOnJoin = true;
}

if(manifest.remotePluginInfoUrl == null) {
manifest.remotePluginInfoUrl = "https://raw.githubusercontent.com/TobiasDeBruijn/SkinFixer/master/plugininfo.json";
}

if(manifest.useDiscord) {
if(manifest.discordSettings == null) {
Expand Down Expand Up @@ -90,5 +94,5 @@ public ConfigManifest read() {
public ConfigManifest getConfigManifest() {
return this.manifest;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class ConfigManifest {
public Boolean disableSkinApplyOnLoginMessage;

public Boolean applySkinOnJoin;

public String remotePluginInfoUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void run() {
return;
}

Triple<Boolean, String, String> mojangApiResponse = new SkinFixerApi().getUuid(event.getPlayer().getName());
Triple<Boolean, String, String> mojangApiResponse = new SkinFixerApi(PlayerJoinEventListener.this.plugin.remoteInfoManifest).getUuid(event.getPlayer().getName());
if(!mojangApiResponse.getA()) {
SkinFixer.logWarn("Something went wrong fetching the UUID from Mojang.");
} else if(mojangApiResponse.getB() != null) {
Expand Down

0 comments on commit ebe9792

Please sign in to comment.