-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
CustomHeadLayerMixin.java
68 lines (61 loc) · 3.07 KB
/
CustomHeadLayerMixin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package dev.tr7zw.skinlayers.mixin;
import static dev.tr7zw.skinlayers.SkullRendererCache.itemCache;
import static dev.tr7zw.skinlayers.SkullRendererCache.lastSkull;
import static dev.tr7zw.skinlayers.SkullRendererCache.renderNext;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.mojang.authlib.GameProfile;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.tr7zw.skinlayers.SkinLayersModBase;
import dev.tr7zw.skinlayers.SkinUtil;
import dev.tr7zw.skinlayers.SkullRendererCache.ItemSettings;
import dev.tr7zw.skinlayers.accessor.SkullSettings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.HeadedModel;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.layers.CustomHeadLayer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.AbstractSkullBlock;
@Mixin(CustomHeadLayer.class)
public class CustomHeadLayerMixin<T extends LivingEntity, M extends EntityModel<T> & HeadedModel> {
@SuppressWarnings("resource")
@Inject(method = "render", at = @At("HEAD"))
public void render(PoseStack poseStack, MultiBufferSource multiBufferSource, int i, T livingEntity, float f,
float g, float h, float j, float k, float l, CallbackInfo info) {
if(!SkinLayersModBase.config.enableSkulls)return;
if(livingEntity.distanceToSqr(Minecraft.getInstance().player) > SkinLayersModBase.config.renderDistanceLOD*SkinLayersModBase.config.renderDistanceLOD) {
return; // too far away
}
ItemStack itemStack = livingEntity.getItemBySlot(EquipmentSlot.HEAD);
if (itemStack.isEmpty())
return;
Item item = itemStack.getItem();
if (item instanceof BlockItem && ((BlockItem) item).getBlock() instanceof AbstractSkullBlock) {
GameProfile gameProfile = null;
if (itemStack.hasTag()) {
CompoundTag compoundTag = itemStack.getTag();
if(compoundTag.contains("CustomModelData")) {
return; // do not try to 3d-fy custom head models
}
if (compoundTag.contains("SkullOwner", 10))
gameProfile = NbtUtils.readGameProfile(compoundTag.getCompound("SkullOwner"));
}
if(gameProfile != null) {
lastSkull = (SkullSettings) itemCache.computeIfAbsent(itemStack, it -> new ItemSettings());
if(lastSkull.getHeadLayers() == null) {
SkinUtil.setup3dLayers(gameProfile, lastSkull);
}
renderNext = lastSkull.getHeadLayers() != null;
}
}
}
}