Skip to content

Commit

Permalink
fix: add async support and image fallback for skin retrieval
Browse files Browse the repository at this point in the history
- Use thenApplyAsync instead of thenApply for better performance
- Add a lambda expression to get the skin image if the image supplier is null
- Check if the image supplier is null before using it
  • Loading branch information
GeorgeV220 committed Jan 3, 2024
1 parent ae533a1 commit b48932c
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,28 @@ public abstract CompletableFuture<Boolean> updateSkin(
*/
public abstract SGameProfile getGameProfile(@NotNull final PlayerObject playerObject) throws IOException, ExecutionException, InterruptedException;

public CompletableFuture<Skin> retrieveOrGenerateSkin(@NotNull PlayerObject playerObject, @Nullable ImageSupplier imageSupplier, @NotNull SkinParts skinParts) {
public CompletableFuture<Skin> retrieveOrGenerateSkin(@NotNull PlayerObject playerObject, @Nullable ImageSupplier image, @NotNull SkinParts skinParts) {
UUID skinUUID = Utilities.generateUUID(skinParts.getSkinName() + playerObject.playerUUID().toString());
return skinOverlay.getSkinManager().exists(skinUUID).thenApply(result -> {
return skinOverlay.getSkinManager().exists(skinUUID).thenApplyAsync(result -> {
if (result) {
skinOverlay.getLogger().info("Skin: " + skinUUID + " found for player: " + playerObject.playerName());
return skinOverlay.getSkinManager().getLoadedEntities().get(skinUUID);
} else {
try {
ImageSupplier imageSupplier = image;
if (imageSupplier == null) {
skinOverlay.getLogger().log(Level.SEVERE, "ImageSupplier cannot be null", new SkinException("ImageSupplier cannot be null"));
return null;
imageSupplier = () -> {
try {
return this.getSkinImage(playerObject);
} catch (ExecutionException | InterruptedException e) {
this.skinOverlay.getLogger().log(Level.SEVERE, "Failed to get image", e);
return null;
}
};
if (imageSupplier.get() == null) {
skinOverlay.getLogger().log(Level.SEVERE, "ImageSupplier cannot be null", new SkinException("ImageSupplier cannot be null"));
return null;
}
}

SkinConfigurationFile skinConfigurationFile = this.skinOverlay.getSkinFileCache().getCacheSkinConfig(skinParts.getSkinName());
Expand Down

0 comments on commit b48932c

Please sign in to comment.