#How to hold item out infront of player (like a spear)

6 messages · Page 1 of 1 (latest)

cedar elbow
#

I am completly new to this, and I'm trying to make a simple hotdog on a stick that you can hold out infront of a campfire to cook and recieve some buffs from. I have everything else in place already, I just don't know how I would make the item be held out, like how you would hold out a stick over a campfire when roasting marshmellows. I tried this:
@Override public ItemUseAnimation getUseAnimation(ItemStack itemStack) { return ItemUseAnimation.SPEAR; }
But looking at the internal Item class, it seems that the animation won't be rendered unless the item has the KINETIC_WEAPON DataComponent, and I don't particularly want to make my hotdog stick a weapon... so.. I'm cluess.

I also tried some mixin stuff for a custom animation but that too did not work.

dreamy marten
#

Hello there. In order to change the way player holds items, as far as i have found, you must use mixins. You should inject at HeldItemRenderer(1.21.1) / ItemInHandRenderer (26.1-SNAP-SHOT4). and inside renderFirstPersonItem / renderArmWithItem method. Although, you should also have a basic understanding of MatrixStack. I'll give a simple one i have made so far (It's not at all optimized though, but good for starting point, it's for 1.21.1):

#
    private FirstPersonOnItemUseRenderer() {}

    public static void render(
            final AbstractClientPlayerEntity player,
            final float tickDelta,
            final float pitch,
            final Hand hand,
            final float swingProgress,
            final ItemStack item,
            final float equipProgress,
            final MatrixStack matrices,
            final VertexConsumerProvider vertexConsumers,
            final int light,
            final PlayerEntityRenderer playerRenderer,
            final CallbackInfo ci) {
        ClientPlayerEntity clientPlayer = (ClientPlayerEntity) player;

        if (shouldRender(player, item)) {
            matrices.push();

            renderArm(
                    matrices,
                    vertexConsumers,                          
                    hand,                                    
                    swingProgress,
                    equipProgress,
                    light,
                    clientPlayer,
                    playerRenderer
            );

            matrices.translate(0.5, -0.44, -1.2);
            matrices.multiply(RotationAxis.NEGATIVE_X.rotationDegrees(90));
            renderItem(
                    player,
                    item,
                    hand,
                    matrices,
                    vertexConsumers,
                    light
            );

            ci.cancel();
        }
    }
#
    private static void renderArm(
        final MatrixStack matrices,
        final VertexConsumerProvider vertexConsumers,
        final Hand hand,
        final float swingProgress,
        final float equipProgress,
        final int light,
        final ClientPlayerEntity player,
        final PlayerEntityRenderer playerEntityRenderer
    ) {

        boolean isMainHand = hand == Hand.MAIN_HAND;
        Arm arm = isMainHand
                ? player.getMainArm()
                : player.getMainArm().getOpposite();

        matrices.push();

        float handOffset = arm == Arm.RIGHT ? 1.0F : -1.0F;

        matrices.translate(handOffset * 0.3F, -1.1F, -2F);
        matrices.translate(0.3, 0.075, 1.35);
        matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(90));
        matrices.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(-75));
        matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(0));

        if (arm == Arm.RIGHT) {
            playerEntityRenderer.renderRightArm(matrices, vertexConsumers, light, player);
        } else {
            playerEntityRenderer.renderLeftArm(matrices, vertexConsumers, light, player);
        }

        matrices.pop();
    }
#
    private static void renderItem(
            final AbstractClientPlayerEntity player,
            final ItemStack item,
            final Hand hand,
            final MatrixStack matrices,
            final VertexConsumerProvider vertexConsumers,
            final int light
    ) {
        boolean isMainHand = hand == Hand.MAIN_HAND;
        Arm arm = isMainHand
                ? player.getMainArm()
                : player.getMainArm().getOpposite();
        boolean rightArm = arm == Arm.RIGHT;

        HeldItemRenderer heldItemRenderer = MinecraftClient.getInstance().gameRenderer.firstPersonRenderer;

        heldItemRenderer.renderItem(
                player,
                item,
                rightArm
                        ? ModelTransformationMode.FIRST_PERSON_RIGHT_HAND
                        : ModelTransformationMode.FIRST_PERSON_LEFT_HAND,
                !rightArm,
                matrices,
                vertexConsumers,
                light
        );
    }
#

These all fall in the same class.