feat: mojangapi throttle support

This commit is contained in:
aro 2022-12-31 12:22:26 +01:00
parent cb34b448de
commit 549c243cb7
11 changed files with 76 additions and 61 deletions

View file

@ -97,7 +97,7 @@ public class NickoBukkit extends JavaPlugin {
}
if (getServer().getPluginManager().isPluginEnabled(this)) {
mojangAPI = new MojangAPI();
mojangAPI = new MojangAPI(this);
getLogger().info("Loading configuration...");
saveDefaultConfig();

View file

@ -2,7 +2,7 @@ package net.artelnatif.nicko.anvil;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.disguise.UpdateResult;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangUtils;
@ -58,11 +58,11 @@ public class AnvilManager {
return AnvilGUI.Response.text("Invalid username!");
} else {
appearanceManager.setName(response);
final UpdateResult updateResult = appearanceManager.updatePlayer(false);
if (!updateResult.isError()) {
final ActionResult actionResult = appearanceManager.updatePlayer(false);
if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_SUCCESS));
} else {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_FAIL, I18N.translateFlat(player, updateResult.getErrorMessage())));
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_FAIL, I18N.translateFlat(player, actionResult.getErrorMessage())));
}
return AnvilGUI.Response.close();
}
@ -79,11 +79,11 @@ public class AnvilManager {
return AnvilGUI.Response.text("Invalid username!");
} else {
appearanceManager.setSkin(response);
final UpdateResult updateResult = appearanceManager.updatePlayer(true);
if (!updateResult.isError()) {
final ActionResult actionResult = appearanceManager.updatePlayer(true);
if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_SUCCESS));
} else {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_FAIL, I18N.translateFlat(player, updateResult.getErrorMessage())));
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_FAIL, I18N.translateFlat(player, actionResult.getErrorMessage())));
}
return AnvilGUI.Response.close();
}

View file

@ -2,16 +2,16 @@ package net.artelnatif.nicko.disguise;
import net.artelnatif.nicko.i18n.I18NDict;
public class UpdateResult {
public class ActionResult {
private final I18NDict errorMessage;
private boolean error = false;
public UpdateResult(I18NDict errorMessage) {
public ActionResult(I18NDict errorMessage) {
this.error = true;
this.errorMessage = errorMessage;
}
public UpdateResult() {
public ActionResult() {
this.errorMessage = null;
}

View file

@ -65,17 +65,17 @@ public class AppearanceManager {
updatePlayer(true);
}
public UpdateResult reset() {
public ActionResult reset() {
final String defaultName = instance.getDataStore().getStoredName(player);
this.profile.setName(defaultName);
this.profile.setSkin(defaultName);
final UpdateResult updateResult = updatePlayer(true);
final ActionResult actionResult = updatePlayer(true);
this.profile.setSkin(null);
this.profile.setName(null);
return updateResult;
return actionResult;
}
public UpdateResult updatePlayer(boolean skinChange) {
public ActionResult updatePlayer(boolean skinChange) {
return NickoBukkit.getInstance().getInternals().updateProfile(player, profile, skinChange);
}
}

View file

@ -2,7 +2,7 @@ package net.artelnatif.nicko.event;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.disguise.UpdateResult;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;
import org.bukkit.Bukkit;
@ -22,11 +22,11 @@ public class PlayerJoinListener implements Listener {
// TODO: 12/5/22 Update from BungeeCord
if (appearanceManager.hasData()) {
final UpdateResult updateResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange());
if (!updateResult.isError()) {
final ActionResult actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange());
if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLIED));
} else {
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLY_FAIL, I18N.translate(player, updateResult.getErrorMessage())));
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLY_FAIL, I18N.translate(player, actionResult.getErrorMessage())));
}
}
}, 20L);

View file

@ -1,7 +1,7 @@
package net.artelnatif.nicko.impl;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.UpdateResult;
import net.artelnatif.nicko.disguise.ActionResult;
import org.bukkit.entity.Player;
public interface Internals {
@ -9,5 +9,5 @@ public interface Internals {
void updateOthers(Player player);
UpdateResult updateProfile(Player player, NickoProfile profile, boolean skinChange);
ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange);
}

