Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewriting skin fetch logic (for 1.17) #60

Open
wants to merge 2 commits into
base: 1.17.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ java.toolchain.languageVersion = JavaLanguageVersion.of(16)
println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getProperty('java.vm.version') + '(' + System.getProperty('java.vendor') + ') Arch: ' + System.getProperty('os.arch'))
minecraft {
mappings channel: "${mcp_channel}", version: "${mcp_mappings}"

accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

runs {
Expand Down Expand Up @@ -129,6 +129,15 @@ def reobfArtifact = artifacts.add('default', reobfFile) {
type 'jar'
builtBy 'reobfJar'
}


task deobfJar(type: Jar) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore this part. it just makes a jar that you can also use while making other mods. I used this to mess with forge to find the lighting bugs

build.dependsOn it
from sourceSets.main.output
classifier = 'deobf'
from file("LICENSE")
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
import com.mojang.math.Vector3f;
import com.shynieke.statues.client.model.PlayerStatueModel;
import com.shynieke.statues.entity.PlayerStatue;
import com.shynieke.statues.util.SkinUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.layers.CustomHeadLayer;
import net.minecraft.client.renderer.entity.layers.ElytraLayer;
import net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer;
import net.minecraft.client.renderer.entity.layers.ItemInHandLayer;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.resources.SkinManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;

import java.util.Map;

public class PlayerStatueRenderer extends LivingEntityRenderer<PlayerStatue, PlayerStatueModel> {
private final PlayerStatueModel playerModel;
private final PlayerStatueModel slimPlayerModel;
Expand Down Expand Up @@ -56,15 +55,8 @@ private ResourceLocation getSkin(GameProfile gameProfile) {
if (!gameProfile.isComplete()) {
return defaultTexture;
} else {
final Minecraft minecraft = Minecraft.getInstance();
SkinManager skinManager = minecraft.getSkinManager();
final Map<Type, MinecraftProfileTexture> loadSkinFromCache = skinManager.getInsecureSkinInformation(gameProfile); // returned map may or may not be typed
if (loadSkinFromCache.containsKey(MinecraftProfileTexture.Type.SKIN)) {
return skinManager.registerTexture(loadSkinFromCache.get(Type.SKIN), Type.SKIN);
}
else {
return DefaultPlayerSkin.getDefaultSkin(gameProfile.getId());
}
SkinUtil.SkinRenderData skinRenderData = SkinUtil.getSkinRenderData(gameProfile);
return skinRenderData.skinLocation;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.client.model.geom.EntityModelSet;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.ItemTransforms.TransformType;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -123,9 +124,9 @@ public void renderPlayerItem(ItemStack stack, PoseStack poseStack, MultiBufferSo
}

public void renderItem(GameProfile gameprofile, PoseStack poseStack, MultiBufferSource bufferSource, int combinedLight) {
boolean flag = gameprofile != null && gameprofile.isComplete() && SkinUtil.isSlimSkin(gameprofile.getId());
VertexConsumer vertexConsumer = bufferSource.getBuffer(PlayerTileRenderer.getRenderType(gameprofile));
if(flag) {
SkinUtil.SkinRenderData skinRenderData = SkinUtil.getSkinRenderData(gameprofile);
VertexConsumer vertexConsumer = bufferSource.getBuffer(RenderType.entityTranslucent(skinRenderData.skinLocation));
if(skinRenderData.isSlim) {
if(slimModel != null) {
slimModel.renderToBuffer(poseStack, vertexConsumer, combinedLight, OverlayTexture.NO_OVERLAY, 1.0F, 1.0F, 1.0F, 1.0F);
}
Expand All @@ -135,4 +136,4 @@ public void renderItem(GameProfile gameprofile, PoseStack poseStack, MultiBuffer
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.shynieke.statues.client.render;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Vector3f;
import com.shynieke.statues.blocks.statues.PlayerStatueBlock;
import com.shynieke.statues.client.ClientHandler;
import com.shynieke.statues.client.model.StatuePlayerTileModel;
import com.shynieke.statues.tiles.PlayerBlockEntity;
import net.minecraft.client.Minecraft;
import com.shynieke.statues.util.SkinUtil;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
Expand All @@ -19,13 +17,11 @@
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nullable;
import java.util.Map;

@OnlyIn(Dist.CLIENT)
public class PlayerTileRenderer implements BlockEntityRenderer<PlayerBlockEntity> {
Expand Down Expand Up @@ -82,13 +78,8 @@ public static RenderType getRenderType(@Nullable GameProfile gameProfileIn) {
if (gameProfileIn == null || !gameProfileIn.isComplete()) {
return RenderType.entityCutoutNoCull(defaultTexture);
} else {
final Minecraft minecraft = Minecraft.getInstance();
final Map<Type, MinecraftProfileTexture> map = minecraft.getSkinManager().getInsecureSkinInformation(gameProfileIn);
if (map.containsKey(Type.SKIN)) {
return RenderType.entityTranslucent(minecraft.getSkinManager().registerTexture((MinecraftProfileTexture)map.get(Type.SKIN), Type.SKIN));
} else {
return RenderType.entityCutoutNoCull(DefaultPlayerSkin.getDefaultSkin(Player.createPlayerUUID(gameProfileIn)));
}
SkinUtil.SkinRenderData skinRenderData = SkinUtil.getSkinRenderData(gameProfileIn);
return RenderType.entityTranslucent(skinRenderData.skinLocation);
}
}
}
16 changes: 3 additions & 13 deletions src/main/java/com/shynieke/statues/entity/PlayerStatue.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class PlayerStatue extends LivingEntity {
/** After punching the stand, the cooldown before you can punch it again without breaking it. */
public long punchCooldown;
private int disabledSlots;
private boolean isSlim = false;
private SkinUtil.SkinRenderData currentSkinRenderData;
private Rotations headRotation = DEFAULT_HEAD_ROTATION;
private Rotations bodyRotation = DEFAULT_BODY_ROTATION;
private Rotations leftArmRotation = DEFAULT_LEFTARM_ROTATION;
Expand Down Expand Up @@ -149,7 +149,6 @@ public Optional<GameProfile> getGameProfile() {
public void setGameProfile(GameProfile playerProfile) {
PlayerBlockEntity.updateGameprofile(playerProfile, (profile) -> {
entityData.set(GAMEPROFILE, Optional.of(profile));
this.setSlim(profile != null && profile.getId() != null && SkinUtil.isSlimSkin(profile.getId()));
});
}

Expand All @@ -171,12 +170,8 @@ public void setUnlocked() {
this.entityData.set(LOCKED_BY_UUID, Optional.empty());
}

public void setSlim(boolean slim) {
this.isSlim = slim;
}

public boolean isSlim() {
return this.isSlim;
return this.currentSkinRenderData != null && this.currentSkinRenderData.isSlim;
}

public void setYOffset(float yOffset) {
Expand Down Expand Up @@ -890,12 +885,7 @@ public void onSyncedDataUpdated(EntityDataAccessor<?> key) {
if (this.level.isClientSide) {
this.getGameProfile().ifPresent(gameProfile -> {
if(gameProfile.isComplete()) {
Minecraft.getInstance().getSkinManager().registerSkins(gameProfile, (textureType, textureLocation, profileTexture) -> {
if (textureType.equals(MinecraftProfileTexture.Type.SKIN)) {
String metadata = profileTexture.getMetadata("model");
this.setSlim(metadata != null && metadata.equals("slim"));
}
}, true);
this.currentSkinRenderData = SkinUtil.getSkinRenderData(gameProfile);
}
});
}
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/com/shynieke/statues/tiles/PlayerBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.authlib.properties.Property;
import com.shynieke.statues.blocks.statues.PlayerStatueBlock;
import com.shynieke.statues.init.StatueBlockEntities;
import com.shynieke.statues.init.StatueRegistry;
import com.shynieke.statues.util.SkinUtil;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
Expand All @@ -30,6 +29,7 @@
import java.util.function.Consumer;

public class PlayerBlockEntity extends BlockEntity implements Nameable {

@Nullable
private static GameProfileCache profileCache;
@Nullable
Expand All @@ -38,7 +38,7 @@ public class PlayerBlockEntity extends BlockEntity implements Nameable {
private static Executor mainThreadExecutor;

private GameProfile playerProfile;
private boolean isSlim = false;
private SkinUtil.SkinRenderData currentSkinRenderData;
private boolean comparatorApplied;
private boolean onlineChecking;
private int checkerCooldown;
Expand Down Expand Up @@ -123,19 +123,14 @@ public GameProfile getPlayerProfile() {
}

public boolean isSlim() {
return this.isSlim;
return this.currentSkinRenderData != null && this.currentSkinRenderData.isSlim;
}

public void setPlayerProfile(@Nullable GameProfile profile) {
synchronized(this) {
this.playerProfile = profile;
if (this.level != null && this.level.isClientSide && this.playerProfile != null && this.playerProfile.isComplete() ) {
Minecraft.getInstance().getSkinManager().registerSkins(this.playerProfile, (textureType, textureLocation, profileTexture) -> {
if (textureType.equals(MinecraftProfileTexture.Type.SKIN)) {
String metadata = profileTexture.getMetadata("model");
this.isSlim = metadata != null && metadata.equals("slim");
}
}, true);
if (this.level != null && this.level.isClientSide && this.playerProfile != null) {
this.currentSkinRenderData = SkinUtil.getSkinRenderData(profile);
}
}

Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/shynieke/statues/util/SkinUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
package com.shynieke.statues.util;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SkinUtil {

public static class SkinRenderData {
public ResourceLocation skinLocation;
public boolean isSlim = false;

public SkinRenderData(GameProfile playerProfile) {
UUID tempUUID = Player.createPlayerUUID(playerProfile);
skinLocation = DefaultPlayerSkin.getDefaultSkin(tempUUID);
isSlim = DefaultPlayerSkin.getSkinModelName(tempUUID).equals("slim");
}

SkinRenderData(ResourceLocation skinLocation) {
this.skinLocation = skinLocation;
}
}

private static final SkinRenderData NULL_PROFILE_DATA = new SkinRenderData(DefaultPlayerSkin.getDefaultSkin());

private static final Map<UUID, SkinRenderData> SKINRENDER_CACHE = new HashMap<>();

/**
* Will return defaults until data is retrieved. And will create a request if none has been done before.
*
* As it is a reference for things like entities and tile entities you can keep a copy.
*
* May want to possibly store when the object was created or if the return timed to check when things come online.
*
* @param playerProfile
* @return
*/
public static SkinRenderData getSkinRenderData(GameProfile playerProfile) {
if(playerProfile == null) {
return NULL_PROFILE_DATA;
}
UUID playerId = playerProfile.getId();
if(!SKINRENDER_CACHE.containsKey(playerId)) {
SkinRenderData newRenderData = new SkinRenderData(playerProfile);
SKINRENDER_CACHE.put(playerId, newRenderData);
Minecraft.getInstance().getSkinManager().registerSkins(playerProfile, (textureType, textureLocation, profileTexture) -> {
if (textureType.equals(MinecraftProfileTexture.Type.SKIN)) {
String metadata = profileTexture.getMetadata("model");
newRenderData.skinLocation = textureLocation;
newRenderData.isSlim = metadata != null && metadata.equals("slim");
}
}, true);
return newRenderData;
} else {
return SKINRENDER_CACHE.get(playerId);
}
}

public static boolean isSlimSkin(UUID playerUUID) {
return (playerUUID.hashCode() & 1) == 1;
}
Expand Down