Skip to content

Commit

Permalink
fix caching
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Dec 21, 2024
1 parent 80cef99 commit 2c41a28
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public void loadNpcs() {
String skinVariantStr = npcConfig.getString("npcs." + id + ".skin.variant", SkinData.SkinVariant.AUTO.name());
SkinData.SkinVariant skinVariant = SkinData.SkinVariant.valueOf(skinVariantStr);
if (!skinIdentifier.isEmpty()) {
skin = FancyNpcs.getInstance().getSkinManagerImpl().tryToGetFromCache(skinIdentifier, skinVariant);
skin = FancyNpcs.getInstance().getSkinManagerImpl().getByIdentifierCached(skinIdentifier, skinVariant);
if (skin == null) {
applySkinLater = true;
}
Expand Down Expand Up @@ -430,7 +430,7 @@ public void loadNpcs() {
npc.create();
registerNpc(npc);

if(applySkinLater) {
if (applySkinLater) {
SkinUtils.applySkinLater(id, skinIdentifier, skinVariant);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onSkin(
}
} else {
SkinData.SkinVariant variant = slim ? SkinData.SkinVariant.SLIM : SkinData.SkinVariant.AUTO;
SkinData skinData = FancyNpcs.getInstance().getSkinManagerImpl().tryToGetFromCache(skin, variant);
SkinData skinData = FancyNpcs.getInstance().getSkinManagerImpl().getByIdentifierCached(skin, variant);
if (skinData == null) {
SkinUtils.applySkinLater(
npc.getData().getId(),
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ public SkinData getByIdentifier(String identifier, SkinData.SkinVariant variant)
return getByUUID(uuid, variant);
}

public SkinData getByIdentifierCached(String identifier, SkinData.SkinVariant variant) {
if (SkinUtils.isUUID(identifier) || SkinUtils.isURL(identifier) || SkinUtils.isFile(identifier)) {
return tryToGetFromCache(identifier, variant);
}

if (SkinUtils.isPlaceholder(identifier)) {
String parsed = ChatColorHandler.translate(identifier);

if (SkinUtils.isPlaceholder(parsed)) {
return null;
}

return tryToGetFromCache(parsed, variant);
}

// is username
UUID uuid = UUIDFetcher.getUUID(identifier);
if (uuid == null) {
return null;
}

return tryToGetFromCache(uuid.toString(), variant);
}

@Override
public SkinData getByUUID(UUID uuid, SkinData.SkinVariant variant) {
SkinData cached = tryToGetFromCache(uuid.toString(), variant);
Expand Down Expand Up @@ -254,7 +278,9 @@ private SkinInfo executeRequest(GenerateRequest req) {
return skinResp.join();
}

public SkinData tryToGetFromCache(String identifier, SkinData.SkinVariant variant) {
private SkinData tryToGetFromCache(String identifier, SkinData.SkinVariant variant) {
FancyNpcs.getInstance().getFancyLogger().debug("Trying to get skin from mem cache: " + identifier);

SkinCacheData data = memCache.getSkin(identifier);
if (data != null) {
if (data.skinData().getVariant() != variant) {
Expand All @@ -264,6 +290,8 @@ public SkinData tryToGetFromCache(String identifier, SkinData.SkinVariant varian
return data.skinData();
}

FancyNpcs.getInstance().getFancyLogger().debug("Trying to get skin from file cache: " + identifier);

data = fileCache.getSkin(identifier);
if (data != null) {
if (data.skinData().getVariant() != variant) {
Expand All @@ -274,6 +302,8 @@ public SkinData tryToGetFromCache(String identifier, SkinData.SkinVariant varian
return data.skinData();
}

FancyNpcs.getInstance().getFancyLogger().debug("Skin not found in cache: " + identifier);

return null;
}

Expand Down

0 comments on commit 2c41a28

Please sign in to comment.