Skip to content

Commit

Permalink
Spawn with default skin and load actual skin later
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Dec 21, 2024
1 parent 0278f3f commit 46381ef
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -428,6 +429,10 @@ public void loadNpcs() {

npc.create();
registerNpc(npc);

if(applySkinLater) {
SkinUtils.applySkinLater(id, skinIdentifier, skinVariant);
}
}

isLoaded = true;
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/de/oliver/fancynpcs/commands/npc/SkinCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -287,4 +287,8 @@ public SkinCache getFileCache() {
public SkinCache getMemCache() {
return memCache;
}

public ScheduledExecutorService getExecutor() {
return executor;
}
}
35 changes: 35 additions & 0 deletions src/main/java/de/oliver/fancynpcs/skins/SkinUtils.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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, () -> {
}, () -> {
});
}
}

0 comments on commit 46381ef

Please sign in to comment.