xanderindalzone Posted June 22, 2020 Posted June 22, 2020 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; } } } Expand any help would be grateful :), thx Quote
poopoodice Posted June 22, 2020 Posted June 22, 2020 (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 June 22, 2020 by poopoodice Quote
xanderindalzone Posted June 22, 2020 Author Posted June 22, 2020 On 6/22/2020 at 8:19 PM, 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. Expand 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 Quote
poopoodice Posted June 23, 2020 Posted June 23, 2020 (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 June 23, 2020 by poopoodice Quote
xanderindalzone Posted June 23, 2020 Author Posted June 23, 2020 On 6/23/2020 at 3:22 AM, 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 Expand 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);; } } Expand Quote
poopoodice Posted June 23, 2020 Posted June 23, 2020 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 Quote
xanderindalzone Posted June 23, 2020 Author Posted June 23, 2020 On 6/23/2020 at 9:14 AM, 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 Expand 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? Quote
sciwhiz12 Posted June 23, 2020 Posted June 23, 2020 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. Quote
poopoodice Posted June 23, 2020 Posted June 23, 2020 On 6/23/2020 at 1:10 PM, 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. Expand 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. Quote
xanderindalzone Posted June 24, 2020 Author Posted June 24, 2020 On 6/23/2020 at 8:11 PM, 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. Expand 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? Quote
poopoodice Posted June 24, 2020 Posted June 24, 2020 (edited) The link I sent already include that except the rotation pitch Edited June 24, 2020 by poopoodice Quote
xanderindalzone Posted June 25, 2020 Author Posted June 25, 2020 On 6/24/2020 at 8:03 PM, poopoodice said: The link I sent already include that except the rotation pitch Expand 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); } Expand Quote
poopoodice Posted June 25, 2020 Posted June 25, 2020 I used reflection to replace the player model's layers and do the rotations and translations in the replaced render method Quote
xanderindalzone Posted June 25, 2020 Author Posted June 25, 2020 (edited) On 6/25/2020 at 10:48 AM, poopoodice said: I used reflection to replace the player model's layers and do the rotations and translations in the replaced render method Expand 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; Expand making the arm and the item copy the render when the player aims with an arrow, the result is just perfect 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 Edited June 25, 2020 by xanderindalzone Quote
Recommended Posts
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.