| 1 | package net.minecraft.client.player;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.renderer.state.MapRenderState;
|
| 6 | import net.minecraft.client.renderer.state.level.FirstPersonHandsAndItemsRenderState;
|
| 7 | import net.minecraft.core.component.DataComponentType;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.world.InteractionHand;
|
| 11 | import net.minecraft.world.entity.HumanoidArm;
|
| 12 | import net.minecraft.world.entity.LivingEntity.SwingDescription;
|
| 13 | import net.minecraft.world.item.CrossbowItem;
|
| 14 | import net.minecraft.world.item.ItemDisplayContext;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.Items;
|
| 17 | import net.minecraft.world.item.MapItem;
|
| 18 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 19 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class FirstPersonHandsAndItems {
|
| 23 | private ItemStack mainHandItem = ItemStack.EMPTY;
|
| 24 | private ItemStack offHandItem = ItemStack.EMPTY;
|
| 25 | private float mainHandHeight;
|
| 26 | private float oMainHandHeight;
|
| 27 | private float offHandHeight;
|
| 28 | private float oOffHandHeight;
|
| 29 |
|
| 30 | public void tick(final LocalPlayer player) {
|
| 31 | this.oMainHandHeight = this.mainHandHeight;
|
| 32 | this.oOffHandHeight = this.offHandHeight;
|
| 33 | ItemStack nextMainHand = player.getMainHandItem();
|
| 34 | ItemStack nextOffHand = player.getOffhandItem();
|
| 35 | if (this.shouldInstantlyReplaceVisibleItem(this.mainHandItem, nextMainHand, player)) {
|
| 36 | this.mainHandItem = nextMainHand;
|
| 37 | }
|
| 38 |
|
| 39 | if (this.shouldInstantlyReplaceVisibleItem(this.offHandItem, nextOffHand, player)) {
|
| 40 | this.offHandItem = nextOffHand;
|
| 41 | }
|
| 42 |
|
| 43 | if (player.isHandsBusy()) {
|
| 44 | this.mainHandHeight = Mth.clamp(this.mainHandHeight - 0.4F, 0.0F, 1.0F);
|
| 45 | this.offHandHeight = Mth.clamp(this.offHandHeight - 0.4F, 0.0F, 1.0F);
|
| 46 | } else {
|
| 47 | float attackAnim = player.getItemSwapScale(1.0F);
|
| 48 | float mainHandTargetHeight = this.mainHandItem != nextMainHand ? 0.0F : attackAnim * attackAnim * attackAnim;
|
| 49 | float offHandTargetHeight = this.offHandItem != nextOffHand ? 0.0F : 1.0F;
|
| 50 | this.mainHandHeight = this.mainHandHeight + Mth.clamp(mainHandTargetHeight - this.mainHandHeight, -0.4F, 0.4F);
|
| 51 | this.offHandHeight = this.offHandHeight + Mth.clamp(offHandTargetHeight - this.offHandHeight, -0.4F, 0.4F);
|
| 52 | }
|
| 53 |
|
| 54 | if (this.mainHandHeight < 0.1F) {
|
| 55 | this.mainHandItem = nextMainHand;
|
| 56 | }
|
| 57 |
|
| 58 | if (this.offHandHeight < 0.1F) {
|
| 59 | this.offHandItem = nextOffHand;
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | public void itemUsed(final InteractionHand hand) {
|
| 64 | if (hand == InteractionHand.MAIN_HAND) {
|
| 65 | this.mainHandHeight = 0.0F;
|
| 66 | } else {
|
| 67 | this.offHandHeight = 0.0F;
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | public void extractRenderState(final LocalPlayer player, final float partialTicks, final FirstPersonHandsAndItemsRenderState state) {
|
| 72 | SwingDescription currentSwing = player.getCurrentSwing();
|
| 73 | state.attackHand = currentSwing == null ? InteractionHand.MAIN_HAND : currentSwing.hand();
|
| 74 | state.viewXRot = player.getViewXRot(partialTicks);
|
| 75 | state.viewYRot = player.getViewYRot(partialTicks);
|
| 76 | state.xBob = Mth.lerp(partialTicks, player.xBobO, player.xBob);
|
| 77 | state.yBob = Mth.lerp(partialTicks, player.yBobO, player.yBob);
|
| 78 | state.isScoping = player.isScoping();
|
| 79 | state.useItemRemainingTicks = player.getUseItemRemainingTicks();
|
| 80 | state.handRenderSelection = evaluateWhichHandsToRender(player);
|
| 81 | state.mainHandItem = this.mainHandItem;
|
| 82 | state.offHandItem = this.offHandItem;
|
| 83 | state.mainHandHeight = this.mainHandHeight;
|
| 84 | state.oldMainHandHeight = this.oMainHandHeight;
|
| 85 | state.offHandHeight = this.offHandHeight;
|
| 86 | state.oldOffHandHeight = this.oOffHandHeight;
|
| 87 | boolean isMainHandRight = player.getMainArm() == HumanoidArm.RIGHT;
|
| 88 | ItemDisplayContext mainHandDisplayContext = isMainHandRight ? ItemDisplayContext.FIRST_PERSON_RIGHT_HAND : ItemDisplayContext.FIRST_PERSON_LEFT_HAND;
|
| 89 | ItemDisplayContext offHandDisplayContext = isMainHandRight ? ItemDisplayContext.FIRST_PERSON_LEFT_HAND : ItemDisplayContext.FIRST_PERSON_RIGHT_HAND;
|
| 90 | state.mainHandRenderState.clear();
|
| 91 | state.offHandRenderState.clear();
|
| 92 | player.minecraft
|
| 93 | .getItemModelResolver()
|
| 94 | .updateForTopItem(
|
| 95 | state.mainHandRenderState, state.mainHandItem, mainHandDisplayContext, player.level(), player, player.getId() + mainHandDisplayContext.ordinal()
|
| 96 | );
|
| 97 | player.minecraft
|
| 98 | .getItemModelResolver()
|
| 99 | .updateForTopItem(
|
| 100 | state.offHandRenderState, state.offHandItem, offHandDisplayContext, player.level(), player, player.getId() + offHandDisplayContext.ordinal()
|
| 101 | );
|
| 102 | state.mainHandUseDuration = state.mainHandItem.getUseDuration(player);
|
| 103 | state.offHandUseDuration = state.offHandItem.getUseDuration(player);
|
| 104 | state.mainHandChargeDuration = CrossbowItem.getChargeDuration(state.mainHandItem, player);
|
| 105 | state.offHandChargeDuration = CrossbowItem.getChargeDuration(state.offHandItem, player);
|
| 106 | state.mainHandSwapScale = player.minecraft.getItemModelResolver().swapAnimationScale(state.mainHandItem);
|
| 107 | state.offHandSwapScale = player.minecraft.getItemModelResolver().swapAnimationScale(state.offHandItem);
|
| 108 | state.hasMainHandMapData = this.extractMapRenderState(player, state.mainHandItem, state.mainHandMapRenderState);
|
| 109 | state.hasOffHandMapData = this.extractMapRenderState(player, state.offHandItem, state.offHandMapRenderState);
|
| 110 | }
|
| 111 |
|
| 112 | private boolean shouldInstantlyReplaceVisibleItem(final ItemStack currentlyVisibleItem, final ItemStack expectedItem, final LocalPlayer player) {
|
| 113 | return ItemStack.matchesIgnoringComponents(currentlyVisibleItem, expectedItem, DataComponentType::ignoreSwapAnimation)
|
| 114 | ? true
|
| 115 | : !player.minecraft.getItemModelResolver().shouldPlaySwapAnimation(expectedItem);
|
| 116 | }
|
| 117 |
|
| 118 | private boolean extractMapRenderState(final LocalPlayer player, final ItemStack itemStack, final MapRenderState state) {
|
| 119 | MapId mapId = itemStack.get(DataComponents.MAP_ID);
|
| 120 | MapItemSavedData mapData = mapId == null ? null : MapItem.getSavedData(mapId, player.level());
|
| 121 | if (mapId != null && mapData != null) {
|
| 122 | player.minecraft.getMapRenderer().extractRenderState(mapId, mapData, state);
|
| 123 | return true;
|
| 124 | } else {
|
| 125 | return false;
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | public static FirstPersonHandsAndItemsRenderState.HandRenderSelection evaluateWhichHandsToRender(final LocalPlayer player) {
|
| 130 | ItemStack mainHandItem = player.getMainHandItem();
|
| 131 | ItemStack offhandItem = player.getOffhandItem();
|
| 132 | boolean holdsBow = mainHandItem.is(Items.BOW) || offhandItem.is(Items.BOW);
|
| 133 | boolean holdsCrossbow = mainHandItem.is(Items.CROSSBOW) || offhandItem.is(Items.CROSSBOW);
|
| 134 | if (!holdsBow && !holdsCrossbow) {
|
| 135 | return FirstPersonHandsAndItemsRenderState.HandRenderSelection.RENDER_BOTH_HANDS;
|
| 136 | } else if (!player.isUsingItem()) {
|
| 137 | return isChargedCrossbow(mainHandItem)
|
| 138 | ? FirstPersonHandsAndItemsRenderState.HandRenderSelection.RENDER_MAIN_HAND_ONLY
|
| 139 | : FirstPersonHandsAndItemsRenderState.HandRenderSelection.RENDER_BOTH_HANDS;
|
| 140 | } else {
|
| 141 | ItemStack usedItemStack = player.getUseItem();
|
| 142 | InteractionHand usedHand = player.getUsedItemHand();
|
| 143 | if (!usedItemStack.is(Items.BOW) && !usedItemStack.is(Items.CROSSBOW)) {
|
| 144 | return usedHand == InteractionHand.MAIN_HAND && isChargedCrossbow(offhandItem)
|
| 145 | ? FirstPersonHandsAndItemsRenderState.HandRenderSelection.RENDER_MAIN_HAND_ONLY
|
| 146 | : FirstPersonHandsAndItemsRenderState.HandRenderSelection.RENDER_BOTH_HANDS;
|
| 147 | } else {
|
| 148 | return FirstPersonHandsAndItemsRenderState.HandRenderSelection.onlyForHand(usedHand);
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private static boolean isChargedCrossbow(final ItemStack item) {
|
| 154 | return item.is(Items.CROSSBOW) && CrossbowItem.isCharged(item);
|
| 155 | }
|
| 156 | }
|
| 157 |
|