| 1 | package net.minecraft.client.model.player;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.model.HumanoidModel;
|
| 8 | import net.minecraft.client.model.geom.ModelPart;
|
| 9 | import net.minecraft.client.model.geom.PartPose;
|
| 10 | import net.minecraft.client.model.geom.builders.CubeDeformation;
|
| 11 | import net.minecraft.client.model.geom.builders.CubeListBuilder;
|
| 12 | import net.minecraft.client.model.geom.builders.MeshDefinition;
|
| 13 | import net.minecraft.client.model.geom.builders.PartDefinition;
|
| 14 | import net.minecraft.client.renderer.entity.ArmorModelSet;
|
| 15 | import net.minecraft.client.renderer.entity.state.AvatarRenderState;
|
| 16 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 17 | import net.minecraft.util.RandomSource;
|
| 18 | import net.minecraft.util.Util;
|
| 19 | import net.minecraft.world.entity.HumanoidArm;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class PlayerModel extends HumanoidModel<AvatarRenderState> {
|
| 23 | protected static final String LEFT_SLEEVE = "left_sleeve";
|
| 24 | protected static final String RIGHT_SLEEVE = "right_sleeve";
|
| 25 | protected static final String LEFT_PANTS = "left_pants";
|
| 26 | protected static final String RIGHT_PANTS = "right_pants";
|
| 27 | private final List<ModelPart> bodyParts;
|
| 28 | public final ModelPart leftSleeve;
|
| 29 | public final ModelPart rightSleeve;
|
| 30 | public final ModelPart leftPants;
|
| 31 | public final ModelPart rightPants;
|
| 32 | public final ModelPart jacket;
|
| 33 | private final boolean slim;
|
| 34 |
|
| 35 | public PlayerModel(final ModelPart root, final boolean slim) {
|
| 36 | super(root, RenderTypes::entityTranslucent);
|
| 37 | this.slim = slim;
|
| 38 | this.leftSleeve = this.leftArm.getChild("left_sleeve");
|
| 39 | this.rightSleeve = this.rightArm.getChild("right_sleeve");
|
| 40 | this.leftPants = this.leftLeg.getChild("left_pants");
|
| 41 | this.rightPants = this.rightLeg.getChild("right_pants");
|
| 42 | this.jacket = this.body.getChild("jacket");
|
| 43 | this.bodyParts = List.of(this.head, this.body, this.leftArm, this.rightArm, this.leftLeg, this.rightLeg);
|
| 44 | }
|
| 45 |
|
| 46 | public static MeshDefinition createMesh(final CubeDeformation scale, final boolean slim) {
|
| 47 | MeshDefinition mesh = HumanoidModel.createMesh(scale, 0.0F);
|
| 48 | PartDefinition root = mesh.getRoot();
|
| 49 | float overlayScale = 0.25F;
|
| 50 | if (slim) {
|
| 51 | PartDefinition leftArm = root.addOrReplaceChild(
|
| 52 | "left_arm", CubeListBuilder.create().texOffs(32, 48).addBox(-1.0F, -2.0F, -2.0F, 3.0F, 12.0F, 4.0F, scale), PartPose.offset(5.0F, 2.0F, 0.0F)
|
| 53 | );
|
| 54 | PartDefinition rightArm = root.addOrReplaceChild(
|
| 55 | "right_arm", CubeListBuilder.create().texOffs(40, 16).addBox(-2.0F, -2.0F, -2.0F, 3.0F, 12.0F, 4.0F, scale), PartPose.offset(-5.0F, 2.0F, 0.0F)
|
| 56 | );
|
| 57 | leftArm.addOrReplaceChild(
|
| 58 | "left_sleeve", CubeListBuilder.create().texOffs(48, 48).addBox(-1.0F, -2.0F, -2.0F, 3.0F, 12.0F, 4.0F, scale.extend(0.25F)), PartPose.ZERO
|
| 59 | );
|
| 60 | rightArm.addOrReplaceChild(
|
| 61 | "right_sleeve", CubeListBuilder.create().texOffs(40, 32).addBox(-2.0F, -2.0F, -2.0F, 3.0F, 12.0F, 4.0F, scale.extend(0.25F)), PartPose.ZERO
|
| 62 | );
|
| 63 | } else {
|
| 64 | PartDefinition leftArm = root.addOrReplaceChild(
|
| 65 | "left_arm", CubeListBuilder.create().texOffs(32, 48).addBox(-1.0F, -2.0F, -2.0F, 4.0F, 12.0F, 4.0F, scale), PartPose.offset(5.0F, 2.0F, 0.0F)
|
| 66 | );
|
| 67 | PartDefinition rightArm = root.getChild("right_arm");
|
| 68 | leftArm.addOrReplaceChild(
|
| 69 | "left_sleeve", CubeListBuilder.create().texOffs(48, 48).addBox(-1.0F, -2.0F, -2.0F, 4.0F, 12.0F, 4.0F, scale.extend(0.25F)), PartPose.ZERO
|
| 70 | );
|
| 71 | rightArm.addOrReplaceChild(
|
| 72 | "right_sleeve", CubeListBuilder.create().texOffs(40, 32).addBox(-3.0F, -2.0F, -2.0F, 4.0F, 12.0F, 4.0F, scale.extend(0.25F)), PartPose.ZERO
|
| 73 | );
|
| 74 | }
|
| 75 |
|
| 76 | PartDefinition leftLeg = root.addOrReplaceChild(
|
| 77 | "left_leg", CubeListBuilder.create().texOffs(16, 48).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 12.0F, 4.0F, scale), PartPose.offset(1.9F, 12.0F, 0.0F)
|
| 78 | );
|
| 79 | PartDefinition rightLeg = root.getChild("right_leg");
|
| 80 | leftLeg.addOrReplaceChild(
|
| 81 | "left_pants", CubeListBuilder.create().texOffs(0, 48).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 12.0F, 4.0F, scale.extend(0.25F)), PartPose.ZERO
|
| 82 | );
|
| 83 | rightLeg.addOrReplaceChild(
|
| 84 | "right_pants", CubeListBuilder.create().texOffs(0, 32).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 12.0F, 4.0F, scale.extend(0.25F)), PartPose.ZERO
|
| 85 | );
|
| 86 | PartDefinition body = root.getChild("body");
|
| 87 | body.addOrReplaceChild("jacket", CubeListBuilder.create().texOffs(16, 32).addBox(-4.0F, 0.0F, -2.0F, 8.0F, 12.0F, 4.0F, scale.extend(0.25F)), PartPose.ZERO);
|
| 88 | return mesh;
|
| 89 | }
|
| 90 |
|
| 91 | public static ArmorModelSet<MeshDefinition> createArmorMeshSet(final CubeDeformation innerDeformation, final CubeDeformation outerDeformation) {
|
| 92 | return HumanoidModel.createArmorMeshSet(innerDeformation, outerDeformation).map(mesh -> {
|
| 93 | PartDefinition root = mesh.getRoot();
|
| 94 | PartDefinition leftArm = root.getChild("left_arm");
|
| 95 | PartDefinition rightArm = root.getChild("right_arm");
|
| 96 | leftArm.addOrReplaceChild("left_sleeve", CubeListBuilder.create(), PartPose.ZERO);
|
| 97 | rightArm.addOrReplaceChild("right_sleeve", CubeListBuilder.create(), PartPose.ZERO);
|
| 98 | PartDefinition leftLeg = root.getChild("left_leg");
|
| 99 | PartDefinition rightLeg = root.getChild("right_leg");
|
| 100 | leftLeg.addOrReplaceChild("left_pants", CubeListBuilder.create(), PartPose.ZERO);
|
| 101 | rightLeg.addOrReplaceChild("right_pants", CubeListBuilder.create(), PartPose.ZERO);
|
| 102 | PartDefinition body = root.getChild("body");
|
| 103 | body.addOrReplaceChild("jacket", CubeListBuilder.create(), PartPose.ZERO);
|
| 104 | return (MeshDefinition)mesh;
|
| 105 | });
|
| 106 | }
|
| 107 |
|
| 108 | public void setupAnim(final AvatarRenderState state) {
|
| 109 | boolean showBody = !state.isSpectator;
|
| 110 | this.body.visible = showBody;
|
| 111 | this.rightArm.visible = showBody;
|
| 112 | this.leftArm.visible = showBody;
|
| 113 | this.rightLeg.visible = showBody;
|
| 114 | this.leftLeg.visible = showBody;
|
| 115 | this.hat.visible = state.showHat;
|
| 116 | this.jacket.visible = state.showJacket;
|
| 117 | this.leftPants.visible = state.showLeftPants;
|
| 118 | this.rightPants.visible = state.showRightPants;
|
| 119 | this.leftSleeve.visible = state.showLeftSleeve;
|
| 120 | this.rightSleeve.visible = state.showRightSleeve;
|
| 121 | super.setupAnim(state);
|
| 122 | }
|
| 123 |
|
| 124 | public void translateToHand(final AvatarRenderState state, final HumanoidArm arm, final PoseStack poseStack) {
|
| 125 | this.root().translateAndRotate(poseStack);
|
| 126 | ModelPart part = this.getArm(arm);
|
| 127 | if (this.slim) {
|
| 128 | float offset = 0.5F * (arm == HumanoidArm.RIGHT ? 1 : -1);
|
| 129 | part.x += offset;
|
| 130 | part.translateAndRotate(poseStack);
|
| 131 | part.x -= offset;
|
| 132 | } else {
|
| 133 | part.translateAndRotate(poseStack);
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public ModelPart getRandomBodyPart(final RandomSource random) {
|
| 138 | return Util.getRandom(this.bodyParts, random);
|
| 139 | }
|
| 140 | }
|
| 141 |
|