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)) { if (getServer().getPluginManager().isPluginEnabled(this)) {
mojangAPI = new MojangAPI(); mojangAPI = new MojangAPI(this);
getLogger().info("Loading configuration..."); getLogger().info("Loading configuration...");
saveDefaultConfig(); saveDefaultConfig();

View file

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

View file

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

View file

@ -65,17 +65,17 @@ public class AppearanceManager {
updatePlayer(true); updatePlayer(true);
} }
public UpdateResult reset() { public ActionResult reset() {
final String defaultName = instance.getDataStore().getStoredName(player); final String defaultName = instance.getDataStore().getStoredName(player);
this.profile.setName(defaultName); this.profile.setName(defaultName);
this.profile.setSkin(defaultName); this.profile.setSkin(defaultName);
final UpdateResult updateResult = updatePlayer(true); final ActionResult actionResult = updatePlayer(true);
this.profile.setSkin(null); this.profile.setSkin(null);
this.profile.setName(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); 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.NickoBukkit;
import net.artelnatif.nicko.disguise.AppearanceManager; 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.I18N;
import net.artelnatif.nicko.i18n.I18NDict; import net.artelnatif.nicko.i18n.I18NDict;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -22,11 +22,11 @@ public class PlayerJoinListener implements Listener {
// TODO: 12/5/22 Update from BungeeCord // TODO: 12/5/22 Update from BungeeCord
if (appearanceManager.hasData()) { if (appearanceManager.hasData()) {
final UpdateResult updateResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange()); final ActionResult actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange());
if (!updateResult.isError()) { if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLIED)); player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLIED));
} else { } 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); }, 20L);

View file

@ -1,7 +1,7 @@
package net.artelnatif.nicko.impl; package net.artelnatif.nicko.impl;
import net.artelnatif.nicko.disguise.NickoProfile; import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.UpdateResult; import net.artelnatif.nicko.disguise.ActionResult;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public interface Internals { public interface Internals {
@ -9,5 +9,5 @@ public interface Internals {
void updateOthers(Player player); 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.JsonObject;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import net.artelnatif.nicko.NickoBukkit;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -33,6 +34,12 @@ public class MojangAPI {
.expireAfterWrite(120, TimeUnit.MINUTES) .expireAfterWrite(120, TimeUnit.MINUTES)
.build(loader); .build(loader);
private final NickoBukkit instance;
public MojangAPI(NickoBukkit instance) {
this.instance = instance;
}
public Optional<MojangSkin> getSkin(String uuid) throws IOException, ExecutionException { public Optional<MojangSkin> getSkin(String uuid) throws IOException, ExecutionException {
return cache.get(uuid); return cache.get(uuid);
} }
@ -63,19 +70,27 @@ public class MojangAPI {
con.setDoInput(true); con.setDoInput(true);
con.setRequestMethod("GET"); con.setRequestMethod("GET");
final BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream())); switch (con.getResponseCode()) {
final StringBuilder builder = new StringBuilder(); case 429 -> {
String line; instance.getLogger().warning("Failed to parse request! The connection is throttled.");
while ((line = input.readLine()) != null) { return getErrorObject();
builder.append(line); }
} case 200 -> {
final BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream()));
final StringBuilder builder = new StringBuilder();
String line;
while ((line = input.readLine()) != null) {
builder.append(line);
}
try { try {
final JsonElement jsonElt = JsonParser.parseString(builder.toString()); final JsonElement jsonElt = JsonParser.parseString(builder.toString());
return jsonElt.getAsJsonObject(); return jsonElt.getAsJsonObject();
} catch (JsonParseException | IllegalStateException exception) { } 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(); return getErrorObject();
}
}
} }
} }

View file

@ -5,7 +5,7 @@ import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap; import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile; 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.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin; import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent; import net.minecraft.network.chat.IChatBaseComponent;
@ -82,7 +82,7 @@ public class v1_17_R1 implements Internals {
} }
@Override @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 CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle(); final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName()); 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())); properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player); updateSelf(player);
} else { } else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG); return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
} }
} else { } else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG); return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
} }
} catch (ExecutionException e) { } catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE); return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) { } 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); onlineEntityPlayer.b.sendPacket(add);
}); });
updateOthers(player); 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 com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile; 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.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin; import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent; import net.minecraft.network.chat.IChatBaseComponent;
@ -84,7 +84,7 @@ public class v1_18_R1 implements Internals {
} }
@Override @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 CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle(); final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName()); 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())); properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player); updateSelf(player);
} else { } else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG); return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
} }
} else { } else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG); return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
} }
} catch (ExecutionException e) { } catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE); return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) { } 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); onlineEntityPlayer.b.a(add);
}); });
updateOthers(player); 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 com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile; 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.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin; import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.core.Holder; import net.minecraft.core.Holder;
@ -84,7 +84,7 @@ public class v1_18_R2 implements Internals {
} }
@Override @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 CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle(); final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName()); 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())); properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player); updateSelf(player);
} else { } else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG); return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
} }
} else { } else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG); return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
} }
} catch (ExecutionException e) { } catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE); return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) { } 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); onlineEntityPlayer.b.a(add);
}); });
updateOthers(player); 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 com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile; 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.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangSkin; import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent; import net.minecraft.network.chat.IChatBaseComponent;
@ -86,7 +86,7 @@ public class v1_19_R1 implements Internals {
} }
@Override @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 CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle(); final EntityPlayer entityPlayer = craftPlayer.getHandle();
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName()); 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())); properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player); updateSelf(player);
} else { } else {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_MOJANG); return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
} }
} else { } else {
return new UpdateResult(I18NDict.Error.NAME_FAIL_MOJANG); return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
} }
} catch (ExecutionException e) { } catch (ExecutionException e) {
return new UpdateResult(I18NDict.Error.SKIN_FAIL_CACHE); return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) { } 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); onlineEntityPlayer.b.a(add);
}); });
updateOthers(player); updateOthers(player);
return new UpdateResult(); return new ActionResult();
} }
} }