Skip to content

Commit 4a6167e

Browse files
committed
Add mc skin redirect to out-of-jar texture loading (#116)
1 parent 589ee48 commit 4a6167e

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/main/java/com/ldtteam/blockui/mod/ClientEventSubscriber.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.mojang.blaze3d.platform.InputConstants;
1313
import net.minecraft.client.Minecraft;
1414
import net.minecraft.client.gui.screens.Screen;
15+
import net.minecraft.client.resources.PlayerSkin;
1516
import net.minecraft.network.chat.Component;
1617
import net.minecraft.resources.ResourceLocation;
1718
import net.minecraftforge.client.event.InputEvent.MouseScrollingEvent;
@@ -64,6 +65,12 @@ public static void onClientTickEvent(final ClientTickEvent event)
6465
window.addChild(createTestGuiButton(0, "General All-in-one", new ResourceLocation(BlockUI.MOD_ID, "gui/test.xml"), parent -> {
6566
parent.findPaneOfTypeByID("missing_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.ofMinecraftFolder(BlockUI.MOD_ID, "missing_out_of_jar.png"), false);
6667
parent.findPaneOfTypeByID("working_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.of(BlockUI.MOD_ID, Path.of("../../src/test/resources/button.png")), false);
68+
OutOfJarResourceLocation.ofMinecraftSkin(Minecraft.getInstance(), Minecraft.getInstance().getGameProfile(), null)
69+
.thenAccept(resLoc -> parent.findPaneOfTypeByID("player_skin", Image.class).setImage(resLoc, false));
70+
OutOfJarResourceLocation.ofMinecraftSkin(Minecraft.getInstance(), Minecraft.getInstance().getGameProfile(), PlayerSkin::capeTexture)
71+
.thenAccept(resLoc -> {if (resLoc!=null){parent.findPaneOfTypeByID("player_cape", Image.class).setImage(resLoc, false);}});
72+
OutOfJarResourceLocation.ofMinecraftSkin(Minecraft.getInstance(), Minecraft.getInstance().getGameProfile(), PlayerSkin::elytraTexture)
73+
.thenAccept(resLoc -> {if (resLoc!=null){parent.findPaneOfTypeByID("player_elytra", Image.class).setImage(resLoc, false);}});
6774
}));
6875
window.addChild(createTestGuiButton(1, "Tooltip Positioning", new ResourceLocation(BlockUI.MOD_ID, "gui/test2.xml")));
6976
window.addChild(createTestGuiButton(2, "ItemIcon To BlockState", new ResourceLocation(BlockUI.MOD_ID, "gui/test3.xml"), BlockStateTestGui::setup));

src/main/java/com/ldtteam/blockui/util/resloc/OutOfJarResourceLocation.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.ldtteam.blockui.util.resloc;
22

3+
import com.mojang.authlib.GameProfile;
34
import net.minecraft.client.Minecraft;
5+
import net.minecraft.client.renderer.texture.AbstractTexture;
6+
import net.minecraft.client.renderer.texture.HttpTexture;
7+
import net.minecraft.client.resources.DefaultPlayerSkin;
48
import net.minecraft.resources.ResourceLocation;
59
import net.minecraft.server.packs.PackResources;
610
import net.minecraft.server.packs.resources.FallbackResourceManager;
@@ -15,6 +19,7 @@
1519
import java.io.InputStream;
1620
import java.nio.file.Files;
1721
import java.nio.file.Path;
22+
import java.util.function.Function;
1823
import java.util.function.UnaryOperator;
1924

2025
public class OutOfJarResourceLocation extends ResourceLocation
@@ -44,6 +49,32 @@ public static OutOfJarResourceLocation ofMinecraftFolder(final String namespace,
4449
return of(namespace, path);
4550
}
4651

52+
/**
53+
* @param minecraft minecraft instance
54+
* @param gameProfile player profile
55+
* @param textureSelector null for {@code PlayerSkin#texture()}, or {@code PlayerSkin#capeTexture()} or
56+
* {@code PlayerSkin#elytraTexture()} - both cape and elytry may return null future
57+
*/
58+
public static ResourceLocation ofMinecraftSkin(
59+
final Minecraft minecraft,
60+
final GameProfile gameProfile,
61+
@Nullable final Function<DefaultPlayerSkin, ResourceLocation> textureSelector)
62+
{
63+
final ResourceLocation skinResLoc = minecraft.getSkinManager().getInsecureSkinLocation(gameProfile);
64+
if (skinResLoc == null)
65+
{
66+
return null;
67+
}
68+
69+
final AbstractTexture texture = minecraft.getTextureManager().getTexture(skinResLoc);
70+
if (!(texture instanceof final HttpTexture httpTexture))
71+
{
72+
return skinResLoc;
73+
}
74+
75+
return new OutOfJarResourceLocation(skinResLoc.getNamespace(), httpTexture.file.toPath(), skinResLoc.getPath());
76+
}
77+
4778
public Path getNioPath()
4879
{
4980
return nioPath;

src/main/resources/META-INF/accesstransformer.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ public com.mojang.blaze3d.vertex.PoseStack f_85834_ # poseStack
1717

1818
# blockstate rendering
1919
public net.minecraft.client.resources.model.ModelBakery f_244132_ # modelResources
20+
public net.minecraft.client.renderer.texture.HttpTexture f_117994_

src/main/resources/assets/blockui/gui/test.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@
4949
<itemicon item="minecraft:oak_stairs" size="64 64" pos="580 50"/>
5050
<itemicon item="minecraft:clock" properties="{_item:{time:0.5}}" size="32 32" pos="650 50"/>
5151
<itemicon item="minecraft:clock" size="32 32" pos="700 50"/>
52+
<image id="player_skin" pos="300 300" size="128 128" source="blockui:veryuglymissingtexture"/>
53+
<image id="player_cape" pos="428 300" size="128 64" source="blockui:veryuglymissingtexture"/>
54+
<image id="player_elytra" pos="556 300" size="128 128" source="blockui:veryuglymissingtexture"/>
5255
</zoomdragview>
5356
</window>

0 commit comments

Comments
 (0)