Jump to content

Recommended Posts

Posted

Hi, I was trying to make my player look like it was holding a Gun type Item, I saw a couple of topics, wrote a bit of code in the Player render Pre event, but it doesn't do anything, the only thing it changes is when i make the arm not visible, this my GunEvents code:
 

Quote

@Mod.EventBusSubscriber(modid = CustomGunsMod.MOD_ID, bus = Bus.FORGE)
public class GunEvents 
{
    
    @SubscribeEvent    //DISPARAR SIN ANIMACION
    public static void cancelRightClickAnimation(PlayerInteractEvent.RightClickItem event)
    {
        PlayerEntity player = event.getPlayer();
        if(player != null){
            Item item = player.getHeldItemMainhand().getItem();
            if(item instanceof GunBase){
                event.setCanceled(true);
                ((GunBase) item).onItemRightClickPrivateEvent(player.world, player);
            }
        }
    }
    
    @SubscribeEvent        //THIS EVENT NOT WORKING <-----------------------
    public static void holdGun(RenderPlayerEvent.Pre event)
    {
        PlayerEntity player = event.getPlayer();
        
        if(player != null&&player.getHeldItemMainhand().getItem() instanceof GunBase){
            event.getRenderer().getEntityModel().bipedRightArm.rotateAngleX=90;
            event.getRenderer().getEntityModel().swimAnimation=0;
            event.getRenderer().getEntityModel().bipedRightArm.showModel=true;
            //event.getRenderer().getEntityModel().bipedRightArm.showModel=false;
        }
    }

    
    
    
    
}
 


any help would be grateful :), thx
 

Posted (edited)

I haven't find the best solution yet, but any changes (e.g. rotation) made in the pre event will not work because in render method which gets call after pre event resets model's rotation angle and stuff. What I do is hard coded the rotation and position of the custom player model and render it in post event, and set the bodyparts.showModel to false for the parts that you want to render in post event.

Edited by poopoodice
Posted
2 hours ago, poopoodice said:

I haven't find the best solution yet, but any changes (e.g. rotation) made in the pre event will not work because in render method which gets call after pre event resets model's rotation angle and stuff. What I do is hard coded the rotation and position of the custom player model and render it in post event, and set the bodyparts.showModel to false for the parts that you want to render in post event.

could I see your classes to see how you did it? the custom model render thing to override the vanilla player model

the custom model class and how did you register that custom model....

i'm not very good understanding render stuff XD

Posted (edited)

Models does not need to be registered, only renderers

And what I mean by a custom player model is basically the copy and paste of the player model with some rotations and translations... anyways here's the code

https://bitbucket.org/poopoodice/ava/src/master/src/main/java/poopoodice/ava/misc/AVATPRenderer.java

Just want you to know the code is extremely terrible and should not be an example of doing anything.

