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,19 +70,27 @@ public class MojangAPI {
con.setDoInput(true);
con.setRequestMethod("GET");
final BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream()));
final StringBuilder builder = new StringBuilder();
String line;
while ((line = input.readLine()) != null) {
builder.append(line);
}
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;
while ((line = input.readLine()) != null) {
builder.append(line);
}
try {
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?");
return getErrorObject();
try {
final JsonElement jsonElt = JsonParser.parseString(builder.toString());
return jsonElt.getAsJsonObject();
} catch (JsonParseException | IllegalStateException exception) {
instance.getLogger().warning("Failed to parse request (" + parametrizedUrl + ")! Does the username exists?");
return getErrorObject();
}
}
}
}