Make setSkin smarter about gameprofiles, but not really. Allows new PlayerDisguise(profileString)

This commit is contained in:
libraryaddict 2020-06-22 09:26:29 +12:00
parent 9cf9c85fed
commit e317f676b3
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4

@ -50,6 +50,17 @@ public class PlayerDisguise extends TargetedDisguise {
public PlayerDisguise(String name, String skinToUse) { public PlayerDisguise(String name, String skinToUse) {
this(); this();
if (name.equals(skinToUse)) {
WrappedGameProfile profile = getProfile(skinToUse);
if (profile != null) {
setName(profile.getName());
setSkin(profile);
createDisguise();
return;
}
}
setName(name); setName(name);
setSkin(skinToUse); setSkin(skinToUse);
@ -387,17 +398,10 @@ public class PlayerDisguise extends TargetedDisguise {
} }
public PlayerDisguise setSkin(String newSkin) { public PlayerDisguise setSkin(String newSkin) {
if (newSkin != null && newSkin.length() > 70 && newSkin.startsWith("{") && newSkin.endsWith("}")) { WrappedGameProfile profile = getProfile(newSkin);
try {
return setSkin(DisguiseUtilities.getGson().fromJson(newSkin, WrappedGameProfile.class)); if (profile != null) {
} return setSkin(profile);
catch (Exception ex) {
if (!"12345".equals("%%__USER__%%")) {
throw new IllegalArgumentException(String.format(
"The skin %s is too long to normally be a playername, but cannot be parsed to a " +
"GameProfile!", newSkin));
}
}
} }
if (newSkin != null) { if (newSkin != null) {
@ -475,6 +479,21 @@ public class PlayerDisguise extends TargetedDisguise {
return this; return this;
} }
private WrappedGameProfile getProfile(String string) {
if (string != null && string.length() > 70 && string.startsWith("{\"id\":") && string.endsWith("}") &&
string.contains(",\"name\":")) {
try {
return DisguiseUtilities.getGson().fromJson(string, WrappedGameProfile.class);
}
catch (Exception ex) {
throw new IllegalStateException(
"Tried to parse " + string + " to a GameProfile, but it has been formatted incorrectly!");
}
}
return null;
}
private void refreshDisguise() { private void refreshDisguise() {
if (DisguiseUtilities.isDisguiseInUse(this)) { if (DisguiseUtilities.isDisguiseInUse(this)) {
if (isDisplayedInTab()) { if (isDisplayedInTab()) {