feat wip(skin): skin entry name
This commit is contained in:
parent
228e3c4374
commit
4aa9b23952
2 changed files with 29 additions and 8 deletions
|
@ -5,9 +5,11 @@ import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import xyz.atnrch.nicko.NickoBukkit;
|
||||||
import xyz.atnrch.nicko.gui.ConfirmGUI;
|
import xyz.atnrch.nicko.gui.ConfirmGUI;
|
||||||
import xyz.atnrch.nicko.gui.admin.cache.CacheDetailedGUI;
|
import xyz.atnrch.nicko.gui.admin.cache.CacheDetailedGUI;
|
||||||
import xyz.atnrch.nicko.gui.items.confirm.ActionCallback;
|
import xyz.atnrch.nicko.gui.items.confirm.ActionCallback;
|
||||||
|
import xyz.atnrch.nicko.mojang.MojangAPI;
|
||||||
import xyz.xenondevs.invui.item.builder.ItemBuilder;
|
import xyz.xenondevs.invui.item.builder.ItemBuilder;
|
||||||
import xyz.xenondevs.invui.item.builder.SkullBuilder;
|
import xyz.xenondevs.invui.item.builder.SkullBuilder;
|
||||||
import xyz.xenondevs.invui.item.impl.AsyncItem;
|
import xyz.xenondevs.invui.item.impl.AsyncItem;
|
||||||
|
@ -16,17 +18,20 @@ import java.util.UUID;
|
||||||
|
|
||||||
public class CacheEntry extends AsyncItem {
|
public class CacheEntry extends AsyncItem {
|
||||||
private final String name;
|
private final String name;
|
||||||
|
private final String uuid;
|
||||||
|
private final MojangAPI mojangAPI = NickoBukkit.getInstance().getMojangAPI();
|
||||||
|
|
||||||
public CacheEntry(String name) {
|
public CacheEntry(String uuid) {
|
||||||
super(new ItemBuilder(Material.PAINTING).setDisplayName("§7§oLoading..."), () -> {
|
super(new ItemBuilder(Material.PAINTING).setDisplayName("§7§oLoading..."), () -> {
|
||||||
final String stringUUID = name.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
|
final String dashedUuid = uuid.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
|
||||||
final UUID uuid = UUID.fromString(stringUUID);
|
final UUID uuidObject = UUID.fromString(dashedUuid);
|
||||||
final SkullBuilder skull = new SkullBuilder(uuid);
|
final SkullBuilder skull = new SkullBuilder(uuidObject);
|
||||||
skull.setDisplayName("§6Skin Entry");
|
skull.setDisplayName("§6" + NickoBukkit.getInstance().getMojangAPI().getUUIDName(uuid));
|
||||||
skull.addLoreLines("§7Click to invalidate skin");
|
skull.addLoreLines("§7Click to invalidate skin");
|
||||||
return skull;
|
return skull;
|
||||||
});
|
});
|
||||||
this.name = name;
|
this.uuid = uuid;
|
||||||
|
this.name = mojangAPI.getUUIDName(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,7 +41,8 @@ public class CacheEntry extends AsyncItem {
|
||||||
new ConfirmGUI(player, new ActionCallback() {
|
new ConfirmGUI(player, new ActionCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onConfirm() {
|
public void onConfirm() {
|
||||||
player.sendMessage(name + " cleared");
|
mojangAPI.eraseFromCache(uuid);
|
||||||
|
player.sendMessage(name + " has been erased from the cache.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -14,6 +14,7 @@ import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -25,6 +26,8 @@ public class MojangAPI {
|
||||||
|
|
||||||
private final Logger logger = Logger.getLogger("MojangAPI");
|
private final Logger logger = Logger.getLogger("MojangAPI");
|
||||||
|
|
||||||
|
private final HashMap<String, String> uuidToName = new HashMap<>();
|
||||||
|
|
||||||
private final CacheLoader<String, Optional<MojangSkin>> loader = new CacheLoader<String, Optional<MojangSkin>>() {
|
private final CacheLoader<String, Optional<MojangSkin>> loader = new CacheLoader<String, Optional<MojangSkin>>() {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public Optional<MojangSkin> load(@Nonnull String uuid) throws Exception {
|
public Optional<MojangSkin> load(@Nonnull String uuid) throws Exception {
|
||||||
|
@ -50,11 +53,19 @@ public class MojangAPI {
|
||||||
final String parametrizedUrl = URL_NAME.replace("{name}", name);
|
final String parametrizedUrl = URL_NAME.replace("{name}", name);
|
||||||
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
||||||
if (hasNoError(object)) {
|
if (hasNoError(object)) {
|
||||||
return Optional.of(object.get("id").getAsString());
|
final JsonElement idObject = object.get("id");
|
||||||
|
final String id = idObject.getAsString();
|
||||||
|
uuidToName.put(id, name);
|
||||||
|
return Optional.of(id);
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void eraseFromCache(String uuid) {
|
||||||
|
cache.invalidate(uuid);
|
||||||
|
uuidToName.remove(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
private Optional<MojangSkin> getSkinFromMojang(String uuid) throws IOException {
|
private Optional<MojangSkin> getSkinFromMojang(String uuid) throws IOException {
|
||||||
final String parametrizedUrl = URL_SKIN.replace("{uuid}", uuid);
|
final String parametrizedUrl = URL_SKIN.replace("{uuid}", uuid);
|
||||||
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
||||||
|
@ -66,6 +77,10 @@ public class MojangAPI {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUUIDName(String uuid) {
|
||||||
|
return uuidToName.get(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
private JsonObject getRequestToUrl(String parametrizedUrl) throws IOException {
|
private JsonObject getRequestToUrl(String parametrizedUrl) throws IOException {
|
||||||
final URL url = new URL(parametrizedUrl);
|
final URL url = new URL(parametrizedUrl);
|
||||||
final HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
final HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||||
|
|
Loading…
Reference in a new issue