Skip to content

Commit

Permalink
Rework head hiding to support more mods
Browse files Browse the repository at this point in the history
Fixes Improved-Animations
  • Loading branch information
tr7zw committed Feb 7, 2024
1 parent 708ec0d commit 73f747f
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void upgradeConfig(FirstPersonSettings conf) {
}
if (conf.configVersion < 2) {
conf.autoToggleModItems.add("exposure:camera");
conf.configVersion = 2;
conf.configVersion = 2;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class FirstPersonSettings {
public Set<String> autoVanillaHands = new HashSet<>(Arrays.asList("antiqueatlas:antique_atlas",
"twilightforest:filled_magic_map", "twilightforest:filled_maze_map", "twilightforest:filled_ore_map",
"create:potato_cannon", "create:extendo_grip", "create:handheld_worldshaper", "map_atlases:atlas"));

public Set<String> autoToggleModItems = new HashSet<>(Arrays.asList("exposure:camera"));

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.tr7zw.firstperson.access;

import net.minecraft.client.model.geom.ModelPart;

public interface AgeableListModelAccess {

Iterable<ModelPart> firstPersonHeadPartsGetter();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.tr7zw.firstperson.mixins;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import dev.tr7zw.firstperson.access.AgeableListModelAccess;
import net.minecraft.client.model.AgeableListModel;
import net.minecraft.client.model.geom.ModelPart;

@Mixin(AgeableListModel.class)
public abstract class AgeableListModelMixin implements AgeableListModelAccess {

@Override
public Iterable<ModelPart> firstPersonHeadPartsGetter() {
return headParts();
}

@Shadow
public abstract Iterable<ModelPart> headParts();

}
60 changes: 0 additions & 60 deletions src/main/java/dev/tr7zw/firstperson/mixins/HumanoidModelMixin.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.mojang.blaze3d.vertex.PoseStack;

import dev.tr7zw.firstperson.FirstPersonModelCore;
import dev.tr7zw.firstperson.access.AgeableListModelAccess;
import dev.tr7zw.firstperson.access.PlayerModelAccess;
import dev.tr7zw.firstperson.versionless.mixinbase.ModelPartBase;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.HeadedModel;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.Model;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.model.VillagerHeadModel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.world.entity.LivingEntity;
Expand All @@ -22,40 +28,86 @@
@Mixin(LivingEntityRenderer.class)
public abstract class LivingEntityRendererMixin {

@Inject(method = "render", at = @At("HEAD"), cancellable = true)
public void renderHead(LivingEntity livingEntity, float f, float g, PoseStack matrixStack,
// pull all registers to try to get rid of the head or other bodyparts
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/model/EntityModel;setupAnim(Lnet/minecraft/world/entity/Entity;FFFFF)V", shift = Shift.AFTER), cancellable = true)
public void renderPostAnim(LivingEntity livingEntity, float f, float g, PoseStack matrixStack,
MultiBufferSource vertexConsumerProvider, int i, CallbackInfo info) {
if (!FirstPersonModelCore.instance.isRenderingPlayer())
return;
if (livingEntity instanceof Shulker) {
return;// No need to mess with
}
// only run when the player is rendering, and it's not a "Humanoid" model(it
// otherwise gets handeled in HumanoidModelMixin)
if (FirstPersonModelCore.instance.isRenderingPlayer() && !(getModel() instanceof HumanoidModel)) {
EntityModel<LivingEntity> model = getModel();
if (!(model instanceof HeadedModel)) {
FirstPersonModelCore.instance.setRenderingPlayer(false);
info.cancel();
return;
}
((ModelPartBase) (Object) ((HeadedModel) model).getHead()).setHidden();
if (model instanceof VillagerHeadModel villaterHead) {
villaterHead.hatVisible(false);
Model model = getModel();
boolean headShouldBeHidden = false;
if (model instanceof AgeableListModelAccess agable) {
agable.firstPersonHeadPartsGetter().forEach(part -> ((ModelPartBase) (Object) part).setHidden());
headShouldBeHidden = true;
}
if (model instanceof HeadedModel headed) {
((ModelPartBase) (Object) headed.getHead()).setHidden();
headShouldBeHidden = true;
}
if (model instanceof HumanoidModel<?> humanModel
&& FirstPersonModelCore.instance.getLogicHandler().showVanillaHands()) {
((ModelPartBase) (Object) humanModel.leftArm).setHidden();
((ModelPartBase) (Object) humanModel.rightArm).setHidden();
}
if (model instanceof VillagerHeadModel villaterHead) {
villaterHead.hatVisible(false);
}
if (model instanceof PlayerModel<?> playerModel) {
headShouldBeHidden = true;
((ModelPartBase) (Object) playerModel.hat).setHidden();
if (FirstPersonModelCore.instance.getLogicHandler().showVanillaHands()) {
((ModelPartBase) (Object) playerModel.leftSleeve).setHidden();
((ModelPartBase) (Object) playerModel.rightSleeve).setHidden();
}
}
if (livingEntity instanceof AbstractClientPlayer player && (Object) model instanceof PlayerModel<?> playerModel
&& FirstPersonModelCore.instance.getLogicHandler().isSwimming(player)) {
((ModelPartBase) (Object) playerModel.body).setHidden();
((ModelPartBase) (Object) ((PlayerModelAccess) model).getCloak()).setHidden();
}
if (!headShouldBeHidden) {
// we failed to hide the head. So either its a mob without one, or something is
// going really wrong. Cancel the render for the firstperson mode
matrixStack.popPose();
info.cancel();
}
}

@Inject(method = "render", at = @At("RETURN"))
public void renderReturn(LivingEntity livingEntity, float f, float g, PoseStack matrixStack,
MultiBufferSource vertexConsumerProvider, int i, CallbackInfo info) {
if (FirstPersonModelCore.instance.isRenderingPlayer()) {
EntityModel<LivingEntity> model = getModel();
if (model instanceof HeadedModel) {
((ModelPartBase) (Object) ((HeadedModel) model).getHead()).showAgain();
if (model instanceof VillagerHeadModel villaterHead) {
villaterHead.hatVisible(true);
}
if (!FirstPersonModelCore.instance.isRenderingPlayer())
return;
// revert sate
Model model = getModel();
if (model instanceof AgeableListModelAccess agable) {
agable.firstPersonHeadPartsGetter().forEach(part -> ((ModelPartBase) (Object) part).showAgain());
}
if (model instanceof HeadedModel headed) {
((ModelPartBase) (Object) headed.getHead()).showAgain();
}
if (model instanceof HumanoidModel<?> humanModel
&& FirstPersonModelCore.instance.getLogicHandler().showVanillaHands()) {
((ModelPartBase) (Object) humanModel.leftArm).showAgain();
((ModelPartBase) (Object) humanModel.rightArm).showAgain();
}
if (model instanceof VillagerHeadModel villaterHead) {
villaterHead.hatVisible(false);
}
if (model instanceof PlayerModel<?> playerModel) {
((ModelPartBase) (Object) playerModel.hat).showAgain();
if (FirstPersonModelCore.instance.getLogicHandler().showVanillaHands()) {
((ModelPartBase) (Object) playerModel.leftSleeve).showAgain();
((ModelPartBase) (Object) playerModel.rightSleeve).showAgain();
}
}
if ((Object) model instanceof PlayerModel<?> playerModel) {
((ModelPartBase) (Object) playerModel.body).showAgain();
((ModelPartBase) (Object) ((PlayerModelAccess) model).getCloak()).showAgain();
}
FirstPersonModelCore.instance.setRenderingPlayer(false);
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/dev/tr7zw/firstperson/mixins/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ public class PlayerMixin {

@Shadow
private Inventory inventory;

@Inject(method = "getItemBySlot", at = @At("HEAD"), cancellable = true)
public void getItemBySlot(EquipmentSlot slot, CallbackInfoReturnable<ItemStack> ci) {
if (FirstPersonModelCore.instance.isRenderingPlayer() && Minecraft.getInstance().isSameThread()) {
if (slot == EquipmentSlot.HEAD) {
ci.setReturnValue(ItemStack.EMPTY);
return;
}
if (FirstPersonModelCore.instance.getLogicHandler().showVanillaHands(this.inventory.getSelected(), this.inventory.offhand.get(0))) {
if (FirstPersonModelCore.instance.getLogicHandler().showVanillaHands(this.inventory.getSelected(),
this.inventory.offhand.get(0))) {
ci.setReturnValue(ItemStack.EMPTY);
return;
}
Expand Down
70 changes: 0 additions & 70 deletions src/main/java/dev/tr7zw/firstperson/mixins/PlayerRenderMixin.java

This file was deleted.

5 changes: 2 additions & 3 deletions src/main/resources/firstperson.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"mixins": [],
"client": [
"HeldItemRendererMixin",
"PlayerRenderMixin",
"WorldRendererMixin",
"FishingBobberRendererMixin",
"RenderDispatcherMixin",
Expand All @@ -17,10 +16,10 @@
"StuckInBodyLayerMixin",
"ElytraLayerMixin",
"PlayerRendererMixin",
"HumanoidModelMixin",
"CustomHeadLayerMixin",
"PlayerModelMixin",
"PlayerMixin"
"PlayerMixin",
"AgeableListModelMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 73f747f

Please sign in to comment.