From 46381ef89233cf63b1f10073a20a4efd954717d8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 21 Dec 2024 13:45:12 +0100 Subject: [PATCH] Spawn with default skin and load actual skin later --- .../de/oliver/fancynpcs/NpcManagerImpl.java | 15 +++++--- .../fancynpcs/commands/npc/SkinCMD.java | 21 ++++++++++- .../fancynpcs/skins/SkinManagerImpl.java | 12 ++++--- .../de/oliver/fancynpcs/skins/SkinUtils.java | 35 +++++++++++++++++++ 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java b/src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java index 8374a908..0efbe3cd 100644 --- a/src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java +++ b/src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java @@ -10,6 +10,7 @@ import de.oliver.fancynpcs.api.events.NpcsLoadedEvent; import de.oliver.fancynpcs.api.skins.SkinData; import de.oliver.fancynpcs.api.utils.NpcEquipmentSlot; +import de.oliver.fancynpcs.skins.SkinUtils; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -269,14 +270,14 @@ public void loadNpcs() { } SkinData skin = null; + boolean applySkinLater = false; String skinIdentifier = npcConfig.getString("npcs." + id + ".skin.identifier", npcConfig.getString("npcs." + id + ".skin.uuid", "")); + String skinVariantStr = npcConfig.getString("npcs." + id + ".skin.variant", SkinData.SkinVariant.AUTO.name()); + SkinData.SkinVariant skinVariant = SkinData.SkinVariant.valueOf(skinVariantStr); if (!skinIdentifier.isEmpty()) { - String skinVariantStr = npcConfig.getString("npcs." + id + ".skin.variant", SkinData.SkinVariant.AUTO.name()); - SkinData.SkinVariant skinVariant = SkinData.SkinVariant.valueOf(skinVariantStr); - - skin = FancyNpcs.getInstance().getSkinManagerImpl().getByIdentifier(skinIdentifier, skinVariant); + skin = FancyNpcs.getInstance().getSkinManagerImpl().tryToGetFromCache(skinIdentifier, skinVariant); if (skin == null) { - logger.warn("Could not load skin for npc '" + id + "'"); + applySkinLater = true; } } @@ -428,6 +429,10 @@ public void loadNpcs() { npc.create(); registerNpc(npc); + + if(applySkinLater) { + SkinUtils.applySkinLater(id, skinIdentifier, skinVariant); + } } isLoaded = true; diff --git a/src/main/java/de/oliver/fancynpcs/commands/npc/SkinCMD.java b/src/main/java/de/oliver/fancynpcs/commands/npc/SkinCMD.java index 4dc73764..89fd3744 100644 --- a/src/main/java/de/oliver/fancynpcs/commands/npc/SkinCMD.java +++ b/src/main/java/de/oliver/fancynpcs/commands/npc/SkinCMD.java @@ -5,6 +5,7 @@ import de.oliver.fancynpcs.api.Npc; import de.oliver.fancynpcs.api.events.NpcModifyEvent; import de.oliver.fancynpcs.api.skins.SkinData; +import de.oliver.fancynpcs.skins.SkinUtils; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; @@ -65,7 +66,25 @@ public void onSkin( translator.translate("command_npc_modification_cancelled").send(sender); } } else { - SkinData skinData = FancyNpcs.getInstance().getSkinManager().getByIdentifier(skin, (slim) ? SkinData.SkinVariant.SLIM : SkinData.SkinVariant.AUTO); + SkinData.SkinVariant variant = slim ? SkinData.SkinVariant.SLIM : SkinData.SkinVariant.AUTO; + SkinData skinData = FancyNpcs.getInstance().getSkinManagerImpl().tryToGetFromCache(skin, variant); + if (skinData == null) { + SkinUtils.applySkinLater( + npc.getData().getId(), + skin, + variant, + () -> { + translator.translate("npc_skin_set") + .replace("npc", npc.getData().getName()) + .replace("name", skin) + .send(sender); + }, + () -> { + translator.translate("npc_skin_set_error").replace("npc", npc.getData().getName()).send(sender); + }); + translator.translate("npc_skin_set_later").replace("npc", npc.getData().getName()).send(sender); + return; + } if (new NpcModifyEvent(npc, NpcModifyEvent.NpcModification.SKIN, false, sender).callEvent() && new NpcModifyEvent(npc, NpcModifyEvent.NpcModification.SKIN, skinData, sender).callEvent()) { npc.getData().setMirrorSkin(false); diff --git a/src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java b/src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java index 04f13154..688b39a3 100644 --- a/src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java +++ b/src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java @@ -38,7 +38,7 @@ public class SkinManagerImpl implements SkinManager { private final SkinCache memCache; public SkinManagerImpl(SkinCache fileCache, SkinCache memCache) { - this.executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder() + this.executor = Executors.newScheduledThreadPool(2, new ThreadFactoryBuilder() .setNameFormat("FancyNpcs-Skins") .build()); @@ -252,8 +252,8 @@ private SkinInfo executeRequest(GenerateRequest req) { return skinResp.join(); } - private SkinData tryToGetFromCache(String id, SkinData.SkinVariant variant) { - SkinCacheData data = memCache.getSkin(id); + public SkinData tryToGetFromCache(String identifier, SkinData.SkinVariant variant) { + SkinCacheData data = memCache.getSkin(identifier); if (data != null) { if (data.skinData().getVariant() != variant) { return null; @@ -262,7 +262,7 @@ private SkinData tryToGetFromCache(String id, SkinData.SkinVariant variant) { return data.skinData(); } - data = fileCache.getSkin(id); + data = fileCache.getSkin(identifier); if (data != null) { if (data.skinData().getVariant() != variant) { return null; @@ -287,4 +287,8 @@ public SkinCache getFileCache() { public SkinCache getMemCache() { return memCache; } + + public ScheduledExecutorService getExecutor() { + return executor; + } } diff --git a/src/main/java/de/oliver/fancynpcs/skins/SkinUtils.java b/src/main/java/de/oliver/fancynpcs/skins/SkinUtils.java index 067a9e79..a1cff4c8 100644 --- a/src/main/java/de/oliver/fancynpcs/skins/SkinUtils.java +++ b/src/main/java/de/oliver/fancynpcs/skins/SkinUtils.java @@ -1,5 +1,9 @@ package de.oliver.fancynpcs.skins; +import de.oliver.fancynpcs.FancyNpcs; +import de.oliver.fancynpcs.api.Npc; +import de.oliver.fancynpcs.api.skins.SkinData; + public class SkinUtils { public static boolean isPlaceholder(String identifier) { @@ -18,4 +22,35 @@ public static boolean isFile(String identifier) { return identifier.endsWith(".png") || identifier.endsWith(".jpg") || identifier.endsWith(".jpeg"); } + public static void applySkinLater(String npcID, String skinID, SkinData.SkinVariant variant, Runnable successCallback, Runnable errorCallback) { + FancyNpcs.getInstance().getSkinManagerImpl().getExecutor().submit(() -> { + SkinData skin = FancyNpcs.getInstance().getSkinManagerImpl().getByIdentifier(skinID, variant); + if (skin == null) { + FancyNpcs.getInstance().getFancyLogger().error("Could not fetch skin for npc '" + npcID + "'"); + errorCallback.run(); + return; + } + + Npc npc = FancyNpcs.getInstance().getNpcManager().getNpcById(npcID); + if (npc == null) { + FancyNpcs.getInstance().getFancyLogger().error("Could not find npc '" + npcID + "'"); + errorCallback.run(); + return; + } + + npc.getData().setMirrorSkin(false); + npc.getData().setSkin(skin); + npc.removeForAll(); + npc.create(); + npc.spawnForAll(); + + successCallback.run(); + }); + } + + public static void applySkinLater(String npcID, String skinID, SkinData.SkinVariant variant) { + applySkinLater(npcID, skinID, variant, () -> { + }, () -> { + }); + } }