Jump to content

[1.19.3] Displaying hands in first-person


Luckydel

Recommended Posts

Hi, I have a couple of questions, maybe it's not difficult for someone to answer.
As far as I understand, I need RenderHandEvent

Further, as far as I understand, I need:
ItemInHandRenderer#renderHandsWithItems
But how is it supposed to work? Should I change the PoseStack somehow?

Or, I need PlayerRenderer#renderHand
Or do I need to create my own PlayerRenderer or ItemInHandRenderer


 

Link to comment
Share on other sites

Hands are not displayed ... Maybe I'm doing something wrong.
Event:

Spoiler
	@SubscribeEvent
    public static void DefaultItemAnimation(RenderHandEvent event){
      
        float i = 1.0f;
        Minecraft minecraft = Minecraft.getInstance();
        EntityRenderDispatcher renderer2 = minecraft.getEntityRenderDispatcher();
        AbstractClientPlayer abstractclientplayer = minecraft.player;
        assert abstractclientplayer != null;

        if(checkItem(event.getItemStack().getItem())) {
            Object itemstack = Minecraft.getInstance().player.getItemInHand(event.getHand()).getItem();
            if (itemstack instanceof GunShot gunShot) {
                CUSTOMRENDER2 customrender2 = new CUSTOMRENDER2(minecraft, renderer2, minecraft.getItemRenderer());
                customrender2.renderHandsWithItems(i, event.getPoseStack(),
                        minecraft.renderBuffers().bufferSource(), minecraft.player, 				            minecraft.getEntityRenderDispatcher().getPackedLightCoords(minecraft.player, i));
            }
        }
    }

 

Render:
 

Spoiler
@OnlyIn(Dist.CLIENT)
public class CUSTOMRENDER2 {
    private final Minecraft minecraft;
    private ItemStack mainHandItem = ItemStack.EMPTY;
    private ItemStack offHandItem = ItemStack.EMPTY;
    private float mainHandHeight;
    private float oMainHandHeight;
    private float offHandHeight;
    private float oOffHandHeight;
    private final EntityRenderDispatcher entityRenderDispatcher;
    private final ItemRenderer itemRenderer;

    public CUSTOMRENDER2(Minecraft p_234241_, EntityRenderDispatcher p_234242_, ItemRenderer p_234243_) {
        this.minecraft = p_234241_;
        this.entityRenderDispatcher = p_234242_;
        this.itemRenderer = p_234243_;
    }

    public void renderHandsWithItems(float p_109315_, PoseStack p_109316_, MultiBufferSource.BufferSource p_109317_, LocalPlayer p_109318_, int p_109319_) {
        float f = p_109318_.getAttackAnim(p_109315_);
        InteractionHand interactionhand = MoreObjects.firstNonNull(p_109318_.swingingArm, InteractionHand.MAIN_HAND);
        float f1 = Mth.lerp(p_109315_, p_109318_.xRotO, p_109318_.getXRot());
        CUSTOMRENDER2.HandRenderSelection iteminhandrenderer$handrenderselection = evaluateWhichHandsToRender(p_109318_);
        float f2 = Mth.lerp(p_109315_, p_109318_.xBobO, p_109318_.xBob);
        float f3 = Mth.lerp(p_109315_, p_109318_.yBobO, p_109318_.yBob);
        p_109316_.mulPose(Axis.XP.rotationDegrees((p_109318_.getViewXRot(p_109315_) - f2) * 0.1F));
        p_109316_.mulPose(Axis.YP.rotationDegrees((p_109318_.getViewYRot(p_109315_) - f3) * 0.1F));
        if (iteminhandrenderer$handrenderselection.renderMainHand) {
            float f4 = interactionhand == InteractionHand.MAIN_HAND ? f : 0.0F;
            float f5 = 1.0F - Mth.lerp(p_109315_, this.oMainHandHeight, this.mainHandHeight);
            if(!net.minecraftforge.client.ForgeHooksClient.renderSpecificFirstPersonHand(InteractionHand.MAIN_HAND, p_109316_, p_109317_, p_109319_, p_109315_, f1, f4, f5, this.mainHandItem))
                this.renderArmWithItem(p_109318_, p_109315_, f1, InteractionHand.MAIN_HAND, f4, this.mainHandItem, f5, p_109316_, p_109317_, p_109319_);
        }

        if (iteminhandrenderer$handrenderselection.renderOffHand) {
            float f6 = interactionhand == InteractionHand.OFF_HAND ? f : 0.0F;
            float f7 = 1.0F - Mth.lerp(p_109315_, this.oOffHandHeight, this.offHandHeight);
            if(!net.minecraftforge.client.ForgeHooksClient.renderSpecificFirstPersonHand(InteractionHand.OFF_HAND, p_109316_, p_109317_, p_109319_, p_109315_, f1, f6, f7, this.offHandItem))
                this.renderArmWithItem(p_109318_, p_109315_, f1, InteractionHand.OFF_HAND, f6, this.offHandItem, f7, p_109316_, p_109317_, p_109319_);
        }

        p_109317_.endBatch();
    }