To explain a bit more: I did not replace anything in vanilla rendering (except the layers, you can't "hide" them). Instead, I hide them and render my own model. I hope there's an easier way to do this but I haven't find anything yet.

You may find this more helpful

 

Edited by poopoodice
Posted
5 hours ago, poopoodice said:

Models does not need to be registered, only renderers

And what I mean by a custom player model is basically the copy and paste of the player model with some rotations and translations... anyways here's the code

https://bitbucket.org/poopoodice/ava/src/master/src/main/java/poopoodice/ava/misc/AVATPRenderer.java

Just want you to know the code is extremely terrible and should not be an example of doing anything.

To explain a bit more: I did not replace anything in vanilla rendering (except the layers, you can't "hide" them). Instead, I hide them and render my own model. I hope there's an easier way to do this but I haven't find anything yet.

You may find this more helpful

 

thx for the help, at least i'm doing some progress now XD

 

 

 


(actual event method), this is the first attempt XD:

Quote

@SubscribeEvent        
    public static void holdGunPost(RenderPlayerEvent.Post event)
    {
        PlayerEntity player = event.getPlayer();
        PlayerRenderer render = event.getRenderer();
        PlayerModel<AbstractClientPlayerEntity> model = render.getEntityModel();
        
        if(player != null&&player.getHeldItemMainhand().getItem() instanceof GunBase){
            ModelRenderer arm = model.bipedRightArm;
            arm.rotateAngleX=90;
            //model.swimAnimation=0;
            
            MatrixStack matrix = event.getMatrixStack();
            IVertexBuilder buffer = event.getBuffers().getBuffer(model.getRenderType(((AbstractClientPlayerEntity) player).getLocationSkin()));
            int light = event.getLight();
            int texture = OverlayTexture.NO_OVERLAY;
            
            model.bipedRightArm.copyModelAngles(arm);
            model.bipedRightArm.showModel=true;    
            arm.render(matrix, buffer, light, texture);;
        }
    }

 

2020-06-23_10.56.52.png

Posted
39 minutes ago, poopoodice said:

It took me really long to adjust the rotations angles and points, btw you can run it in debug mode so you dont have to relaunch the game everytime you make any changes

 

thx for the debug tip, the rotation and pose are pretty much done, but how do I make the gun Item follow the new rendered arm position/rotation?

 

2020-06-23_14.35.44.png

Posted
6 hours ago, sciwhiz12 said:

If the gun model will not need to move dynamically and the gun model is defined using model JSONs, use item display transforms through the "display" option (see MC wiki article on Item Models). I recommend using a program like Blockbench to edit the model's display transforms.

you can but the problem is if he want the hand to keep the rotation angle (no swinging), he will need to access the layers and replace then with his own layers, like I did.

Posted
13 hours ago, poopoodice said:

you can but the problem is if he want the hand to keep the rotation angle (no swinging), he will need to access the layers and replace then with his own layers, like I did.

Exactly, I want the gun to follow the body's YAW and the head's PITCH(removing the walking bobbing animation) to match the Arms Pos./Rot.

How could I do that exactly? Is there a wiki or example code somewhere?

2020-06-24_12.04.43.png

2020-06-24_12.06.06.png

Posted
14 hours ago, poopoodice said:

The link I sent already include that except the rotation pitch

Sorry, I'm trying to understand the layer code stuff, but a bit lost, it's the first time I "try" to work with layers.

 

Could you kindly show me the classes and highlight the code that makes posible to modify position and rotation of the item in the player's hand in third person view? (just the item rendering, I got the player model rendering fine for now).... that way I could study how does the layer code work etc etc.... 

 

I'm looking everywhere to find a solution but i'm not getting any progress :c

 

 

This is my actual rendering code, just in case someone wants to see it:

Quote

    @SubscribeEvent        
    public static void renderHeldGun(RenderPlayerEvent.Pre event)
    {
        
        
        PlayerEntity player = event.getPlayer();
        PlayerRenderer render = event.getRenderer();
        PlayerModel<AbstractClientPlayerEntity> model = render.getEntityModel();

        
        
        if(player != null&&player.getHeldItemMainhand().getItem() instanceof GunBase){
            GunBase gun = (GunBase) player.getHeldItemMainhand().getItem();
            
            
            if(gun.isAiming())
            {
                model.bipedLeftArm.showModel=false;    
            }
            else
            {
                model.bipedLeftArm.showModel=true;    
            }

            model.bipedRightArm.showModel=false;
            
        }
    }
    
    @SubscribeEvent        
    public static void holdGunPost(RenderPlayerEvent.Post event)
    {
        PlayerEntity player = event.getPlayer();
        PlayerModel<AbstractClientPlayerEntity> model = event.getRenderer().getEntityModel();
        
        if(player != null&&player.getHeldItemMainhand().getItem() instanceof GunBase){
            GunBase gun = (GunBase) player.getHeldItemMainhand().getItem();
            if(gun.isAiming())
            {
                renderArmModelIdleHoldingPistolAiming(model, player, event);
            }
            else
            {
                renderArmModelIdleHoldingPistol(model, player, event);
            }
            
        }
    }
    
    
    
    
    
    
    private static void renderArmModelIdleHoldingPistol(PlayerModel<AbstractClientPlayerEntity> model, PlayerEntity player, RenderPlayerEvent event)
    {
        MatrixStack matrix = event.getMatrixStack();
        IVertexBuilder buffer = event.getBuffers().getBuffer(model.getRenderType(((AbstractClientPlayerEntity) player).getLocationSkin()));
        int light = event.getLight();
        int texture = OverlayTexture.NO_OVERLAY;
        
        model.bipedRightArm.rotationPointX = -MathHelper.cos((float) Math.toRadians(player.renderYawOffset)) * 5.5F;
        model.bipedRightArm.rotationPointY = player.isCrouching() ? 17.5F : 20.5F;
        model.bipedRightArm.rotationPointZ = -MathHelper.sin((float) Math.toRadians(player.renderYawOffset)) * 5.5F;
        model.bipedRightArm.rotateAngleX = -1.6F - (player.rotationPitch/90)*1.2F; //-3.0F > -1.65F > -0.0F;    
        model.bipedRightArm.rotateAngleY =  (float) -Math.toRadians(player.renderYawOffset) + 3.2F + -Math.abs((player.rotationPitch/90))*-0.05F;
        model.bipedRightArm.rotateAngleZ = 0.0F;
        
        
        model.bipedRightArm.showModel=true;    
        model.bipedRightArm.render(matrix, buffer, light, texture);
    }
    
    private static void renderArmModelIdleHoldingPistolAiming(PlayerModel<AbstractClientPlayerEntity> model, PlayerEntity player, RenderPlayerEvent event)
    {
        MatrixStack matrix = event.getMatrixStack();
        IVertexBuilder buffer = event.getBuffers().getBuffer(model.getRenderType(((AbstractClientPlayerEntity) player).getLocationSkin()));
        int light = event.getLight();
        int texture = OverlayTexture.NO_OVERLAY;
        
        model.bipedRightArm.rotationPointX = -MathHelper.cos((float) Math.toRadians(player.renderYawOffset)) * 5.5F;
        model.bipedRightArm.rotationPointY = player.isCrouching() ? 17.5F : 20.5F;
        model.bipedRightArm.rotationPointZ = -MathHelper.sin((float) Math.toRadians(player.renderYawOffset)) * 5.5F;
        model.bipedRightArm.rotateAngleX = -1.6F - (player.rotationPitch/90)*1.2F; //-3.0F > -1.65F > -0.0F;    
        model.bipedRightArm.rotateAngleY =  (float) -Math.toRadians(player.renderYawOffset) + 3.5F + -Math.abs((player.rotationPitch/90))*-0.6F;
        model.bipedRightArm.rotateAngleZ = 0.0F;
        
        model.bipedLeftArm.rotationPointX = -MathHelper.cos((float) Math.toRadians(player.renderYawOffset)) * -5.5F;
        model.bipedLeftArm.rotationPointY = player.isCrouching() ? 17.5F : 20.5F;
        model.bipedLeftArm.rotationPointZ = -MathHelper.sin((float) Math.toRadians(player.renderYawOffset)) * -5.5F;
        model.bipedLeftArm.rotateAngleX = -1.7F - (player.rotationPitch/90)*1.2F; //-3.0F > -1.65F > -0.0F;    
        model.bipedLeftArm.rotateAngleY =  (float) -Math.toRadians(player.renderYawOffset) - 3.5F + -Math.abs((player.rotationPitch/90))*0.6F;
        model.bipedLeftArm.rotateAngleZ = 0.0F;

        
        model.bipedRightArm.showModel=true;    
        model.bipedLeftArm.showModel=true;    
        model.bipedRightArm.render(matrix, buffer, light, texture);
        model.bipedLeftArm.render(matrix, buffer, light, texture);
    }

 

Posted (edited)
11 hours ago, poopoodice said:

I used reflection to replace the player model's layers and do the rotations and translations in the replaced render method

I found this website with a bunch of PlayerRenderEvent examples and.... LMAO..... it only took 1 CODE LINE to make the player render as I wanted when it was holding the gun. WEBSITE: https://www.programcreek.com/java-api-examples/?api=net.minecraftforge.client.event.RenderPlayerEvent

 

Literally, just this line of code:

Quote

event.getRenderer().getEntityModel().rightArmPose=ArmPose.BOW_AND_ARROW;

 

making the arm and the item copy the render when the player aims with an arrow, the result is just perfect :D

 

tomorrow i'll try to render the left arm as normal, which will be easy I guess.

But now I can finally progress with this thing done XD

 

Footage of the Gun with proper rendering:

https://gyazo.com/de87cfcb79dea7b565013077cb77bb04

 

thx for the help, I guess this topic is now solved :P

Edited by xanderindalzone

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.

Announcements



×
×
  • Create New...

Important Information

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