├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src └── main │ ├── resources │ ├── assets │ │ └── BedrockSkinUtility │ │ │ └── icon.png │ ├── BedrockSkinUtility.mixins.json │ └── fabric.mod.json │ └── java │ └── net │ └── camotoy │ └── bedrockskinutility │ └── client │ ├── BedrockSkinPluginMessageType.java │ ├── interfaces │ ├── BedrockPlayerInfo.java │ └── BedrockPlayerSkin.java │ ├── BedrockPlayerEntityModel.java │ ├── BedrockCachedProperties.java │ ├── mixin │ ├── PlayerEntityRendererChangeModel.java │ ├── PlayerSkinFieldAccessor.java │ ├── BedrockAbstractClientPlayerEntity.java │ ├── PlayerInfoMixin.java │ ├── PlayerSkinMixin.java │ ├── CapeFeatureRendererMixin.java │ ├── EntityRendererDispatcherMixin.java │ └── ClientPlayNetworkHandlerMixin.java │ ├── pluginmessage │ ├── data │ │ ├── SkinData.java │ │ ├── BedrockData.java │ │ ├── CapeData.java │ │ └── BaseSkinInfo.java │ ├── GeyserSkinManagerListener.java │ └── BedrockMessageHandler.java │ ├── SkinManager.java │ ├── BedrockSkinUtilityClient.java │ ├── PlayerSkinBuilder.java │ ├── MixinConfigPlugin.java │ ├── SkinInfo.java │ ├── SkinUtils.java │ └── GeometryUtil.java ├── settings.gradle ├── gradle.properties ├── README.md ├── .github └── workflows │ └── build.yml ├── LICENSE ├── .gitignore ├── gradlew.bat └── gradlew /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Camotoy/BedrockSkinUtility/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/resources/assets/BedrockSkinUtility/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Camotoy/BedrockSkinUtility/HEAD/src/main/resources/assets/BedrockSkinUtility/icon.png -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/BedrockSkinPluginMessageType.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | public enum BedrockSkinPluginMessageType { 4 | CAPE, 5 | SKIN_INFORMATION, 6 | SKIN_DATA 7 | } 8 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | jcenter() 4 | maven { 5 | name = 'Fabric' 6 | url = 'https://maven.fabricmc.net/' 7 | } 8 | gradlePluginPortal() 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/interfaces/BedrockPlayerInfo.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.interfaces; 2 | 3 | import net.minecraft.client.renderer.entity.player.PlayerRenderer; 4 | 5 | public interface BedrockPlayerInfo { 6 | PlayerRenderer bedrockskinutility$getModel(); 7 | 8 | void bedrockskinutility$setModel(PlayerRenderer renderer); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/interfaces/BedrockPlayerSkin.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.interfaces; 2 | 3 | public interface BedrockPlayerSkin { 4 | boolean bedrockskinutility$bedrockSkin(); 5 | 6 | boolean bedrockskinutility$bedrockCape(); 7 | 8 | void bedrockskinutility$bedrockSkin(boolean bedrockSkin); 9 | 10 | void bedrockskinutility$bedrockCape(boolean bedrockCape); 11 | } 12 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Done to increase the memory available to gradle. 2 | org.gradle.jvmargs=-Xmx2G 3 | # Fabric Properties 4 | # check these on https://modmuss50.me/fabric.html 5 | minecraft_version=1.21 6 | loader_version=0.15.11 7 | # Mod Properties 8 | mod_version=1.6.0 9 | maven_group=net.camotoy 10 | archives_base_name=BedrockSkinUtility 11 | # Dependencies 12 | # check this on https://modmuss50.me/fabric.html 13 | fabric_version=0.100.3+1.21 -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/BedrockPlayerEntityModel.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import net.minecraft.client.model.PlayerModel; 4 | import net.minecraft.client.model.geom.ModelPart; 5 | import net.minecraft.world.entity.LivingEntity; 6 | 7 | public class BedrockPlayerEntityModel extends PlayerModel { 8 | public BedrockPlayerEntityModel(ModelPart root) { 9 | super(root, false); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/BedrockCachedProperties.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import net.minecraft.client.renderer.entity.player.PlayerRenderer; 4 | import net.minecraft.resources.ResourceLocation; 5 | 6 | /** 7 | * All cached properties of a player, if their PlayerListPacket has not sent or is being reloaded 8 | */ 9 | public class BedrockCachedProperties { 10 | public ResourceLocation cape; 11 | public PlayerRenderer model; 12 | public ResourceLocation skin; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/PlayerEntityRendererChangeModel.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.minecraft.client.model.EntityModel; 4 | import net.minecraft.client.renderer.entity.LivingEntityRenderer; 5 | import org.spongepowered.asm.mixin.Mixin; 6 | import org.spongepowered.asm.mixin.gen.Accessor; 7 | 8 | @Mixin(LivingEntityRenderer.class) 9 | public interface PlayerEntityRendererChangeModel { 10 | @Accessor("model") 11 | void bedrockskinutility$setModel(EntityModel model); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/PlayerSkinFieldAccessor.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.minecraft.client.multiplayer.PlayerInfo; 4 | import net.minecraft.client.resources.PlayerSkin; 5 | import org.spongepowered.asm.mixin.Mixin; 6 | import org.spongepowered.asm.mixin.Mutable; 7 | import org.spongepowered.asm.mixin.gen.Accessor; 8 | 9 | import java.util.function.Supplier; 10 | 11 | @Mixin(PlayerInfo.class) 12 | public interface PlayerSkinFieldAccessor { 13 | @Accessor("skinLookup") 14 | @Mutable 15 | void setPlayerSkin(Supplier playerSkin); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/resources/BedrockSkinUtility.mixins.json: -------------------------------------------------------------------------------- 1 | { 2 | "required": true, 3 | "minVersion": "0.8", 4 | "package": "net.camotoy.bedrockskinutility.client.mixin", 5 | "compatibilityLevel": "JAVA_17", 6 | "client": [ 7 | "BedrockAbstractClientPlayerEntity", 8 | "CapeFeatureRendererMixin", 9 | "ClientPlayNetworkHandlerMixin", 10 | "EntityRendererDispatcherMixin", 11 | "PlayerEntityRendererChangeModel", 12 | "PlayerInfoMixin", 13 | "PlayerSkinFieldAccessor", 14 | "PlayerSkinMixin" 15 | ], 16 | "plugin": "net.camotoy.bedrockskinutility.client.MixinConfigPlugin", 17 | "injectors": { 18 | "defaultRequire": 1 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/BedrockAbstractClientPlayerEntity.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.minecraft.client.multiplayer.PlayerInfo; 4 | import net.minecraft.client.player.AbstractClientPlayer; 5 | import org.spongepowered.asm.mixin.Mixin; 6 | import org.spongepowered.asm.mixin.gen.Invoker; 7 | 8 | @Mixin(AbstractClientPlayer.class) 9 | public interface BedrockAbstractClientPlayerEntity { 10 | /** 11 | * Expose the player list entry for retrieving the model of this player 12 | */ 13 | @Invoker("getPlayerInfo") 14 | PlayerInfo bedrockskinutility$getPlayerListEntry(); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/PlayerInfoMixin.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.camotoy.bedrockskinutility.client.interfaces.BedrockPlayerInfo; 4 | import net.minecraft.client.multiplayer.PlayerInfo; 5 | import net.minecraft.client.renderer.entity.player.PlayerRenderer; 6 | import org.spongepowered.asm.mixin.Mixin; 7 | 8 | @Mixin(PlayerInfo.class) 9 | public class PlayerInfoMixin implements BedrockPlayerInfo { 10 | private PlayerRenderer bedrockModel; 11 | 12 | @Override 13 | public PlayerRenderer bedrockskinutility$getModel() { 14 | return this.bedrockModel; 15 | } 16 | 17 | @Override 18 | public void bedrockskinutility$setModel(PlayerRenderer renderer) { 19 | this.bedrockModel = renderer; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/resources/fabric.mod.json: -------------------------------------------------------------------------------- 1 | { 2 | "schemaVersion": 1, 3 | "id": "bedrockskinutility", 4 | "version": "${version}", 5 | "name": "BedrockSkinUtility", 6 | "description": "BedrockSkinUtility is a client-only mod that allows Bedrock Edition skin and cape properties to appear rendered. Any non-character-creator geometry should render, and any Bedrock capes on the player will also render.", 7 | "authors": [ 8 | "Camotoy" 9 | ], 10 | "contact": {}, 11 | "license": "MIT", 12 | "icon": "assets/BedrockSkinUtility/icon.png", 13 | "environment": "client", 14 | "entrypoints": { 15 | "client": [ 16 | "net.camotoy.bedrockskinutility.client.BedrockSkinUtilityClient" 17 | ] 18 | }, 19 | "mixins": [ 20 | "BedrockSkinUtility.mixins.json" 21 | ], 22 | "depends": { 23 | "fabricloader": ">=0.15.11", 24 | "fabric-api": "*", 25 | "minecraft": ">=1.21" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BedrockSkinUtility 2 | 3 | Fabric mod that allows you to view Bedrock skins and capes. 4 | 5 | ![2021-08-02_22 06 21](https://user-images.githubusercontent.com/20743703/127946308-e68f88e8-11ec-47da-a3f2-63034fe0540e.png) 6 | 7 | 8 | Persona/character creator skins are not supported at this time, though this will be supported in the future. 9 | 10 | Setup: 11 | 12 | - Install this mod clientside 13 | - Install this plugin on your server: https://github.com/Camotoy/GeyserSkinManager 14 | - Done! 15 | 16 | Logo courtesy of NotNotNotSwipez (Discord: notnotnotswipez#9106). 17 | 18 | Thanks to https://github.com/VelvetMC/SimpleCapes for helping me figure out how capes work in Fabric, and for the base of translucent capes. 19 | 20 | Thanks to Blockbench for having a convert option to Fabric which greatly helped me get the last puzzle pieces together. 21 | 22 | Camotoy's Discord server: https://discord.gg/jNNC4CZtsN 23 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/PlayerSkinMixin.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.camotoy.bedrockskinutility.client.interfaces.BedrockPlayerSkin; 4 | import net.minecraft.client.resources.PlayerSkin; 5 | import org.spongepowered.asm.mixin.Mixin; 6 | 7 | @Mixin(PlayerSkin.class) 8 | public class PlayerSkinMixin implements BedrockPlayerSkin { 9 | private boolean bedrockSkin; 10 | private boolean bedrockCape; 11 | 12 | @Override 13 | public boolean bedrockskinutility$bedrockSkin() { 14 | return bedrockSkin; 15 | } 16 | 17 | @Override 18 | public boolean bedrockskinutility$bedrockCape() { 19 | return bedrockCape; 20 | } 21 | 22 | @Override 23 | public void bedrockskinutility$bedrockSkin(boolean bedrockSkin) { 24 | this.bedrockSkin = bedrockSkin; 25 | } 26 | 27 | @Override 28 | public void bedrockskinutility$bedrockCape(boolean bedrockCape) { 29 | this.bedrockCape = bedrockCape; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | paths-ignore: 7 | - '.gitignore' 8 | - 'CONTRIBUTING.md' 9 | - 'LICENSE' 10 | - 'README.md' 11 | - 'licenseheader.txt' 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@72f2cec99f417b1a1c5e2e88945068983b7965f9 18 | - name: Change wrapper permissions 19 | run: chmod +x ./gradlew 20 | - uses: gradle/wrapper-validation-action@56b90f209b02bf6d1deae490e9ef18b21a389cd4 21 | - uses: actions/setup-java@4075bfc1b51bf22876335ae1cd589602d60d8758 22 | with: 23 | distribution: 'temurin' 24 | java-version: 21 25 | - name: Build Project 26 | run: ./gradlew build 27 | - name: Archive Artifacts (fabric) 28 | uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 29 | if: success() 30 | with: 31 | name: BedrockSkinUtility 32 | path: build/libs/BedrockSkinUtility-*.jar 33 | if-no-files-found: error -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 Camotoy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/pluginmessage/data/SkinData.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.pluginmessage.data; 2 | 3 | import net.camotoy.bedrockskinutility.client.pluginmessage.BedrockMessageHandler; 4 | import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; 5 | import net.minecraft.network.FriendlyByteBuf; 6 | import net.minecraft.network.codec.StreamDecoder; 7 | 8 | import java.util.UUID; 9 | 10 | public record SkinData(UUID playerUuid, int chunkPosition, int available, byte[] skinData) implements BedrockData { 11 | public static final StreamDecoder STREAM_DECODER = buf -> { 12 | UUID playerUuid = buf.readUUID(); 13 | int chunkPosition = buf.readInt(); 14 | int available = buf.readableBytes(); 15 | byte[] skinData = new byte[available]; 16 | buf.readBytes(skinData); 17 | return new SkinData(playerUuid, chunkPosition, available, skinData); 18 | }; 19 | 20 | @Override 21 | public void handle(ClientPlayNetworking.Context context, BedrockMessageHandler handler) { 22 | handler.handle(this, context); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/SkinManager.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import com.google.common.cache.Cache; 4 | import com.google.common.cache.CacheBuilder; 5 | 6 | import java.util.Map; 7 | import java.util.UUID; 8 | import java.util.concurrent.ConcurrentHashMap; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | public class SkinManager { 12 | private static SkinManager instance; 13 | 14 | /** 15 | * If a player cannot be found, then stuff the UUID in here until they spawn. 16 | */ 17 | private final Cache cachedPlayers = CacheBuilder.newBuilder() 18 | .expireAfterAccess(15, TimeUnit.SECONDS) 19 | .build(); 20 | 21 | private final Map skinInfo = new ConcurrentHashMap<>(); 22 | 23 | public SkinManager() { 24 | instance = this; 25 | } 26 | 27 | public Cache getCachedPlayers() { 28 | return cachedPlayers; 29 | } 30 | 31 | public Map getSkinInfo() { 32 | return skinInfo; 33 | } 34 | 35 | public static SkinManager getInstance() { 36 | return instance; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/pluginmessage/data/BedrockData.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.pluginmessage.data; 2 | 3 | import net.camotoy.bedrockskinutility.client.pluginmessage.BedrockMessageHandler; 4 | import net.camotoy.bedrockskinutility.client.pluginmessage.GeyserSkinManagerListener; 5 | import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; 6 | import net.minecraft.network.FriendlyByteBuf; 7 | import net.minecraft.network.protocol.common.custom.CustomPacketPayload; 8 | 9 | import java.nio.charset.StandardCharsets; 10 | 11 | public interface BedrockData extends CustomPacketPayload { 12 | 13 | /** 14 | * Read a string in a platform-friendly way - Mojang's way uses VarInts which aren't easily implemented on our end. 15 | */ 16 | static String readString(FriendlyByteBuf buf) { 17 | int length = buf.readInt(); 18 | String result = buf.toString(buf.readerIndex(), length, StandardCharsets.UTF_8); 19 | buf.readerIndex(buf.readerIndex() + length); 20 | return result; 21 | } 22 | 23 | void handle(ClientPlayNetworking.Context context, BedrockMessageHandler handler); 24 | 25 | @Override 26 | default Type type() { 27 | return GeyserSkinManagerListener.TYPE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/BedrockSkinUtilityClient.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import net.camotoy.bedrockskinutility.client.pluginmessage.BedrockMessageHandler; 4 | import net.camotoy.bedrockskinutility.client.pluginmessage.GeyserSkinManagerListener; 5 | import net.fabricmc.api.ClientModInitializer; 6 | import net.fabricmc.api.EnvType; 7 | import net.fabricmc.api.Environment; 8 | import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; 9 | import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; 10 | import org.apache.logging.log4j.LogManager; 11 | import org.apache.logging.log4j.Logger; 12 | 13 | @Environment(EnvType.CLIENT) 14 | public class BedrockSkinUtilityClient implements ClientModInitializer { 15 | private final Logger logger = LogManager.getLogger("BedrockSkinUtility"); 16 | 17 | @Override 18 | public void onInitializeClient() { 19 | logger.info("Hello from BedrockClientSkinUtility!"); 20 | 21 | var handler = new BedrockMessageHandler(logger, new SkinManager()); 22 | PayloadTypeRegistry.playS2C().register(GeyserSkinManagerListener.TYPE, GeyserSkinManagerListener.STREAM_CODEC); 23 | ClientPlayNetworking.registerGlobalReceiver(GeyserSkinManagerListener.TYPE, (payload, context) -> payload.handle(context, handler)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/CapeFeatureRendererMixin.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.minecraft.client.renderer.RenderType; 4 | import net.minecraft.client.renderer.entity.layers.CapeLayer; 5 | import net.minecraft.resources.ResourceLocation; 6 | import org.spongepowered.asm.mixin.Mixin; 7 | import org.spongepowered.asm.mixin.injection.At; 8 | import org.spongepowered.asm.mixin.injection.Redirect; 9 | 10 | @Mixin(CapeLayer.class) 11 | public class CapeFeatureRendererMixin { 12 | @Redirect( 13 | method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;ILnet/minecraft/client/player/AbstractClientPlayer;FFFFFF)V", 14 | at = @At(value = "INVOKE", 15 | target = "Lnet/minecraft/client/renderer/RenderType;entitySolid(Lnet/minecraft/resources/ResourceLocation;)Lnet/minecraft/client/renderer/RenderType;"), 16 | require = 0 // Fail safely if other mods overwrite this 17 | ) 18 | public RenderType solidToTranslucent(ResourceLocation texture) { 19 | if (texture.getNamespace().equals("geyserskinmanager")) { 20 | // Capes can be translucent in Bedrock 21 | return RenderType.entityTranslucent(texture, true); 22 | } 23 | return RenderType.entitySolid(texture); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/pluginmessage/data/CapeData.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.pluginmessage.data; 2 | 3 | import net.camotoy.bedrockskinutility.client.pluginmessage.BedrockMessageHandler; 4 | import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; 5 | import net.minecraft.network.FriendlyByteBuf; 6 | import net.minecraft.network.codec.StreamDecoder; 7 | import net.minecraft.resources.ResourceLocation; 8 | 9 | import java.util.UUID; 10 | 11 | public record CapeData(UUID playerUuid, int width, int height, ResourceLocation identifier, byte[] capeData) implements BedrockData { 12 | public static final StreamDecoder STREAM_DECODER = buf -> { 13 | int version = buf.readInt(); 14 | if (version != 1) { 15 | throw new RuntimeException("Could not load cape data! Is the mod and plugin updated?"); 16 | } 17 | 18 | UUID playerUuid = new UUID(buf.readLong(), buf.readLong()); 19 | int width = buf.readInt(); 20 | int height = buf.readInt(); 21 | 22 | String capeId = BedrockData.readString(buf); 23 | ResourceLocation identifier = ResourceLocation.fromNamespaceAndPath("geyserskinmanager", capeId); 24 | 25 | byte[] capeData = new byte[buf.readInt()]; 26 | buf.readBytes(capeData); 27 | return new CapeData(playerUuid, width, height, identifier, capeData); 28 | }; 29 | 30 | @Override 31 | public void handle(ClientPlayNetworking.Context context, BedrockMessageHandler handler) { 32 | handler.handle(this, context); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/EntityRendererDispatcherMixin.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.camotoy.bedrockskinutility.client.interfaces.BedrockPlayerInfo; 4 | import net.minecraft.client.multiplayer.PlayerInfo; 5 | import net.minecraft.client.renderer.entity.EntityRenderDispatcher; 6 | import net.minecraft.client.renderer.entity.EntityRenderer; 7 | import net.minecraft.client.renderer.entity.player.PlayerRenderer; 8 | import net.minecraft.world.entity.Entity; 9 | import org.spongepowered.asm.mixin.Mixin; 10 | import org.spongepowered.asm.mixin.injection.At; 11 | import org.spongepowered.asm.mixin.injection.Inject; 12 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; 13 | 14 | @Mixin(EntityRenderDispatcher.class) 15 | public abstract class EntityRendererDispatcherMixin { 16 | 17 | @Inject( 18 | method = "getRenderer", 19 | at = @At(value = "INVOKE", 20 | target = "Lnet/minecraft/client/player/AbstractClientPlayer;getSkin()Lnet/minecraft/client/resources/PlayerSkin;"), 21 | cancellable = true 22 | ) 23 | public void getRenderer(Entity entity, CallbackInfoReturnable> cir) { 24 | PlayerInfo playerListEntry = ((BedrockAbstractClientPlayerEntity) entity).bedrockskinutility$getPlayerListEntry(); 25 | if (playerListEntry != null) { 26 | PlayerRenderer renderer = ((BedrockPlayerInfo) playerListEntry).bedrockskinutility$getModel(); 27 | if (renderer != null) { 28 | cir.setReturnValue(renderer); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/PlayerSkinBuilder.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import net.camotoy.bedrockskinutility.client.interfaces.BedrockPlayerSkin; 4 | import net.minecraft.client.resources.PlayerSkin; 5 | import net.minecraft.resources.ResourceLocation; 6 | 7 | public final class PlayerSkinBuilder { 8 | public ResourceLocation texture; 9 | public String textureUrl; 10 | public ResourceLocation capeTexture; 11 | public ResourceLocation elytraTexture; 12 | public PlayerSkin.Model model; 13 | public boolean secure; 14 | public boolean bedrockSkin; 15 | public boolean bedrockCape; 16 | 17 | public PlayerSkinBuilder(final PlayerSkin base) { 18 | this.texture = base.texture(); 19 | this.textureUrl = base.textureUrl(); 20 | this.capeTexture = base.capeTexture(); 21 | this.elytraTexture = base.elytraTexture(); 22 | this.model = base.model(); 23 | this.secure = base.secure(); 24 | this.bedrockSkin = ((BedrockPlayerSkin) (Object) base).bedrockskinutility$bedrockSkin(); 25 | this.bedrockCape = ((BedrockPlayerSkin) (Object) base).bedrockskinutility$bedrockCape(); 26 | } 27 | 28 | public PlayerSkin build() { 29 | final PlayerSkin playerSkin = new PlayerSkin( 30 | texture, 31 | textureUrl, 32 | capeTexture, 33 | elytraTexture, 34 | model, 35 | secure 36 | ); 37 | ((BedrockPlayerSkin) (Object) playerSkin).bedrockskinutility$bedrockSkin(bedrockSkin); 38 | ((BedrockPlayerSkin) (Object) playerSkin).bedrockskinutility$bedrockCape(bedrockCape); 39 | return playerSkin; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/pluginmessage/GeyserSkinManagerListener.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.pluginmessage; 2 | 3 | import net.camotoy.bedrockskinutility.client.BedrockSkinPluginMessageType; 4 | import net.camotoy.bedrockskinutility.client.pluginmessage.data.BaseSkinInfo; 5 | import net.camotoy.bedrockskinutility.client.pluginmessage.data.BedrockData; 6 | import net.camotoy.bedrockskinutility.client.pluginmessage.data.CapeData; 7 | import net.camotoy.bedrockskinutility.client.pluginmessage.data.SkinData; 8 | import net.minecraft.network.FriendlyByteBuf; 9 | import net.minecraft.network.codec.StreamCodec; 10 | import net.minecraft.network.protocol.common.custom.CustomPacketPayload; 11 | import net.minecraft.resources.ResourceLocation; 12 | 13 | public final class GeyserSkinManagerListener { 14 | public static final CustomPacketPayload.Type TYPE = 15 | new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath("bedrockskin", "data")); 16 | public static final StreamCodec STREAM_CODEC = StreamCodec.of(null, buf -> { 17 | int type = buf.readInt(); 18 | BedrockSkinPluginMessageType[] values = BedrockSkinPluginMessageType.values(); 19 | if (values.length < type) { 20 | throw new RuntimeException("Unknown plugin message type received! Is the mod and plugin updated? Type: " + type); 21 | } 22 | BedrockSkinPluginMessageType pluginMessageType = values[type]; 23 | return switch (pluginMessageType) { 24 | case CAPE -> CapeData.STREAM_DECODER.decode(buf); 25 | case SKIN_INFORMATION -> BaseSkinInfo.STREAM_DECODER.decode(buf); 26 | case SKIN_DATA -> SkinData.STREAM_DECODER.decode(buf); 27 | }; 28 | }); 29 | 30 | private GeyserSkinManagerListener() { 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/MixinConfigPlugin.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import net.fabricmc.loader.api.FabricLoader; 4 | import org.objectweb.asm.tree.ClassNode; 5 | import org.slf4j.LoggerFactory; 6 | import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; 7 | import org.spongepowered.asm.mixin.extensibility.IMixinInfo; 8 | 9 | import java.util.List; 10 | import java.util.Set; 11 | 12 | public class MixinConfigPlugin implements IMixinConfigPlugin { 13 | 14 | @Override 15 | public void onLoad(String mixinPackage) { 16 | 17 | } 18 | 19 | @Override 20 | public String getRefMapperConfig() { 21 | return null; 22 | } 23 | 24 | @Override 25 | public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { 26 | if (mixinClassName.equals("net.camotoy.bedrockskinutility.client.mixin.CapeFeatureRendererMixin")) { 27 | boolean capes = FabricLoader.getInstance().getModContainer("capes").isPresent() 28 | || FabricLoader.getInstance().getModContainer("cosmetica").isPresent(); 29 | if (capes) { 30 | // these mods have Mixins that just set all capes to transparent, so we don't need this Mixin 31 | LoggerFactory.getLogger(MixinConfigPlugin.class).info("Disabling transparent cape mixin in BedrockSkinUtility as another cape-related mod is also installed."); 32 | return false; 33 | } 34 | } 35 | return true; 36 | } 37 | 38 | @Override 39 | public void acceptTargets(Set myTargets, Set otherTargets) { 40 | 41 | } 42 | 43 | @Override 44 | public List getMixins() { 45 | return null; 46 | } 47 | 48 | @Override 49 | public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { 50 | } 51 | 52 | @Override 53 | public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/pluginmessage/data/BaseSkinInfo.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.pluginmessage.data; 2 | 3 | import com.google.gson.JsonObject; 4 | import com.google.gson.JsonParser; 5 | import net.camotoy.bedrockskinutility.client.pluginmessage.BedrockMessageHandler; 6 | import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; 7 | import net.minecraft.network.FriendlyByteBuf; 8 | import net.minecraft.network.codec.StreamDecoder; 9 | 10 | import java.util.UUID; 11 | 12 | public record BaseSkinInfo(UUID playerUuid, int skinWidth, int skinHeight, String geometry, JsonObject jsonGeometry, 13 | JsonObject jsonGeometryName, int chunkCount) implements BedrockData { 14 | public static final StreamDecoder STREAM_DECODER = buf -> { 15 | int version = buf.readInt(); 16 | if (version != 1) { // Version 2 is probably going to be reserved for persona skins 17 | throw new RuntimeException("Could not load skin info! Is the mod and plugin updated?"); 18 | } 19 | 20 | UUID playerUuid = new UUID(buf.readLong(), buf.readLong()); 21 | 22 | int skinWidth = buf.readInt(); 23 | int skinHeight = buf.readInt(); 24 | 25 | String geometry = null; 26 | JsonObject jsonGeometry = null; 27 | JsonObject jsonGeometryName = null; 28 | 29 | if (buf.readBoolean()) { // is geometry present 30 | try { 31 | geometry = BedrockData.readString(buf); 32 | jsonGeometry = JsonParser.parseString(geometry).getAsJsonObject(); 33 | jsonGeometryName = JsonParser.parseString(BedrockData.readString(buf)).getAsJsonObject(); 34 | } catch (Exception e) { 35 | throw new RuntimeException("Error while trying to decode geometry!", e); 36 | } 37 | } 38 | 39 | int chunkCount = buf.readInt(); 40 | 41 | return new BaseSkinInfo(playerUuid, skinWidth, skinHeight, geometry, jsonGeometry, jsonGeometryName, chunkCount); 42 | }; 43 | 44 | @Override 45 | public void handle(ClientPlayNetworking.Context context, BedrockMessageHandler handler) { 46 | handler.handle(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/SkinInfo.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import com.google.gson.JsonObject; 4 | import org.jetbrains.annotations.Nullable; 5 | 6 | public class SkinInfo { 7 | private final int width; 8 | private final int height; 9 | private final JsonObject geometry; 10 | private final JsonObject geometryName; 11 | private final byte[][] skinData; 12 | 13 | public SkinInfo(int width, int height, JsonObject geometry, JsonObject geometryName, int chunkCount) { 14 | this.width = width; 15 | this.height = height; 16 | this.geometry = geometry; 17 | this.geometryName = geometryName; 18 | this.skinData = new byte[chunkCount][]; 19 | } 20 | 21 | public int getWidth() { 22 | return width; 23 | } 24 | 25 | public int getHeight() { 26 | return height; 27 | } 28 | 29 | @Nullable 30 | public JsonObject getGeometry() { 31 | return geometry; 32 | } 33 | 34 | @Nullable 35 | public JsonObject getGeometryName() { 36 | return geometryName; 37 | } 38 | 39 | /** 40 | * Should the skin data be sent to us through multiple plugin messages, assemble it. 41 | */ 42 | public byte[] getData() { 43 | if (skinData.length == 1) { 44 | // No concatenation needed 45 | return skinData[0]; 46 | } 47 | 48 | int totalLength = 0; 49 | for (byte[] data : skinData) { 50 | totalLength += data.length; 51 | } 52 | byte[] totalData = new byte[totalLength]; 53 | int currentIndex = 0; 54 | for (byte[] currentData : skinData) { 55 | // Copy all arrays to one array 56 | System.arraycopy(currentData, 0, totalData, currentIndex, currentData.length); 57 | currentIndex += currentData.length; 58 | } 59 | return totalData; 60 | } 61 | 62 | public void setData(byte[] data, int chunk) { 63 | this.skinData[chunk] = data; 64 | } 65 | 66 | public boolean isComplete() { 67 | for (byte[] data : skinData) { 68 | if (data == null) { 69 | return false; 70 | } 71 | } 72 | return true; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # User-specific stuff 2 | .idea/ 3 | .vscode/ 4 | 5 | *.iml 6 | *.ipr 7 | *.iws 8 | 9 | # IntelliJ 10 | out/ 11 | # mpeltonen/sbt-idea plugin 12 | .idea_modules/ 13 | 14 | # JIRA plugin 15 | atlassian-ide-plugin.xml 16 | 17 | # Compiled class file 18 | *.class 19 | 20 | # Log file 21 | *.log 22 | 23 | # BlueJ files 24 | *.ctxt 25 | 26 | # Package Files # 27 | *.jar 28 | *.war 29 | *.nar 30 | *.ear 31 | *.zip 32 | *.tar.gz 33 | *.rar 34 | 35 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 36 | hs_err_pid* 37 | 38 | *~ 39 | 40 | # temporary files which can be created if a process still has a handle open of a deleted file 41 | .fuse_hidden* 42 | 43 | # KDE directory preferences 44 | .directory 45 | 46 | # Linux trash folder which might appear on any partition or disk 47 | .Trash-* 48 | 49 | # .nfs files are created when an open file is removed but is still being accessed 50 | .nfs* 51 | 52 | # General 53 | .DS_Store 54 | .AppleDouble 55 | .LSOverride 56 | 57 | # Icon must end with two \r 58 | Icon 59 | 60 | # Thumbnails 61 | ._* 62 | 63 | # Files that might appear in the root of a volume 64 | .DocumentRevisions-V100 65 | .fseventsd 66 | .Spotlight-V100 67 | .TemporaryItems 68 | .Trashes 69 | .VolumeIcon.icns 70 | .com.apple.timemachine.donotpresent 71 | 72 | # Directories potentially created on remote AFP share 73 | .AppleDB 74 | .AppleDesktop 75 | Network Trash Folder 76 | Temporary Items 77 | .apdisk 78 | 79 | # Windows thumbnail cache files 80 | Thumbs.db 81 | Thumbs.db:encryptable 82 | ehthumbs.db 83 | ehthumbs_vista.db 84 | 85 | # Dump file 86 | *.stackdump 87 | 88 | # Folder config file 89 | [Dd]esktop.ini 90 | 91 | # Recycle Bin used on file shares 92 | $RECYCLE.BIN/ 93 | 94 | # Windows Installer files 95 | *.cab 96 | *.msi 97 | *.msix 98 | *.msm 99 | *.msp 100 | 101 | # Windows shortcuts 102 | *.lnk 103 | 104 | .gradle 105 | build/ 106 | 107 | # Ignore Gradle GUI config 108 | gradle-app.setting 109 | 110 | # Cache of project 111 | .gradletasknamecache 112 | 113 | **/build/ 114 | 115 | # Common working directory 116 | run/ 117 | remappedSrc/ 118 | bin/ 119 | 120 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 121 | !gradle-wrapper.jar 122 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/SkinUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2021 GeyserMC. http://geysermc.org 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | * 22 | * @author GeyserMC 23 | * @link https://github.com/GeyserMC/Floodgate 24 | */ 25 | 26 | package net.camotoy.bedrockskinutility.client; 27 | 28 | import java.awt.image.BufferedImage; 29 | 30 | public class SkinUtils { 31 | /** 32 | * Get the ARGB int for a given index in some image data 33 | * 34 | * @param index Index to get 35 | * @param data Image data to find in 36 | * @return An int representing ARGB 37 | */ 38 | private static int getARGB(int index, byte[] data) { 39 | return (data[index + 3] & 0xFF) << 24 | (data[index] & 0xFF) << 16 | 40 | (data[index + 1] & 0xFF) << 8 | (data[index + 2] & 0xFF); 41 | } 42 | 43 | /** 44 | * Convert a byte[] to a BufferedImage 45 | * 46 | * @param imageData The byte[] to convert 47 | * @param width The width of the target image 48 | * @param height The height of the target image 49 | * @return The converted BufferedImage 50 | */ 51 | public static BufferedImage toBufferedImage(byte[] imageData, int width, int height) { 52 | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 53 | for (int y = 0; y < height; y++) { 54 | for (int x = 0; x < width; x++) { 55 | image.setRGB(x, y, getARGB((y * width + x) * 4, imageData)); 56 | } 57 | } 58 | return image; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/mixin/ClientPlayNetworkHandlerMixin.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.mixin; 2 | 3 | import net.camotoy.bedrockskinutility.client.BedrockCachedProperties; 4 | import net.camotoy.bedrockskinutility.client.PlayerSkinBuilder; 5 | import net.camotoy.bedrockskinutility.client.SkinManager; 6 | import net.camotoy.bedrockskinutility.client.interfaces.BedrockPlayerInfo; 7 | import net.camotoy.bedrockskinutility.client.interfaces.BedrockPlayerSkin; 8 | import net.fabricmc.api.EnvType; 9 | import net.fabricmc.api.Environment; 10 | import net.minecraft.client.multiplayer.ClientPacketListener; 11 | import net.minecraft.client.multiplayer.PlayerInfo; 12 | import net.minecraft.client.resources.PlayerSkin; 13 | import net.minecraft.network.protocol.game.ClientGamePacketListener; 14 | import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket; 15 | import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket; 16 | import net.minecraft.resources.ResourceLocation; 17 | import org.spongepowered.asm.mixin.Final; 18 | import org.spongepowered.asm.mixin.Mixin; 19 | import org.spongepowered.asm.mixin.Shadow; 20 | import org.spongepowered.asm.mixin.injection.At; 21 | import org.spongepowered.asm.mixin.injection.Inject; 22 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; 23 | 24 | import java.util.Map; 25 | import java.util.UUID; 26 | 27 | @Environment(EnvType.CLIENT) 28 | @Mixin(ClientPacketListener.class) 29 | public abstract class ClientPlayNetworkHandlerMixin implements ClientGamePacketListener { 30 | 31 | @Shadow @Final private Map playerInfoMap; 32 | 33 | /** 34 | * @reason check and see if we already have this player's information 35 | */ 36 | @Inject(method = "handlePlayerInfoUpdate", at = @At("RETURN")) 37 | public void bedrockskinutility$onPlayerAdd(ClientboundPlayerInfoUpdatePacket packet, CallbackInfo ci) { 38 | if (packet.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) { 39 | for (ClientboundPlayerInfoUpdatePacket.Entry entry : packet.entries()) { 40 | BedrockCachedProperties properties = SkinManager.getInstance().getCachedPlayers().getIfPresent(entry.profileId()); 41 | if (properties != null) { 42 | final PlayerInfo playerInfo = this.playerInfoMap.get(entry.profileId()); 43 | final PlayerSkinBuilder builder = new PlayerSkinBuilder(playerInfo.getSkin()); 44 | if (properties.skin != null) { 45 | builder.texture = properties.skin; 46 | builder.bedrockSkin = true; 47 | ((BedrockPlayerInfo) playerInfo).bedrockskinutility$setModel(properties.model); 48 | } 49 | if (properties.cape != null && builder.capeTexture == null) { 50 | // Do not overwrite existing capes 51 | builder.capeTexture = properties.cape; 52 | builder.bedrockCape = true; 53 | } 54 | final PlayerSkin playerSkin = builder.build(); 55 | ((PlayerSkinFieldAccessor) playerInfo).setPlayerSkin(() -> playerSkin); 56 | } 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * @reason sometimes the player will be removed and then instantly re-added (skin refresh). Let's check for that. 63 | */ 64 | @Inject(method = "handlePlayerInfoRemove", at = @At("HEAD")) 65 | public void bedrockskinutility$onPlayerRemove(ClientboundPlayerInfoRemovePacket packet, CallbackInfo ci) { 66 | for (UUID uuid : packet.profileIds()) { 67 | PlayerInfo playerListEntry = this.playerInfoMap.get(uuid); 68 | if (playerListEntry != null) { 69 | final PlayerSkin playerSkin = playerListEntry.getSkin(); 70 | BedrockPlayerSkin bedrockSkin = (BedrockPlayerSkin) (Object) playerSkin; 71 | ResourceLocation skinIdentifier = bedrockSkin.bedrockskinutility$bedrockSkin() ? playerSkin.texture() : null; 72 | ResourceLocation capeIdentifier = bedrockSkin.bedrockskinutility$bedrockCape() ? playerSkin.capeTexture() : null; 73 | if (skinIdentifier != null || capeIdentifier != null) { 74 | BedrockCachedProperties properties = new BedrockCachedProperties(); 75 | properties.skin = skinIdentifier; 76 | properties.model = ((BedrockPlayerInfo) playerListEntry).bedrockskinutility$getModel(); 77 | properties.cape = capeIdentifier; 78 | SkinManager.getInstance().getCachedPlayers().put(uuid, properties); 79 | } 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/pluginmessage/BedrockMessageHandler.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client.pluginmessage; 2 | 3 | import com.mojang.blaze3d.platform.NativeImage; 4 | import net.camotoy.bedrockskinutility.client.*; 5 | import net.camotoy.bedrockskinutility.client.interfaces.BedrockPlayerInfo; 6 | import net.camotoy.bedrockskinutility.client.mixin.PlayerEntityRendererChangeModel; 7 | import net.camotoy.bedrockskinutility.client.mixin.PlayerSkinFieldAccessor; 8 | import net.camotoy.bedrockskinutility.client.pluginmessage.data.BaseSkinInfo; 9 | import net.camotoy.bedrockskinutility.client.pluginmessage.data.CapeData; 10 | import net.camotoy.bedrockskinutility.client.pluginmessage.data.SkinData; 11 | import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; 12 | import net.minecraft.client.Minecraft; 13 | import net.minecraft.client.multiplayer.ClientPacketListener; 14 | import net.minecraft.client.multiplayer.PlayerInfo; 15 | import net.minecraft.client.player.AbstractClientPlayer; 16 | import net.minecraft.client.renderer.entity.EntityRendererProvider; 17 | import net.minecraft.client.renderer.entity.player.PlayerRenderer; 18 | import net.minecraft.client.renderer.texture.DynamicTexture; 19 | import net.minecraft.client.resources.PlayerSkin; 20 | import net.minecraft.resources.ResourceLocation; 21 | import net.minecraft.util.FastColor; 22 | import org.apache.logging.log4j.Logger; 23 | 24 | import java.awt.image.BufferedImage; 25 | import java.util.UUID; 26 | 27 | public final class BedrockMessageHandler { 28 | private final Logger logger; 29 | private final SkinManager skinManager; 30 | 31 | public BedrockMessageHandler(Logger logger, SkinManager skinManager) { 32 | this.logger = logger; 33 | this.skinManager = skinManager; 34 | } 35 | 36 | public void handle(CapeData payload, ClientPlayNetworking.Context context) { 37 | NativeImage capeImage = toNativeImage(payload.capeData(), payload.width(), payload.height()); 38 | 39 | context.client().submit(() -> { 40 | // As of 1.17.1, identical identifiers do not result in multiple objects of the same type being registered 41 | context.client().getTextureManager().register(payload.identifier(), new DynamicTexture(capeImage)); 42 | applyCapeTexture(context.client().getConnection(), payload.playerUuid(), payload.identifier()); 43 | }); 44 | } 45 | 46 | /** 47 | * Should be run from the main thread 48 | */ 49 | private void applyCapeTexture(ClientPacketListener handler, UUID playerUuid, ResourceLocation identifier) { 50 | PlayerInfo entry = handler.getPlayerInfo(playerUuid); 51 | if (entry == null) { 52 | // Save in the cache for later 53 | BedrockCachedProperties properties = skinManager.getCachedPlayers().getIfPresent(playerUuid); 54 | if (properties == null) { 55 | properties = new BedrockCachedProperties(); 56 | skinManager.getCachedPlayers().put(playerUuid, properties); 57 | } 58 | properties.cape = identifier; 59 | } else { 60 | final PlayerSkinBuilder builder = new PlayerSkinBuilder(entry.getSkin()); 61 | builder.capeTexture = identifier; 62 | builder.bedrockCape = true; 63 | final PlayerSkin playerSkin = builder.build(); 64 | ((PlayerSkinFieldAccessor) entry).setPlayerSkin(() -> playerSkin); 65 | } 66 | } 67 | 68 | public void handle(BaseSkinInfo payload) { 69 | skinManager.getSkinInfo().put(payload.playerUuid(), new SkinInfo(payload.skinWidth(), payload.skinHeight(), payload.jsonGeometry(), 70 | payload.jsonGeometryName(), payload.chunkCount())); 71 | } 72 | 73 | public void handle(SkinData payload, ClientPlayNetworking.Context context) { 74 | SkinInfo info = skinManager.getSkinInfo().get(payload.playerUuid()); 75 | if (info == null) { 76 | this.logger.error("Skin info was null!!!"); 77 | return; 78 | } 79 | info.setData(payload.skinData(), payload.chunkPosition()); 80 | this.logger.info("Skin chunk {} received for {}", payload.chunkPosition(), payload.playerUuid()); 81 | 82 | if (info.isComplete()) { 83 | // All skin data has been received 84 | skinManager.getSkinInfo().remove(payload.playerUuid()); 85 | } else { 86 | return; 87 | } 88 | 89 | NativeImage skinImage = toNativeImage(info.getData(), info.getWidth(), info.getHeight()); 90 | PlayerRenderer renderer; 91 | boolean setModel = info.getGeometry() != null; 92 | 93 | Minecraft client = context.client(); 94 | if (setModel) { 95 | // Convert Bedrock JSON geometry into a class format that Java understands 96 | BedrockPlayerEntityModel model = GeometryUtil.bedrockGeoToJava(info); 97 | if (model != null) { 98 | EntityRendererProvider.Context entityContext = new EntityRendererProvider.Context(client.getEntityRenderDispatcher(), 99 | client.getItemRenderer(), client.getBlockRenderer(), client.getEntityRenderDispatcher().getItemInHandRenderer(), 100 | client.getResourceManager(), client.getEntityModels(), client.font); 101 | renderer = new PlayerRenderer(entityContext, false); 102 | ((PlayerEntityRendererChangeModel) renderer).bedrockskinutility$setModel(model); 103 | } else { 104 | renderer = null; 105 | } 106 | } else { 107 | renderer = null; 108 | } 109 | 110 | ResourceLocation identifier = ResourceLocation.fromNamespaceAndPath("geyserskinmanager", payload.playerUuid().toString()); 111 | client.submit(() -> { 112 | client.getTextureManager().register(identifier, new DynamicTexture(skinImage)); 113 | applySkinTexture(client.getConnection(), payload.playerUuid(), identifier, renderer); 114 | }); 115 | } 116 | 117 | /** 118 | * Should be run from the main thread 119 | */ 120 | private void applySkinTexture(ClientPacketListener handler, UUID playerUuid, ResourceLocation identifier, PlayerRenderer renderer) { 121 | PlayerInfo entry = handler.getPlayerInfo(playerUuid); 122 | if (entry == null) { 123 | // Save in the cache for later 124 | BedrockCachedProperties properties = skinManager.getCachedPlayers().getIfPresent(playerUuid); 125 | if (properties == null) { 126 | properties = new BedrockCachedProperties(); 127 | skinManager.getCachedPlayers().put(playerUuid, properties); 128 | } 129 | properties.model = renderer; 130 | properties.skin = identifier; 131 | } else { 132 | try { 133 | ((BedrockPlayerInfo) entry).bedrockskinutility$setModel(renderer); 134 | 135 | final PlayerSkinBuilder builder = new PlayerSkinBuilder(entry.getSkin()); 136 | builder.texture = identifier; 137 | builder.bedrockSkin = true; 138 | final PlayerSkin playerSkin = builder.build(); 139 | ((PlayerSkinFieldAccessor) entry).setPlayerSkin(() -> playerSkin); 140 | } catch (Throwable t) { 141 | t.printStackTrace(); 142 | } 143 | } 144 | } 145 | 146 | private NativeImage toNativeImage(byte[] data, int width, int height) { 147 | BufferedImage bufferedImage = SkinUtils.toBufferedImage(data, width, height); 148 | 149 | NativeImage nativeImage = new NativeImage(width, height, true); 150 | for (int currentWidth = 0; currentWidth < width; currentWidth++) { 151 | for (int currentHeight = 0; currentHeight < height; currentHeight++) { 152 | int rgba = bufferedImage.getRGB(currentWidth, currentHeight); 153 | nativeImage.setPixelRGBA(currentWidth, currentHeight, FastColor.ARGB32.color( 154 | (rgba >> 24) & 0xFF, rgba & 0xFF, (rgba >> 8) & 0xFF, (rgba >> 16) & 0xFF)); 155 | } 156 | } 157 | return nativeImage; 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 87 | APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit 88 | 89 | # Use the maximum available, or set MAX_FD != -1 to use that value. 90 | MAX_FD=maximum 91 | 92 | warn () { 93 | echo "$*" 94 | } >&2 95 | 96 | die () { 97 | echo 98 | echo "$*" 99 | echo 100 | exit 1 101 | } >&2 102 | 103 | # OS specific support (must be 'true' or 'false'). 104 | cygwin=false 105 | msys=false 106 | darwin=false 107 | nonstop=false 108 | case "$( uname )" in #( 109 | CYGWIN* ) cygwin=true ;; #( 110 | Darwin* ) darwin=true ;; #( 111 | MSYS* | MINGW* ) msys=true ;; #( 112 | NONSTOP* ) nonstop=true ;; 113 | esac 114 | 115 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 116 | 117 | 118 | # Determine the Java command to use to start the JVM. 119 | if [ -n "$JAVA_HOME" ] ; then 120 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 121 | # IBM's JDK on AIX uses strange locations for the executables 122 | JAVACMD=$JAVA_HOME/jre/sh/java 123 | else 124 | JAVACMD=$JAVA_HOME/bin/java 125 | fi 126 | if [ ! -x "$JAVACMD" ] ; then 127 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 128 | 129 | Please set the JAVA_HOME variable in your environment to match the 130 | location of your Java installation." 131 | fi 132 | else 133 | JAVACMD=java 134 | if ! command -v java >/dev/null 2>&1 135 | then 136 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | fi 142 | 143 | # Increase the maximum file descriptors if we can. 144 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 145 | case $MAX_FD in #( 146 | max*) 147 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 148 | # shellcheck disable=SC2039,SC3045 149 | MAX_FD=$( ulimit -H -n ) || 150 | warn "Could not query maximum file descriptor limit" 151 | esac 152 | case $MAX_FD in #( 153 | '' | soft) :;; #( 154 | *) 155 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 156 | # shellcheck disable=SC2039,SC3045 157 | ulimit -n "$MAX_FD" || 158 | warn "Could not set maximum file descriptor limit to $MAX_FD" 159 | esac 160 | fi 161 | 162 | # Collect all arguments for the java command, stacking in reverse order: 163 | # * args from the command line 164 | # * the main class name 165 | # * -classpath 166 | # * -D...appname settings 167 | # * --module-path (only if needed) 168 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 169 | 170 | # For Cygwin or MSYS, switch paths to Windows format before running java 171 | if "$cygwin" || "$msys" ; then 172 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 173 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 174 | 175 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 176 | 177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 178 | for arg do 179 | if 180 | case $arg in #( 181 | -*) false ;; # don't mess with options #( 182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 183 | [ -e "$t" ] ;; #( 184 | *) false ;; 185 | esac 186 | then 187 | arg=$( cygpath --path --ignore --mixed "$arg" ) 188 | fi 189 | # Roll the args list around exactly as many times as the number of 190 | # args, so each arg winds up back in the position where it started, but 191 | # possibly modified. 192 | # 193 | # NB: a `for` loop captures its iteration list before it begins, so 194 | # changing the positional parameters here affects neither the number of 195 | # iterations, nor the values presented in `arg`. 196 | shift # remove old arg 197 | set -- "$@" "$arg" # push replacement arg 198 | done 199 | fi 200 | 201 | 202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 204 | 205 | # Collect all arguments for the java command: 206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 207 | # and any embedded shellness will be escaped. 208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 209 | # treated as '${Hostname}' itself on the command line. 210 | 211 | set -- \ 212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 213 | -classpath "$CLASSPATH" \ 214 | org.gradle.wrapper.GradleWrapperMain \ 215 | "$@" 216 | 217 | # Stop when "xargs" is not available. 218 | if ! command -v xargs >/dev/null 2>&1 219 | then 220 | die "xargs is not available" 221 | fi 222 | 223 | # Use "xargs" to parse quoted args. 224 | # 225 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 226 | # 227 | # In Bash we could simply go: 228 | # 229 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 230 | # set -- "${ARGS[@]}" "$@" 231 | # 232 | # but POSIX shell has neither arrays nor command substitution, so instead we 233 | # post-process each arg (as a line of input to sed) to backslash-escape any 234 | # character that might be a shell metacharacter, then use eval to reverse 235 | # that process (while maintaining the separation between arguments), and wrap 236 | # the whole thing up as a single "set" statement. 237 | # 238 | # This will of course break if any of these variables contains a newline or 239 | # an unmatched quote. 240 | # 241 | 242 | eval "set -- $( 243 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 244 | xargs -n1 | 245 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 246 | tr '\n' ' ' 247 | )" '"$@"' 248 | 249 | exec "$JAVACMD" "$@" 250 | -------------------------------------------------------------------------------- /src/main/java/net/camotoy/bedrockskinutility/client/GeometryUtil.java: -------------------------------------------------------------------------------- 1 | package net.camotoy.bedrockskinutility.client; 2 | 3 | import com.google.common.collect.Maps; 4 | import com.google.gson.JsonArray; 5 | import com.google.gson.JsonElement; 6 | import com.google.gson.JsonObject; 7 | import com.mojang.logging.LogUtils; 8 | import net.minecraft.client.model.HumanoidModel; 9 | import net.minecraft.client.model.geom.ModelPart; 10 | import net.minecraft.client.model.geom.PartPose; 11 | import net.minecraft.client.model.geom.builders.CubeDeformation; 12 | import net.minecraft.client.model.geom.builders.CubeListBuilder; 13 | import net.minecraft.client.player.AbstractClientPlayer; 14 | import net.minecraft.core.Direction; 15 | 16 | import java.util.*; 17 | 18 | public class GeometryUtil { 19 | private static final org.slf4j.Logger LOGGER = LogUtils.getLogger(); 20 | // Copied from CubeListBuilder 21 | private static final Set ALL_VISIBLE = EnumSet.allOf(Direction.class); 22 | 23 | public static BedrockPlayerEntityModel bedrockGeoToJava(SkinInfo info) { 24 | // There are some times when the skin image file is larger than the geometry UV points. 25 | // In this case, we need to scale UV calls 26 | // https://github.com/Camotoy/BedrockSkinUtility/issues/9 27 | int uvHeight = info.getHeight(); 28 | int uvWidth = info.getWidth(); 29 | 30 | // Construct a list of all bones we need to translate 31 | List bones = new ArrayList<>(); 32 | try { 33 | String geometryName = info.getGeometryName().getAsJsonObject("geometry").get("default").getAsString(); 34 | if (info.getGeometry().get("format_version").getAsString().equals("1.8.0")) { 35 | for (JsonElement node : info.getGeometry().getAsJsonObject(geometryName).getAsJsonArray("bones")) { 36 | bones.add(node.getAsJsonObject()); 37 | } 38 | } else { // Seen with format_version 1.12.0 39 | for (JsonElement node : info.getGeometry().getAsJsonArray("minecraft:geometry")) { 40 | JsonObject o = node.getAsJsonObject(); 41 | JsonObject description = o.get("description").getAsJsonObject(); 42 | if (!description.get("identifier").getAsString().equals(geometryName)) { 43 | continue; 44 | } else { 45 | uvHeight = description.get("texture_height").getAsInt(); 46 | uvWidth = description.get("texture_width").getAsInt(); 47 | } 48 | for (JsonElement subNode : o.get("bones").getAsJsonArray()) { 49 | bones.add(subNode.getAsJsonObject()); 50 | } 51 | } 52 | } 53 | } catch (Exception e) { 54 | LOGGER.error("Error while parsing geometry!"); 55 | e.printStackTrace(); 56 | return null; 57 | } 58 | 59 | Map stringToPart = new HashMap<>(); 60 | try { 61 | for (JsonObject bone : bones) { 62 | // Iterate through all bones 63 | String name = bone.get("name").getAsString(); 64 | 65 | JsonElement jsonParent = bone.get("parent"); 66 | JsonObject parentPart = null; 67 | String parent = null; 68 | if (jsonParent != null) { 69 | // Search through all bones to find the parent part 70 | parent = jsonParent.getAsString(); 71 | for (JsonObject otherBone : bones) { 72 | if (parent.equals(otherBone.get("name").getAsString())) { 73 | parentPart = otherBone; 74 | break; 75 | } 76 | } 77 | } 78 | 79 | List cuboids = new ArrayList<>(); 80 | JsonArray pivot = bone.getAsJsonArray("pivot"); 81 | float pivotX = pivot.get(0).getAsFloat(); 82 | float pivotY = pivot.get(1).getAsFloat(); 83 | float pivotZ = pivot.get(2).getAsFloat(); 84 | 85 | JsonArray cubes = bone.getAsJsonArray("cubes"); 86 | if (cubes != null) { 87 | for (JsonElement node : cubes) { 88 | JsonObject cube = node.getAsJsonObject(); 89 | JsonElement mirrorNode = cube.get("mirror"); // Can be null on the llama skins in the Wandering Trader pack 90 | boolean mirrored = mirrorNode != null && mirrorNode.getAsBoolean(); 91 | JsonArray origin = cube.getAsJsonArray("origin"); 92 | float originX = origin.get(0).getAsFloat(); 93 | float originY = origin.get(1).getAsFloat(); 94 | float originZ = origin.get(2).getAsFloat(); 95 | JsonArray size = cube.getAsJsonArray("size"); 96 | float sizeX = size.get(0).getAsFloat(); 97 | float sizeY = size.get(1).getAsFloat(); 98 | float sizeZ = size.get(2).getAsFloat(); 99 | JsonArray uv = cube.getAsJsonArray("uv"); 100 | JsonElement inflateNode = cube.get("inflate"); // Again, the llama skin 101 | float inflate = inflateNode != null ? inflateNode.getAsFloat() : 0f; 102 | // I didn't use the below, but it may be a helpful reference in the future 103 | // The Y needs to be inverted, for whatever reason 104 | // https://github.com/JannisX11/blockbench/blob/8529c0adee8565f8dac4b4583c3473b60679966d/js/transform.js#L148 105 | cuboids.add(new ModelPart.Cube((int) uv.get(0).getAsFloat(), (int) uv.get(1).getAsFloat(), 106 | (originX - pivotX), (-(originY + sizeY) + pivotY), (originZ - pivotZ), 107 | sizeX, sizeY, sizeZ, inflate, inflate, inflate, mirrored, uvHeight, uvWidth, ALL_VISIBLE)); 108 | } 109 | } 110 | 111 | Map children = new HashMap<>(); 112 | ModelPart part = new ModelPart(cuboids, children); 113 | if (parentPart != null) { 114 | // This appears to be a difference between Bedrock and Java - pivots are carried over for us 115 | JsonArray parentPivot = parentPart.getAsJsonArray("pivot"); 116 | part.setPos(pivotX - parentPivot.get(0).getAsFloat(), 117 | pivotY - parentPivot.get(1).getAsFloat(), 118 | pivotZ - parentPivot.get(2).getAsFloat()); 119 | } else { 120 | part.setPos(pivotX, pivotY, pivotZ); 121 | } 122 | 123 | switch (name) { // Also do this with the overlays? Those are final, though. 124 | case "head", "hat", "rightArm", "body", "leftArm", "leftLeg", "rightLeg" -> parent = "root"; 125 | } 126 | 127 | name = adjustFormatting(name); 128 | 129 | stringToPart.put(name, new PartInfo(adjustFormatting(parent), part, children)); 130 | } 131 | 132 | for (Map.Entry entry : stringToPart.entrySet()) { 133 | if (entry.getValue().parent != null) { 134 | PartInfo parentPart = stringToPart.get(entry.getValue().parent); 135 | if (parentPart != null) { 136 | parentPart.children.put(entry.getKey(), entry.getValue().part); 137 | } 138 | } 139 | } 140 | 141 | PartInfo root = stringToPart.get("root"); 142 | 143 | ensureAvailable(root.children, "ear"); 144 | root.children.computeIfAbsent("cloak", (string) -> // Required to allow a cape to render 145 | HumanoidModel.createMesh(CubeDeformation.NONE, 0.0F).getRoot().addOrReplaceChild(string, 146 | CubeListBuilder.create() 147 | .texOffs(0, 0) 148 | .addBox(-5.0F, 0.0F, -1.0F, 10.0F, 16.0F, 1.0F, CubeDeformation.NONE, 1.0F, 0.5F), 149 | PartPose.offset(0.0F, 0.0F, 0.0F)).bake(64, 64)); 150 | ensureAvailable(root.children, "left_sleeve"); 151 | ensureAvailable(root.children, "right_sleeve"); 152 | ensureAvailable(root.children, "left_pants"); 153 | ensureAvailable(root.children, "right_pants"); 154 | ensureAvailable(root.children, "jacket"); 155 | 156 | // Create base model 157 | return new BedrockPlayerEntityModel<>(root.part); 158 | } catch (Exception e) { 159 | LOGGER.error("Error while parsing geometry into model!", e); 160 | return null; 161 | } 162 | } 163 | 164 | private static String adjustFormatting(String name) { 165 | if (name == null) { 166 | return null; 167 | } 168 | 169 | return switch (name) { 170 | case "leftArm" -> "left_arm"; 171 | case "rightArm" -> "right_arm"; 172 | case "leftLeg" -> "left_leg"; 173 | case "rightLeg" -> "right_leg"; 174 | default -> name; 175 | }; 176 | } 177 | 178 | /** 179 | * Ensure a part is created, or else the geometry will not load in 1.17. 180 | */ 181 | private static void ensureAvailable(Map children, String name) { 182 | children.computeIfAbsent(name, (string) -> new ModelPart(Collections.emptyList(), Maps.newHashMap())); 183 | } 184 | 185 | private record PartInfo(String parent, ModelPart part, Map children) { 186 | } 187 | } 188 | --------------------------------------------------------------------------------