mirror of
https://github.com/darksaid98/Flashback.git
synced 2026-12-01 14:38:50 +00:00
Add player skin loading from file
This commit is contained in:
1 parent
64e993a16d
commit
b2f4f62ad1
6 files changed
+115
-9
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package com.moulberry.flashback;
|
||||
|
||||
import com.mojang.blaze3d.platform.NativeImage;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.DynamicTexture;
|
||||
import net.minecraft.client.resources.DefaultPlayerSkin;
|
||||
import net.minecraft.client.resources.PlayerSkin;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
|
||||
public class FilePlayerSkin {
|
||||
|
||||
private static class CleanState implements Runnable {
|
||||
private ResourceLocation skinResourceLocation;
|
||||
|
||||
public CleanState(ResourceLocation skinResourceLocation) {
|
||||
this.skinResourceLocation = skinResourceLocation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (this.skinResourceLocation != null) {
|
||||
System.out.println("Cleaning " + this.skinResourceLocation + " because it's no longer in use!");
|
||||
Minecraft.getInstance().getTextureManager().release(this.skinResourceLocation);
|
||||
this.skinResourceLocation = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerSkin playerSkin = null;
|
||||
private final Path pathToSkin;
|
||||
|
||||
public FilePlayerSkin(Path pathToSkin) {
|
||||
this.pathToSkin = pathToSkin;
|
||||
}
|
||||
|
||||
public PlayerSkin getSkin() {
|
||||
if (this.playerSkin != null) {
|
||||
return this.playerSkin;
|
||||
}
|
||||
|
||||
try (InputStream inputStream = Files.newInputStream(this.pathToSkin)) {
|
||||
NativeImage nativeImage = NativeImage.read(inputStream);
|
||||
|
||||
// We determine the type using the alpha of the pixel at 54, 20
|
||||
int argb = nativeImage.getPixel(54, 20);
|
||||
PlayerSkin.Model model = PlayerSkin.Model.WIDE;
|
||||
if (((argb >> 24) & 0xFF) < 20) {
|
||||
model = PlayerSkin.Model.SLIM;
|
||||
}
|
||||
|
||||
DynamicTexture dynamicTexture = new DynamicTexture(nativeImage);
|
||||
|
||||
ResourceLocation resourceLocation = ResourceLocation.fromNamespaceAndPath("flashback", "skin_from_file/" + UUID.randomUUID());
|
||||
Minecraft.getInstance().getTextureManager().register(resourceLocation, dynamicTexture);
|
||||
GlobalCleaner.INSTANCE.register(this, new CleanState(resourceLocation));
|
||||
|
||||
// todo: determine skin type (wide or slim)
|
||||
this.playerSkin = new PlayerSkin(resourceLocation, null, null, null, model, false);
|
||||
} catch (Exception e) {
|
||||
Flashback.LOGGER.error("Unable to load skin from file", e);
|
||||
this.playerSkin = DefaultPlayerSkin.getDefaultSkin();
|
||||
}
|
||||
|
||||
return this.playerSkin;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user