Skip to content

Commit

Permalink
Merge pull request #20 from mineblock11/async
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai authored Jun 27, 2023
2 parents 1d94484 + c4b737e commit 6415e01
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,34 +196,38 @@ public static void apply() {
try {
ConfigSkin configSkin = preset.getSkin().saveToConfig();

if (preset.getSkin() instanceof UrlSkin urlSkin) {
SkinAPIs.setSkinTexture(urlSkin.getUrl(), urlSkin.getModel());
} else {
SkinAPIs.setSkinTexture(configSkin.getFile().toFile(), configSkin.getModel());
}

if (client.world != null && ClientSkinHandling.isInstalledOnServer()) {
new Thread(() -> {
client.executeTask(() -> {
try {
String cachedURL = SkinCacheRegistry.getCachedUploadedSkin(configSkin.getFile().toFile());
Skin result;
if(cachedURL != null) {
result = SkinAPIs.MINESKIN_CLIENT.generateUrl(cachedURL).join();
} else {
result = SkinAPIs.MINESKIN_CLIENT.generateUpload(configSkin.getFile().toFile()).join();
try {
if (preset.getSkin() instanceof UrlSkin urlSkin) {
SkinAPIs.setSkinTexture(urlSkin.getUrl(), urlSkin.getModel());
} else {
SkinAPIs.setSkinTexture(configSkin.getFile().toFile(), configSkin.getModel());
}

if (client.world != null && ClientSkinHandling.isInstalledOnServer()) {
new Thread(() -> {
client.executeTask(() -> {
try {
String cachedURL = SkinCacheRegistry.getCachedUploadedSkin(configSkin.getFile().toFile());
Skin result;
if(cachedURL != null) {
result = SkinAPIs.MINESKIN_CLIENT.generateUrl(cachedURL).join();
} else {
result = SkinAPIs.MINESKIN_CLIENT.generateUpload(configSkin.getFile().toFile()).join();
}

SkinQueryResult queryResult = new SkinQueryResult(false, null, preset.getSkin().getModel(), result.data.texture.signature, result.data.texture.value);
ClientSkinHandling.sendRefresh(queryResult);
} catch (Exception e) {
SkinShuffle.LOGGER.error(e.getMessage());
}

SkinQueryResult queryResult = new SkinQueryResult(false, null, preset.getSkin().getModel(), result.data.texture.signature, result.data.texture.value);
ClientSkinHandling.sendRefresh(queryResult);
} catch (Exception e) {
SkinShuffle.LOGGER.error(e.getMessage());
}
});
}).start();
});
}).start();
}
} catch (Exception e) {
SkinShuffle.LOGGER.error("Failed to apply skin preset.", e);
}
} catch (Exception e) {
SkinShuffle.LOGGER.error("Failed to apply skin preset.", e);
} catch (Exception ignored) {
SkinShuffle.LOGGER.info("Skipping skin preset application due to skin not being fully loaded. If this is first startup, please ignore this message.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import com.mineblock11.skinshuffle.api.SkinAPIs;
import com.mineblock11.skinshuffle.client.skin.ResourceSkin;
import com.mineblock11.skinshuffle.client.skin.Skin;
import com.mineblock11.skinshuffle.client.skin.UUIDSkin;
import com.mineblock11.skinshuffle.client.skin.UrlSkin;
import com.mineblock11.skinshuffle.util.NetworkingUtil;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mojang.util.UUIDTypeAdapter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.Session;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -77,16 +79,7 @@ public static SkinPreset generateDefaultPreset() {

return new SkinPreset(skin, name);
} else {
var skinQueryResult = SkinAPIs.getPlayerSkinTexture(session.getUuid());

if(skinQueryResult.usesDefaultSkin()) {
Identifier skinTexture = client.getSkinProvider().loadSkin(session.getProfile());
Skin skin = new ResourceSkin(skinTexture, skinTexture.getPath().contains("/slim/") ? "slim" : "default");

return new SkinPreset(skin, name);
}

return new SkinPreset(new UrlSkin(skinQueryResult.skinURL(), skinQueryResult.modelType()), name);
return new SkinPreset(new UUIDSkin(UUIDTypeAdapter.fromString(session.getUuid()), null), name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,33 @@

import com.mineblock11.skinshuffle.SkinShuffle;
import com.mineblock11.skinshuffle.api.SkinAPIs;
import com.mineblock11.skinshuffle.client.preset.SkinPreset;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.texture.AbstractTexture;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

public class UUIDSkin extends UrlSkin {
public static final Identifier SERIALIZATION_ID = SkinShuffle.id("uuid");
public static final Codec<UUIDSkin> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("uuid").forGetter(skin -> skin.uuid.toString()),
Codec.STRING.fieldOf("model").forGetter(UUIDSkin::getModel)
).apply(instance, (uuid, model) -> new UUIDSkin(UUID.fromString(uuid), model)));
Codec.STRING.optionalFieldOf("model").forGetter(skin -> Optional.ofNullable(skin.model))
).apply(instance, (uuid, model) -> new UUIDSkin(UUID.fromString(uuid), model.orElse(null))));

protected UUID uuid;

public UUIDSkin(UUID uuid, String model) {
public UUIDSkin(UUID uuid, @Nullable String model) {
super(model);
this.uuid = uuid;
}

protected UUIDSkin(String model) {
protected UUIDSkin(@Nullable String model) {
super(model);
}

Expand All @@ -63,10 +66,19 @@ protected Object getTextureUniqueness() {
}

url = profile.skinURL();
if (model == null) {
model = profile.modelType();
cacheModel();
}

return super.loadTexture(completionCallback);
}

@Override
public Identifier getSerializationId() {
return SERIALIZATION_ID;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/com/mineblock11/skinshuffle/client/skin/UrlSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.mineblock11.skinshuffle.SkinShuffle;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import kong.unirest.Unirest;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.texture.AbstractTexture;
Expand All @@ -33,29 +35,41 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;

public class UrlSkin extends BackedSkin {
public static final Int2ObjectMap<String> MODEL_CACHE = new Int2ObjectOpenHashMap<>();

public static final Identifier SERIALIZATION_ID = SkinShuffle.id("url");
public static final Codec<UrlSkin> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("url").forGetter(skin -> skin.url),
Codec.STRING.fieldOf("model").forGetter(UrlSkin::getModel)
Codec.STRING.optionalFieldOf("model").forGetter(skin -> Optional.ofNullable(skin.model))
).apply(instance, UrlSkin::new));

protected String url;
private String model;
protected String model;

public UrlSkin(String url, String model) {
this.url = url;
this.model = model;
}

protected UrlSkin(String model) {
private UrlSkin(String url, Optional<String> model) {
this.url = url;
this.model = model.orElse(null);
}

protected UrlSkin(@Nullable String model) {
this.model = model;
}

@Override
public String getModel() {
return model;
if (model == null) {
tryLoadModelFromCache();
}

return model == null ? "default" : model;
}

@Override
Expand All @@ -76,6 +90,10 @@ protected Object getTextureUniqueness() {
@Override
public ConfigSkin saveToConfig() {
try {
if (model == null) {
throw new RuntimeException("Model is not loaded yet");
}

var textureName = String.valueOf(Math.abs(getTextureUniqueness().hashCode()));
var configSkin = new ConfigSkin(textureName, getModel());

Expand Down Expand Up @@ -142,4 +160,15 @@ public int hashCode() {
result = 31 * result + (model != null ? model.hashCode() : 0);
return result;
}

protected void cacheModel() {
MODEL_CACHE.put(getTextureUniqueness().hashCode(), getModel());
}

protected void tryLoadModelFromCache() {
var model = MODEL_CACHE.get(getTextureUniqueness().hashCode());
if (model != null) {
setModel(model);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ public class UsernameSkin extends UUIDSkin {
public static final Identifier SERIALIZATION_ID = SkinShuffle.id("username");
public static final Codec<UsernameSkin> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("username").forGetter(skin -> skin.username),
Codec.STRING.fieldOf("model").forGetter(UsernameSkin::getModel)
Codec.STRING.optionalFieldOf("model").forGetter(skin -> Optional.ofNullable(skin.model))
).apply(instance, UsernameSkin::new));

private final String username;

public UsernameSkin(String username, String model) {
public UsernameSkin(String username, @Nullable String model) {
super(model);
this.username = username;
}

private UsernameSkin(String username, Optional<String> model) {
this(username, model.orElse(null));
}

@Override
public ConfigSkin saveToConfig() {
Optional<UUID> uuid = SkinAPIs.getUUIDFromUsername(this.username);
Expand Down

0 comments on commit 6415e01

Please sign in to comment.