some more head code fixes

This commit is contained in:
rockyhawk64 2025-10-15 12:10:20 +11:00
parent 0c107e9c00
commit dc2aab1d22

View File

@ -6,7 +6,7 @@ import com.google.gson.JsonParser;
import me.rockyhawk.commandpanels.Context; import me.rockyhawk.commandpanels.Context;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.profile.PlayerTextures; import org.bukkit.profile.PlayerTextures;
@ -104,15 +104,17 @@ public class CustomHeads {
// Try to get a profile from cache first // Try to get a profile from cache first
PlayerProfile profile = profileCache.get(key); PlayerProfile profile = profileCache.get(key);
if (profile == null) { if(profile == null){
// Fallback to offline player profile to show textures when players are already online // Use local texture if player is currently online
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerName); Player onlinePlayer = Bukkit.getPlayerExact(playerName);
profile = offlinePlayer.getPlayerProfile(); if(onlinePlayer != null){
profile = onlinePlayer.getPlayerProfile();
// Enqueue async task to fill cache for next time profileCache.put(key, profile);
enqueuePlayerHead(key); }
} }
if (!profileCache.containsKey(key)) enqueuePlayerHead(key);
// Put profile on the head // Put profile on the head
skullMeta.setPlayerProfile(profile); skullMeta.setPlayerProfile(profile);
skull.setItemMeta(skullMeta); skull.setItemMeta(skullMeta);
@ -128,17 +130,16 @@ public class CustomHeads {
*/ */
private void cachePlayerHeadAsync(String playerName) { private void cachePlayerHeadAsync(String playerName) {
String key = playerName.toLowerCase(); String key = playerName.toLowerCase();
if (profileCache.containsKey(key)) return; // already cached
Bukkit.getAsyncScheduler().runNow(ctx.plugin, (t) -> { Bukkit.getAsyncScheduler().runNow(ctx.plugin, (t) -> {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerName); PlayerProfile p = Bukkit.createProfile(playerName);
PlayerProfile p = offlinePlayer.getPlayerProfile(); p.complete(true); // network calls
if (p == null) { if(p.isComplete()){
UUID offlineUuid = UUID.nameUUIDFromBytes(playerName.getBytes(StandardCharsets.UTF_8)); profileCache.put(key, p);
p = Bukkit.createProfile(offlineUuid, playerName); }else{
profileCache.put(key, null);
enqueuePlayerHead(key); // allow a second try
} }
p.complete(true); // network call
profileCache.put(key, p);
}); });
} }
@ -168,6 +169,6 @@ public class CustomHeads {
// Run the async fetch for this key // Run the async fetch for this key
Bukkit.getAsyncScheduler().runNow(ctx.plugin, t -> cachePlayerHeadAsync(next)); Bukkit.getAsyncScheduler().runNow(ctx.plugin, t -> cachePlayerHeadAsync(next));
} }
}, 1, 15); // tick interval per head api lookup }, 1, 20); // tick interval per head api lookup
} }
} }