fix skins

This commit is contained in:
Pyrbu 2023-05-10 14:43:05 +01:00
parent bd22453f76
commit 6566260089
6 changed files with 29 additions and 14 deletions

@ -8,19 +8,19 @@ import lol.pyr.director.adventure.command.CommandManager;
import lol.pyr.director.adventure.command.MultiCommand; import lol.pyr.director.adventure.command.MultiCommand;
import lol.pyr.znpcsplus.api.ZApiProvider; import lol.pyr.znpcsplus.api.ZApiProvider;
import lol.pyr.znpcsplus.commands.*; import lol.pyr.znpcsplus.commands.*;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.commands.hologram.*;
import lol.pyr.znpcsplus.npc.NpcTypeImpl;
import lol.pyr.znpcsplus.config.Configs; import lol.pyr.znpcsplus.config.Configs;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.interaction.InteractionPacketListener; import lol.pyr.znpcsplus.interaction.InteractionPacketListener;
import lol.pyr.znpcsplus.interaction.types.ConsoleCommandAction; import lol.pyr.znpcsplus.interaction.types.ConsoleCommandAction;
import lol.pyr.znpcsplus.interaction.types.MessageAction; import lol.pyr.znpcsplus.interaction.types.MessageAction;
import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.npc.NpcTypeImpl;
import lol.pyr.znpcsplus.scheduling.FoliaScheduler; import lol.pyr.znpcsplus.scheduling.FoliaScheduler;
import lol.pyr.znpcsplus.scheduling.SpigotScheduler; import lol.pyr.znpcsplus.scheduling.SpigotScheduler;
import lol.pyr.znpcsplus.scheduling.TaskScheduler; 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.cache.SkinCacheCleanTask;
import lol.pyr.znpcsplus.skin.descriptor.FetchingDescriptor; import lol.pyr.znpcsplus.skin.descriptor.FetchingDescriptor;
import lol.pyr.znpcsplus.skin.descriptor.MirrorDescriptor; import lol.pyr.znpcsplus.skin.descriptor.MirrorDescriptor;
@ -150,7 +150,7 @@ public class ZNpcsPlus extends JavaPlugin {
entry.setProcessed(true); entry.setProcessed(true);
NpcImpl npc = entry.getNpc(); NpcImpl npc = entry.getNpc();
if (type.getType() == EntityTypes.PLAYER) { 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.INVISIBLE, true);
} }
npc.setProperty(EntityPropertyImpl.GLOW, NamedTextColor.RED); npc.setProperty(EntityPropertyImpl.GLOW, NamedTextColor.RED);

@ -9,6 +9,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
public class NpcRegistryImpl implements NpcRegistry { public class NpcRegistryImpl implements NpcRegistry {
private final static NpcRegistryImpl registry = new NpcRegistryImpl(); private final static NpcRegistryImpl registry = new NpcRegistryImpl();
@ -39,6 +40,13 @@ public class NpcRegistryImpl implements NpcRegistry {
return Collections.unmodifiableSet(npcMap.keySet()); return Collections.unmodifiableSet(npcMap.keySet());
} }
public Collection<String> 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) { public NpcEntryImpl create(String id, World world, NpcType type, ZLocation location) {
return create(id, world, (NpcTypeImpl) type, location); return create(id, world, (NpcTypeImpl) type, location);
} }

@ -37,7 +37,7 @@ public class Skin {
public Skin(JsonObject obj) { public Skin(JsonObject obj) {
for (JsonElement e : obj.get("properties").getAsJsonArray()) { for (JsonElement e : obj.get("properties").getAsJsonArray()) {
JsonObject o = e.getAsJsonObject(); 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));
} }
} }

