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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.moulberry.flashback;
|
||||
|
||||
import java.lang.ref.Cleaner;
|
||||
|
||||
public class GlobalCleaner {
|
||||
public static final Cleaner INSTANCE = Cleaner.create();
|
||||
}
|
||||
@@ -2,17 +2,24 @@ package com.moulberry.flashback.editor.ui.windows;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.yggdrasil.ProfileResult;
|
||||
import com.moulberry.flashback.FilePlayerSkin;
|
||||
import com.moulberry.flashback.Flashback;
|
||||
import com.moulberry.flashback.editor.ui.ImGuiHelper;
|
||||
import com.moulberry.flashback.exporting.AsyncFileDialogs;
|
||||
import com.moulberry.flashback.state.EditorState;
|
||||
import imgui.ImGui;
|
||||
import imgui.type.ImString;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.PlayerInfo;
|
||||
import net.minecraft.commands.arguments.EntityAnchorArgument;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class SelectedEntityPopup {
|
||||
|
||||
@@ -98,25 +105,35 @@ public class SelectedEntityPopup {
|
||||
ImGui.setNextItemWidth(320);
|
||||
ImGui.inputTextWithHint("##SetSkinInput", "e.g. d0e05de7-6067-454d-beae-c6d19d886191", changeSkinInput);
|
||||
|
||||
boolean hasApplySkin = false;
|
||||
if (!changeSkinInput.isEmpty()) {
|
||||
String string = ImGuiHelper.getString(changeSkinInput);
|
||||
try {
|
||||
UUID changeSkinUuid = UUID.fromString(string);
|
||||
if (ImGui.button("Apply Skin")) {
|
||||
if (ImGui.button("Apply Skin from UUID")) {
|
||||
ProfileResult profile = Minecraft.getInstance().getMinecraftSessionService().fetchProfile(changeSkinUuid, true);
|
||||
editorState.skinOverride.put(entity.getUUID(), profile.profile());
|
||||
editorState.skinOverrideFromFile.remove(entity.getUUID());
|
||||
}
|
||||
hasApplySkin = true;
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
if (editorState.skinOverride.containsKey(entity.getUUID())) {
|
||||
if (hasApplySkin) {
|
||||
ImGui.sameLine();
|
||||
}
|
||||
if (ImGui.button("Upload Skin from File")) {
|
||||
Path gameDir = FabricLoader.getInstance().getGameDir();
|
||||
CompletableFuture<String> future = AsyncFileDialogs.openFileDialog(gameDir.toString(),
|
||||
"Skin Texture", "png");
|
||||
future.thenAccept(pathStr -> {
|
||||
if (pathStr != null) {
|
||||
Path path = Path.of(pathStr);
|
||||
editorState.skinOverride.remove(entity.getUUID());
|
||||
editorState.skinOverrideFromFile.put(entity.getUUID(), new FilePlayerSkin(path));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (editorState.skinOverride.containsKey(entity.getUUID()) || editorState.skinOverrideFromFile.containsKey(entity.getUUID())) {
|
||||
if (ImGui.button("Reset Skin")) {
|
||||
editorState.skinOverride.remove(entity.getUUID());
|
||||
editorState.skinOverrideFromFile.remove(entity.getUUID());
|
||||
changeSkinInput.set("");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.moulberry.flashback.mixin;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
import com.mojang.authlib.yggdrasil.ProfileResult;
|
||||
import com.moulberry.flashback.FilePlayerSkin;
|
||||
import com.moulberry.flashback.state.EditorState;
|
||||
import com.moulberry.flashback.state.EditorStateManager;
|
||||
import net.minecraft.client.Minecraft;
|
||||
@@ -32,6 +33,12 @@ public abstract class MixinAbstractClientPlayer extends Player {
|
||||
public void getSkin(CallbackInfoReturnable<PlayerSkin> cir) {
|
||||
EditorState editorState = EditorStateManager.getCurrent();
|
||||
if (editorState != null) {
|
||||
FilePlayerSkin filePlayerSkin = editorState.skinOverrideFromFile.get(this.getUUID());
|
||||
if (filePlayerSkin != null) {
|
||||
cir.setReturnValue(filePlayerSkin.getSkin());
|
||||
return;
|
||||
}
|
||||
|
||||
GameProfile skinOverride = editorState.skinOverride.get(this.uuid);
|
||||
if (skinOverride != null) {
|
||||
if (skinOverridePlayerInfo == null || skinOverridePlayerInfo.getProfile() != skinOverride) {
|
||||
|
||||
@@ -24,8 +24,9 @@ public abstract class MixinPlayer extends LivingEntity {
|
||||
public void isModelPartShown(PlayerModelPart playerModelPart, CallbackInfoReturnable<Boolean> cir) {
|
||||
EditorState editorState = EditorStateManager.getCurrent();
|
||||
if (editorState != null) {
|
||||
GameProfile skinOverride = editorState.skinOverride.get(this.uuid);
|
||||
if (skinOverride != null) {
|
||||
var skinOverride = editorState.skinOverride.get(this.uuid);
|
||||
var skinOverrideFile = editorState.skinOverrideFromFile.get(this.uuid);
|
||||
if (skinOverride != null || skinOverrideFile != null) {
|
||||
cir.setReturnValue(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.moulberry.flashback.state;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.moulberry.flashback.FilePlayerSkin;
|
||||
import com.moulberry.flashback.Flashback;
|
||||
import com.moulberry.flashback.FlashbackGson;
|
||||
import com.moulberry.flashback.editor.ui.ReplayUI;
|
||||
@@ -48,6 +49,7 @@ public class EditorState {
|
||||
public Set<UUID> hideDuringExport = new HashSet<>();
|
||||
public Set<UUID> hideNametags = new HashSet<>();
|
||||
public Map<UUID, GameProfile> skinOverride = new HashMap<>();
|
||||
public Map<UUID, FilePlayerSkin> skinOverrideFromFile = new HashMap<>();
|
||||
public Map<UUID, String> nameOverride = new HashMap<>();
|
||||
|
||||
public EditorState() {
|
||||
|
||||
Reference in new issue
Block a user