View file

@ -7,6 +7,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import net.artelnatif.nicko.NickoBukkit;
import javax.annotation.Nonnull;
import javax.net.ssl.HttpsURLConnection;
@ -33,6 +34,12 @@ public class MojangAPI {
.expireAfterWrite(120, TimeUnit.MINUTES)
.build(loader);
private final NickoBukkit instance;
public MojangAPI(NickoBukkit instance) {
this.instance = instance;
}
public Optional<MojangSkin> getSkin(String uuid) throws IOException, ExecutionException {
return cache.get(uuid);
}
@ -63,6 +70,12 @@ public class MojangAPI {
con.setDoInput(true);
con.setRequestMethod("GET");
switch (con.getResponseCode()) {
case 429 -> {
instance.getLogger().warning("Failed to parse request! The connection is throttled.");
return getErrorObject();
}
case 200 -> {
final BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream()));
final StringBuilder builder = new StringBuilder();
String line;
@ -74,10 +87,12 @@ public class MojangAPI {
final JsonElement jsonElt = JsonParser.parseString(builder.toString());
return jsonElt.getAsJsonObject();
} catch (JsonParseException | IllegalStateException exception) {
System.out.println("Failed to parse request (" + parametrizedUrl + ")! Does the username exists?");
instance.getLogger().warning("Failed to parse request (" + parametrizedUrl + ")! Does the username exists?");
return getErrorObject();
}
}
}
}
private JsonObject getErrorObject() {
final JsonObject errorObject = new JsonObject();

View file

@ -5,7 +5,7 @@ import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.UpdateResult;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent;
@ -82,7 +82,7 @@ public class v1_17_R1 implements Internals {
}
@Override
public UpdateResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
@ -102,15 +102,15 @@ public class v1_17_R1 implements Internals {
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE);
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new UpdateResult(I18NDict.Error.UNEXPECTED_ERROR);
return new ActionResult(I18NDict.Error.UNEXPECTED_ERROR);
}
}
@ -127,6 +127,6 @@ public class v1_17_R1 implements Internals {
onlineEntityPlayer.b.sendPacket(add);
});
updateOthers(player);
return new UpdateResult();
return new ActionResult();
}
}

View file

@ -5,7 +5,7 @@ import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.UpdateResult;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent;
@ -84,7 +84,7 @@ public class v1_18_R1 implements Internals {
}
@Override
public UpdateResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
@ -104,15 +104,15 @@ public class v1_18_R1 implements Internals {
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE);
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new UpdateResult(I18NDict.Error.UNEXPECTED_ERROR);
return new ActionResult(I18NDict.Error.UNEXPECTED_ERROR);
}
}
@ -129,6 +129,6 @@ public class v1_18_R1 implements Internals {
onlineEntityPlayer.b.a(add);
});
updateOthers(player);
return new UpdateResult();
return new ActionResult();
}
}

View file

@ -5,7 +5,7 @@ import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.UpdateResult;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.core.Holder;
@ -84,7 +84,7 @@ public class v1_18_R2 implements Internals {
}
@Override
public UpdateResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
@ -104,15 +104,15 @@ public class v1_18_R2 implements Internals {
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE);
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new UpdateResult(I18NDict.Error.UNEXPECTED_ERROR);
return new ActionResult(I18NDict.Error.UNEXPECTED_ERROR);
}
}
@ -129,6 +129,6 @@ public class v1_18_R2 implements Internals {
onlineEntityPlayer.b.a(add);
});
updateOthers(player);
return new UpdateResult();
return new ActionResult();
}
}

View file

@ -5,7 +5,7 @@ import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.UpdateResult;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent;
@ -86,7 +86,7 @@ public class v1_19_R1 implements Internals {
}
@Override
public UpdateResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
@ -109,15 +109,15 @@ public class v1_19_R1 implements Internals {
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE);
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG);
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
}
@ -137,6 +137,6 @@ public class v1_19_R1 implements Internals {
onlineEntityPlayer.b.a(add);
});
updateOthers(player);
return new UpdateResult();
return new ActionResult();
}
}