From 656626008907e8e299c547a6b4abac0461899edb Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 10 May 2023 14:43:05 +0100 Subject: [PATCH] fix skins --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 8 ++++---- .../pyr/znpcsplus/npc/NpcRegistryImpl.java | 8 ++++++++ .../java/lol/pyr/znpcsplus/skin/Skin.java | 2 +- .../pyr/znpcsplus/skin/cache/SkinCache.java | 19 +++++++++++-------- .../skin/descriptor/PrefetchedDescriptor.java | 5 +++++ .../znpcsplus/storage/yaml/YamlStorage.java | 1 - 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 0fdd8bb..5bfe5f5 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -8,19 +8,19 @@ import lol.pyr.director.adventure.command.CommandManager; import lol.pyr.director.adventure.command.MultiCommand; import lol.pyr.znpcsplus.api.ZApiProvider; import lol.pyr.znpcsplus.commands.*; -import lol.pyr.znpcsplus.entity.EntityPropertyImpl; -import lol.pyr.znpcsplus.npc.NpcTypeImpl; +import lol.pyr.znpcsplus.commands.hologram.*; import lol.pyr.znpcsplus.config.Configs; +import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.interaction.InteractionPacketListener; import lol.pyr.znpcsplus.interaction.types.ConsoleCommandAction; import lol.pyr.znpcsplus.interaction.types.MessageAction; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import lol.pyr.znpcsplus.npc.NpcTypeImpl; import lol.pyr.znpcsplus.scheduling.FoliaScheduler; import lol.pyr.znpcsplus.scheduling.SpigotScheduler; import lol.pyr.znpcsplus.scheduling.TaskScheduler; -import lol.pyr.znpcsplus.skin.cache.SkinCache; import lol.pyr.znpcsplus.skin.cache.SkinCacheCleanTask; import lol.pyr.znpcsplus.skin.descriptor.FetchingDescriptor; import lol.pyr.znpcsplus.skin.descriptor.MirrorDescriptor; @@ -150,7 +150,7 @@ public class ZNpcsPlus extends JavaPlugin { entry.setProcessed(true); NpcImpl npc = entry.getNpc(); if (type.getType() == EntityTypes.PLAYER) { - SkinCache.fetchByName("Notch").thenAccept(skin -> npc.setProperty(EntityPropertyImpl.SKIN, new PrefetchedDescriptor(skin))); + PrefetchedDescriptor.forPlayer("Notch").thenAccept(skin -> npc.setProperty(EntityPropertyImpl.SKIN, skin)); npc.setProperty(EntityPropertyImpl.INVISIBLE, true); } npc.setProperty(EntityPropertyImpl.GLOW, NamedTextColor.RED); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java index 4392949..d7eeb32 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java @@ -9,6 +9,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; public class NpcRegistryImpl implements NpcRegistry { private final static NpcRegistryImpl registry = new NpcRegistryImpl(); @@ -39,6 +40,13 @@ public class NpcRegistryImpl implements NpcRegistry { return Collections.unmodifiableSet(npcMap.keySet()); } + public Collection modifiableIds() { + return Collections.unmodifiableSet(npcMap.entrySet().stream() + .filter(entry -> entry.getValue().isAllowCommandModification()) + .map(Map.Entry::getKey) + .collect(Collectors.toSet())); + } + public NpcEntryImpl create(String id, World world, NpcType type, ZLocation location) { return create(id, world, (NpcTypeImpl) type, location); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java b/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java index b0be451..26357a1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java @@ -37,7 +37,7 @@ public class Skin { public Skin(JsonObject obj) { for (JsonElement e : obj.get("properties").getAsJsonArray()) { JsonObject o = e.getAsJsonObject(); - properties.add(new TextureProperty(o.get("getName").getAsString(), o.get("value").getAsString(), o.has("signature") ? o.get("signature").getAsString() : null)); + properties.add(new TextureProperty(o.get("name").getAsString(), o.get("value").getAsString(), o.has("signature") ? o.get("signature").getAsString() : null)); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/skin/cache/SkinCache.java b/plugin/src/main/java/lol/pyr/znpcsplus/skin/cache/SkinCache.java index 0c79817..1d0f230 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/skin/cache/SkinCache.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/skin/cache/SkinCache.java @@ -22,11 +22,11 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; public class SkinCache { - private final static Map cache = new ConcurrentHashMap<>(); + private final static Map cache = new ConcurrentHashMap<>(); private final static Map idCache = new ConcurrentHashMap<>(); public static void cleanCache() { - for (Map.Entry entry : cache.entrySet()) if (entry.getValue().isExpired()) cache.remove(entry.getKey()); + for (Map.Entry entry : cache.entrySet()) if (entry.getValue().isExpired()) cache.remove(entry.getKey()); for (Map.Entry entry : idCache.entrySet()) if (entry.getValue().isExpired()) cache.remove(entry.getKey()); } @@ -68,16 +68,16 @@ public class SkinCache { if (!idCache.containsKey(name)) return false; CachedId id = idCache.get(name); if (id.isExpired() || !cache.containsKey(id.getId())) return false; - lol.pyr.znpcsplus.skin.Skin skin = cache.get(id.getId()); + Skin skin = cache.get(id.getId()); return !skin.isExpired(); } - public static lol.pyr.znpcsplus.skin.Skin getFullyCachedByName(String s) { + public static Skin getFullyCachedByName(String s) { String name = s.toLowerCase(); if (!idCache.containsKey(name)) return null; CachedId id = idCache.get(name); if (id.isExpired() || !cache.containsKey(id.getId())) return null; - lol.pyr.znpcsplus.skin.Skin skin = cache.get(id.getId()); + Skin skin = cache.get(id.getId()); if (skin.isExpired()) return null; return skin; } @@ -87,7 +87,7 @@ public class SkinCache { if (player != null && player.isOnline()) return CompletableFuture.completedFuture(getFromPlayer(player)); if (cache.containsKey(uuid)) { - lol.pyr.znpcsplus.skin.Skin skin = cache.get(uuid); + Skin skin = cache.get(uuid); if (!skin.isExpired()) return CompletableFuture.completedFuture(skin); } @@ -96,9 +96,11 @@ public class SkinCache { HttpURLConnection connection = null; try { connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); try (Reader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)) { - lol.pyr.znpcsplus.skin.Skin skin = new lol.pyr.znpcsplus.skin.Skin(JsonParser.parseReader(reader).getAsJsonObject()); + JsonObject obj = JsonParser.parseReader(reader).getAsJsonObject(); + Skin skin = new Skin(obj); cache.put(uuid, skin); return skin; } @@ -107,6 +109,7 @@ public class SkinCache { } finally { if (connection != null) connection.disconnect(); } + Bukkit.broadcastMessage("failed"); return null; }); } @@ -115,7 +118,7 @@ public class SkinCache { try { Object playerHandle = Reflections.GET_HANDLE_PLAYER_METHOD.get().invoke(player); GameProfile gameProfile = (GameProfile) Reflections.GET_PROFILE_METHOD.get().invoke(playerHandle, new Object[0]); - return new lol.pyr.znpcsplus.skin.Skin(gameProfile.getProperties()); + return new Skin(gameProfile.getProperties()); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/skin/descriptor/PrefetchedDescriptor.java b/plugin/src/main/java/lol/pyr/znpcsplus/skin/descriptor/PrefetchedDescriptor.java index 954ed80..e98c8e4 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/skin/descriptor/PrefetchedDescriptor.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/skin/descriptor/PrefetchedDescriptor.java @@ -4,6 +4,7 @@ import com.github.retrooper.packetevents.protocol.player.TextureProperty; import lol.pyr.znpcsplus.api.skin.SkinDescriptor; import lol.pyr.znpcsplus.skin.BaseSkinDescriptor; import lol.pyr.znpcsplus.skin.Skin; +import lol.pyr.znpcsplus.skin.cache.SkinCache; import org.bukkit.entity.Player; import java.util.concurrent.CompletableFuture; @@ -15,6 +16,10 @@ public class PrefetchedDescriptor implements BaseSkinDescriptor, SkinDescriptor this.skin = skin; } + public static CompletableFuture forPlayer(String name) { + return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(SkinCache.fetchByName(name).join())); + } + @Override public CompletableFuture fetch(Player player) { return CompletableFuture.completedFuture(skin); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java index 927dab4..648f886 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/storage/yaml/YamlStorage.java @@ -72,7 +72,6 @@ public class YamlStorage implements NpcStorage { npc.setProperty((EntityPropertyImpl) property, (T) value); } - @Override public void saveNpcs(Collection npcs) { for (NpcEntryImpl entry : npcs) if (entry.isSave()) try {