@ -22,11 +22,11 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class SkinCache { public class SkinCache {
private final static Map<String, lol.pyr.znpcsplus.skin.Skin> cache = new ConcurrentHashMap<>(); private final static Map<String, Skin> cache = new ConcurrentHashMap<>();
private final static Map<String, CachedId> idCache = new ConcurrentHashMap<>(); private final static Map<String, CachedId> idCache = new ConcurrentHashMap<>();
public static void cleanCache() { public static void cleanCache() {
for (Map.Entry<String, lol.pyr.znpcsplus.skin.Skin> entry : cache.entrySet()) if (entry.getValue().isExpired()) cache.remove(entry.getKey()); for (Map.Entry<String, Skin> entry : cache.entrySet()) if (entry.getValue().isExpired()) cache.remove(entry.getKey());
for (Map.Entry<String, CachedId> entry : idCache.entrySet()) if (entry.getValue().isExpired()) cache.remove(entry.getKey()); for (Map.Entry<String, CachedId> entry : idCache.entrySet()) if (entry.getValue().isExpired()) cache.remove(entry.getKey());
} }
@ -68,16 +68,16 @@ public class SkinCache {
if (!idCache.containsKey(name)) return false; if (!idCache.containsKey(name)) return false;
CachedId id = idCache.get(name); CachedId id = idCache.get(name);
if (id.isExpired() || !cache.containsKey(id.getId())) return false; 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(); return !skin.isExpired();
} }
public static lol.pyr.znpcsplus.skin.Skin getFullyCachedByName(String s) { public static Skin getFullyCachedByName(String s) {
String name = s.toLowerCase(); String name = s.toLowerCase();
if (!idCache.containsKey(name)) return null; if (!idCache.containsKey(name)) return null;
CachedId id = idCache.get(name); CachedId id = idCache.get(name);
if (id.isExpired() || !cache.containsKey(id.getId())) return null; 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; if (skin.isExpired()) return null;
return skin; return skin;
} }
@ -87,7 +87,7 @@ public class SkinCache {
if (player != null && player.isOnline()) return CompletableFuture.completedFuture(getFromPlayer(player)); if (player != null && player.isOnline()) return CompletableFuture.completedFuture(getFromPlayer(player));
if (cache.containsKey(uuid)) { 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); if (!skin.isExpired()) return CompletableFuture.completedFuture(skin);
} }
@ -96,9 +96,11 @@ public class SkinCache {
HttpURLConnection connection = null; HttpURLConnection connection = null;
try { try {
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
try (Reader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)) { 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); cache.put(uuid, skin);
return skin; return skin;
} }
@ -107,6 +109,7 @@ public class SkinCache {
} finally { } finally {
if (connection != null) connection.disconnect(); if (connection != null) connection.disconnect();
} }
Bukkit.broadcastMessage("failed");
return null; return null;
}); });
} }
@ -115,7 +118,7 @@ public class SkinCache {
try { try {
Object playerHandle = Reflections.GET_HANDLE_PLAYER_METHOD.get().invoke(player); Object playerHandle = Reflections.GET_HANDLE_PLAYER_METHOD.get().invoke(player);
GameProfile gameProfile = (GameProfile) Reflections.GET_PROFILE_METHOD.get().invoke(playerHandle, new Object[0]); 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) { } catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

@ -4,6 +4,7 @@ import com.github.retrooper.packetevents.protocol.player.TextureProperty;
import lol.pyr.znpcsplus.api.skin.SkinDescriptor; import lol.pyr.znpcsplus.api.skin.SkinDescriptor;
import lol.pyr.znpcsplus.skin.BaseSkinDescriptor; import lol.pyr.znpcsplus.skin.BaseSkinDescriptor;
import lol.pyr.znpcsplus.skin.Skin; import lol.pyr.znpcsplus.skin.Skin;
import lol.pyr.znpcsplus.skin.cache.SkinCache;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -15,6 +16,10 @@ public class PrefetchedDescriptor implements BaseSkinDescriptor, SkinDescriptor
this.skin = skin; this.skin = skin;
} }
public static CompletableFuture<PrefetchedDescriptor> forPlayer(String name) {
return CompletableFuture.supplyAsync(() -> new PrefetchedDescriptor(SkinCache.fetchByName(name).join()));
}
@Override @Override
public CompletableFuture<Skin> fetch(Player player) { public CompletableFuture<Skin> fetch(Player player) {
return CompletableFuture.completedFuture(skin); return CompletableFuture.completedFuture(skin);

@ -72,7 +72,6 @@ public class YamlStorage implements NpcStorage {
npc.setProperty((EntityPropertyImpl<T>) property, (T) value); npc.setProperty((EntityPropertyImpl<T>) property, (T) value);
} }
@Override @Override
public void saveNpcs(Collection<NpcEntryImpl> npcs) { public void saveNpcs(Collection<NpcEntryImpl> npcs) {
for (NpcEntryImpl entry : npcs) if (entry.isSave()) try { for (NpcEntryImpl entry : npcs) if (entry.isSave()) try {