    @VisibleForTesting
    static CUSTOMRENDER2.HandRenderSelection evaluateWhichHandsToRender(LocalPlayer p_172915_) {
        ItemStack itemstack = p_172915_.getMainHandItem();
        ItemStack itemstack1 = p_172915_.getOffhandItem();
        return CUSTOMRENDER2.HandRenderSelection.RENDER_BOTH_HANDS;
    }

    private void renderArmWithItem(AbstractClientPlayer p_109372_, float p_109373_, float p_109374_, InteractionHand p_109375_,
                                   float p_109376_, ItemStack p_109377_, float p_109378_, PoseStack p_109379_, MultiBufferSource p_109380_, int p_109381_) {
        if (!p_109372_.isScoping()) {
            boolean flag = p_109375_ == InteractionHand.MAIN_HAND;
            HumanoidArm humanoidarm = flag ? p_109372_.getMainArm() : p_109372_.getMainArm().getOpposite();
            p_109379_.pushPose();


            this.renderPlayerArm(p_109379_, p_109380_, p_109381_, p_109378_, p_109376_, humanoidarm);

            p_109379_.popPose();
        }
    }

    private void renderPlayerArm(PoseStack p_109347_, MultiBufferSource p_109348_, int p_109349_, float p_109350_, float p_109351_, HumanoidArm p_109352_) {
        boolean flag = p_109352_ != HumanoidArm.LEFT;
        float f = flag ? 1.0F : -1.0F;
        float f1 = Mth.sqrt(p_109351_);
        float f2 = -0.3F * Mth.sin(f1 * (float)Math.PI);
        float f3 = 0.4F * Mth.sin(f1 * ((float)Math.PI * 2F));
        float f4 = -0.4F * Mth.sin(p_109351_ * (float)Math.PI);
        p_109347_.translate(f * (f2 + 0.64000005F), f3 + -0.6F + p_109350_ * -0.6F, f4 + -0.71999997F);
        p_109347_.mulPose(Axis.YP.rotationDegrees(f * 45.0F));
        float f5 = Mth.sin(p_109351_ * p_109351_ * (float)Math.PI);
        float f6 = Mth.sin(f1 * (float)Math.PI);
        p_109347_.mulPose(Axis.YP.rotationDegrees(f * f6 * 70.0F));
        p_109347_.mulPose(Axis.ZP.rotationDegrees(f * f5 * -20.0F));
        AbstractClientPlayer abstractclientplayer = this.minecraft.player;
        RenderSystem.setShaderTexture(0, abstractclientplayer.getSkinTextureLocation());
        p_109347_.translate(f * -1.0F, 3.6F, 3.5F);
        p_109347_.mulPose(Axis.ZP.rotationDegrees(f * 120.0F));
        p_109347_.mulPose(Axis.XP.rotationDegrees(200.0F));
        p_109347_.mulPose(Axis.YP.rotationDegrees(f * -135.0F));
        p_109347_.translate(f * 5.6F, 0.0F, 0.0F);
        PlayerRenderer playerrenderer = (PlayerRenderer)this.entityRenderDispatcher.<AbstractClientPlayer>getRenderer(abstractclientplayer);
        if (flag) {
            playerrenderer.renderRightHand(p_109347_, p_109348_, p_109349_, abstractclientplayer);
        } else {
            playerrenderer.renderLeftHand(p_109347_, p_109348_, p_109349_, abstractclientplayer);
        }

    }
    @OnlyIn(Dist.CLIENT)
    @VisibleForTesting
    static enum HandRenderSelection {
        RENDER_BOTH_HANDS(true, true),
        RENDER_MAIN_HAND_ONLY(true, false),
        RENDER_OFF_HAND_ONLY(false, true);

        final boolean renderMainHand;
        final boolean renderOffHand;

        private HandRenderSelection(boolean p_172928_, boolean p_172929_) {
            this.renderMainHand = p_172928_;
            this.renderOffHand = p_172929_;
        }

        public static CUSTOMRENDER2.HandRenderSelection onlyForHand(InteractionHand p_172932_) {
            return p_172932_ == InteractionHand.MAIN_HAND ? RENDER_MAIN_HAND_ONLY : RENDER_OFF_HAND_ONLY;
        }
    }
}


 

I check if my item is in my hands:

if(checkItem(event.getItemStack().getItem())) {
            Object itemstack = Minecraft.getInstance().player.getItemInHand(event.getHand()).getItem();//возможно оптимизация выводом на шаг верх
            if (itemstack instanceof GunShot gunShot) {

Without it I get crash
 

Spoiler

---- Minecraft Crash Report ----
// This doesn't make any sense!

Time: 2023-02-09 23:39:10
Description: Unexpected error

java.lang.LinkageError: loader constraint violation: loader 'MC-BOOTSTRAP' @5ad851c9 wants to load interface org.apache.logging.log4j.util.MessageSupplier. (org.apache.logging.log4j.util.MessageSupplier is in module [email protected] of loader 'MC-BOOTSTRAP' @5ad851c9, parent loader 'bootstrap')
    at net.minecraftforge.eventbus.EventBus.handleException(EventBus.java:329) ~[eventbus-6.0.3.jar:?] {}
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:320) ~[eventbus-6.0.3.jar:?] {}
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:296) ~[eventbus-6.0.3.jar:?] {}
    at net.minecraftforge.client.ForgeHooksClient.renderSpecificFirstPersonHand(ForgeHooksClient.java:296) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading}
    at net.minecraft.client.renderer.ItemInHandRenderer.renderHandsWithItems(ItemInHandRenderer.java:289) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.client.renderer.GameRenderer.renderItemInHand(GameRenderer.java:818) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.renderer.GameRenderer.renderLevel(GameRenderer.java:1092) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.renderer.GameRenderer.render(GameRenderer.java:875) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.runTick(Minecraft.java:1135) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.run(Minecraft.java:713) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.main.Main.run(Main.java:212) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.client.main.Main.main(Main.java:51) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}
    at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}
    at net.minecraftforge.fml.loading.targets.ForgeClientUserdevLaunchHandler.lambda$launchService$0(ForgeClientUserdevLaunchHandler.java:25) ~[fmlloader-1.19.3-44.0.30.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.Launcher.run(Launcher.java:106) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.Launcher.main(Launcher.java:77) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {}


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Render thread
Stacktrace:
    at net.minecraftforge.eventbus.EventBus.handleException(EventBus.java:329) ~[eventbus-6.0.3.jar%23108!/:?] {}
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:320) ~[eventbus-6.0.3.jar%23108!/:?] {}
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:296) ~[eventbus-6.0.3.jar%23108!/:?] {}
    at net.minecraftforge.client.ForgeHooksClient.renderSpecificFirstPersonHand(ForgeHooksClient.java:296) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar%23185%23192!/:?] {re:classloading}
    at net.minecraft.client.renderer.ItemInHandRenderer.renderHandsWithItems(ItemInHandRenderer.java:289) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar%23186!/:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.client.renderer.GameRenderer.renderItemInHand(GameRenderer.java:818) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar%23186!/:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.renderer.GameRenderer.renderLevel(GameRenderer.java:1092) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar%23186!/:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
-- Affected level --
Details:
    All players: 1 total; [LocalPlayer['Dev'/181, l='ClientLevel', x=-270.05, y=-60.00, z=1.35]]
    Chunk stats: 361, 58
    Level dimension: minecraft:overworld
    Level spawn location: World: (0,-60,0), Section: (at 0,4,0 in 0,-4,0; chunk contains blocks 0,-64,0 to 15,319,15), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,-64,0 to 511,319,511)
    Level time: 156246 game time, 24761 day time
    Server brand: forge
    Server type: Integrated singleplayer server
Stacktrace:
    at net.minecraft.client.multiplayer.ClientLevel.fillReportDetails(ClientLevel.java:452) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.fillReport(Minecraft.java:2319) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.run(Minecraft.java:735) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.main.Main.run(Main.java:212) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.client.main.Main.main(Main.java:51) ~[forge-1.19.3-44.0.30_mapped_official_1.19.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}
    at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}
    at net.minecraftforge.fml.loading.targets.ForgeClientUserdevLaunchHandler.lambda$launchService$0(ForgeClientUserdevLaunchHandler.java:25) ~[fmlloader-1.19.3-44.0.30.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.Launcher.run(Launcher.java:106) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.Launcher.main(Launcher.java:77) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) ~[modlauncher-10.0.8.jar:?] {}
    at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {}


-- Last reload --
Details:
    Reload number: 1
    Reload reason: initial
    Finished: Yes
    Packs: vanilla, mod_resources
 


It enters rendering, but sometimes it crashes if I set a breakpoint

Edited by Luckydel
.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.