Jump to content

[1.12.2] Make Custom Ridden Mob Jump


Icedice9

Recommended Posts

Hello all!

 

I am trying to make a custom rideable mob similar to the pig called the Elytra Pig. When the player presses the space bar, I want this custom mob to jump. I've looked at the horse code for how they jump, but horses require "jump power," represented by the jump bar. I have also explored using the forge "LivingJumpEvent" which successfully detects when the player has jumped. However, when the player is riding an entity, the LivingJumpEvent does not fire. Here is my code for the event for reference:

//called when an entity jumps
@SubscribeEvent
public void livingJumpEvent(LivingJumpEvent event)
{
	EntityLivingBase jumpingEntity = event.getEntityLiving();
  	
	if (jumpingEntity instanceof EntityPlayer && jumpingEntity.world.isRemote)
	{
		//TODO
		System.out.println("Player Jumped!"); //this correctly outputs, but only when the player is not riding my mob
		EntityPlayer player = (EntityPlayer) jumpingEntity;
		if (player.isRiding() && player.getRidingEntity() instanceof EntityElytraPig)
		{
			//TODO
			System.out.println("Player is riding ElytraPig!");
			EntityElytraPig elytraPig = (EntityElytraPig) player.getRidingEntity();
          
        	if (!elytraPig.isElytraPigJumping() && !elytraPig.onGround)
			{
				//TODO
	    		System.out.println("Elytra pig is flying!");
	            //change to make go higher
	            elytraPig.motionY = 0.7F * 1.0F;

				if (elytraPig.isPotionActive(MobEffects.JUMP_BOOST))
				{
					elytraPig.motionY += (double)((float)(elytraPig.getActivePotionEffect(MobEffects.JUMP_BOOST).getAmplifier() + 1) * 0.1F);
                }

				elytraPig.setElytraPigJumping(true);
				elytraPig.isAirBorne = true;

				if (elytraPig.moveForward > 0.0F)
                {
					float f = MathHelper.sin(elytraPig.rotationYaw * 0.017453292F);
					float f1 = MathHelper.cos(elytraPig.rotationYaw * 0.017453292F);
					elytraPig.motionX += (double)(-0.4F * f);
					elytraPig.motionZ += (double)(0.4F * f1);
					elytraPig.playSound(SoundEvents.ENTITY_HORSE_JUMP, 0.4F, 1.0F);
				}
			}
			 
	        elytraPig.jumpMovementFactor = elytraPig.getAIMoveSpeed() * 0.1F;
        }
    }
}

 

Anyone have any other ideas I can try? Mostly, I need an event that can test if the player hits the spacebar while riding my custom entity.

Thanks for any and all help!

Link to comment
Share on other sites

8 minutes ago, Icedice9 said:

but horses require "jump power,"

Make "jump power" a constant and use the jumping code, aka modify the code to fit your means.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Sweet! It's almost working! Now it jumps, but only once when "!this.onGround." How can I make it only jump when the player hits the spacebar? The player needs to set the jump power to 1.0F when they jump.

Here is my code in the travel method of my entity class:

public void travel(float p_191986_1_, float p_191986_2_, float p_191986_3_)
    {
        Entity entity = this.getPassengers().isEmpty() ? null : (Entity)this.getPassengers().get(0);

        if (this.isBeingRidden() && this.canBeSteered())
        {
            //...more code here...

            if (this.jumpPower > 0.0F && !this.isElytraPigJumping() && !this.onGround)
            {
            	//TODO set to make stronger/less strong
                this.motionY = 1.0F * (double)this.jumpPower;

                if (this.isPotionActive(MobEffects.JUMP_BOOST))
                {
                    this.motionY += (double)((float)(this.getActivePotionEffect(MobEffects.JUMP_BOOST).getAmplifier() + 1) * 0.1F);
                }

                this.setElytraPigJumping(true);

                if (p_191986_3_ > 0.0F)
                {
                    float f = MathHelper.sin(this.rotationYaw * 0.017453292F);
                    float f1 = MathHelper.cos(this.rotationYaw * 0.017453292F);
                    this.motionX += (double)(-0.4F * f * this.jumpPower);
                    this.motionZ += (double)(0.4F * f1 * this.jumpPower);
                    this.playSound(SoundEvents.ENTITY_HORSE_JUMP, 0.4F, 1.0F);
                }
                
		//jumpPower set to 0.0 to avoid constant jumping
                this.jumpPower = 0.0F;
            }

            //...more code here..
            
            if (this.onGround)
            {
                this.jumpPower = 0.0F;
                this.setJumping(false);
            }

            //...more code here...
        }
        else
        {
            this.stepHeight = 0.5F;
            this.jumpMovementFactor = 0.02F;
            super.travel(p_191986_1_, p_191986_2_, p_191986_3_);
        }
    }

 

Edit: The horse’s setJump code is called inside the player entity, so I don’t think I can override/call that.

Edited by Icedice9
Link to comment
Share on other sites

32 minutes ago, Icedice9 said:

Edit: The horse’s setJump code is called inside the player entity, so I don’t think I can override/call that

Use a key press event and send a packet if the player is riding your entity and then make it jump.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Yes! It's working! Thank you for your help! One last thing. Is a packet necessary, or is the following code fine? It seems to be working, but is their a cleaner way to do it?

//detects when the player presses the spacebar
	private static Field KEYBIND_ARRAY = null;

	@SubscribeEvent (priority = EventPriority.LOWEST)
	public void onClientTick(TickEvent.PlayerTickEvent event) throws Exception//.ClientTickEvent event) throws Exception 
	{
		if(KEYBIND_ARRAY == null)
		{
			KEYBIND_ARRAY = KeyBinding.class.getDeclaredField("KEYBIND_ARRAY");
			KEYBIND_ARRAY.setAccessible(true);
		}
		if(event.phase.equals(Phase.END))
		{
			Map<String, KeyBinding> binds = (Map<String, KeyBinding>) KEYBIND_ARRAY.get(null);
	    	for (String bind : binds.keySet())
	    	{
				if(binds.get(bind).isKeyDown())
				{
					EntityPlayer player = event.player;
					if (bind.equals("key.jump") && player.isRiding() && player.getRidingEntity() instanceof EntityElytraPig)
					{
						EntityElytraPig elytraPig = (EntityElytraPig) player.getRidingEntity();
						elytraPig.setJumpPower(90); //this is max horse jump power
					}
					break;
				}
			}
		}
	}

 

I used the code I found here.

Link to comment
Share on other sites

45 minutes ago, Icedice9 said:

Is a packet necessary, or is the following code fine?

I'm not sure if it is necessary. However a good way to check would be to test your mod on a dedicated server. And it may not work all the time.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Turns out I do need to send a packet. I've sent packets before, but they were always server to client and involved setting static world values. Using the code I have below, how can I pass the value the client gives to the server side mob entity? Thanks so much again for the help!

 

Updated event handler code testing for player inputs (spacebar):

@SubscribeEvent (priority = EventPriority.LOWEST)
    @SideOnly(Side.CLIENT)
	public void onClientTick(TickEvent.PlayerTickEvent event) throws Exception//.ClientTickEvent event) throws Exception 
	{
		if(KEYBIND_ARRAY == null)
		{
			KEYBIND_ARRAY = KeyBinding.class.getDeclaredField("KEYBIND_ARRAY");
			KEYBIND_ARRAY.setAccessible(true);
		}
		
		EntityPlayer player = event.player;
		
		if (player.isRiding() && player.getRidingEntity() instanceof EntityElytraPig)
		{
			EntityElytraPig elytraPig = (EntityElytraPig) player.getRidingEntity();
			
			if(elytraPig.isDragon() && elytraPig.isElytraFlying() && elytraPig.canPassengerSteer() && event.phase.equals(Phase.END))
			{
				Map<String, KeyBinding> binds = (Map<String, KeyBinding>) KEYBIND_ARRAY.get(null);
		    	for (String bind : binds.keySet())
		    	{
					if(binds.get(bind).isKeyDown())
					{
						if (bind.equals("key.jump"))
						{
							//TODO How do I get the packet to connect to the player correctly?
							WhenPigsFly.INSTANCE.sendTo(new ClientKeyboardPacket(90), (EntityPlayerMP) event.player);
							elytraPig.setJumpPower(90);
						}
						break;
					}
				}
			}
		}
	}

 

Custom Client to Server Packet Code:

package whenpigsfly.util;

import java.util.ArrayList;
import java.util.HashMap;

import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

/**
 * Sends pig jump power to server.
 */
public class ClientKeyboardPacket implements IMessage 
{
	public ClientKeyboardPacket(){}
	
	int jumpPower;
	
	public ClientKeyboardPacket(int jumpPower) 
	{
		this.jumpPower = jumpPower;
	}

	@Override 
	public void toBytes(ByteBuf buf) 
	{
		buf.writeInt(jumpPower);
	}

	@Override 
	public void fromBytes(ByteBuf buf)
	{
		this.jumpPower = buf.readInt();
	}
	
	public int getJumpPower()
	{
		return jumpPower;
	}
	
	//this is what ClientKeyboardPacket does to the server (output comes from server)
	//The params of the IMessageHandler are <REQ, REPLY>, meaning that the first is the packet you are receiving, and the second is the packet you are returning. The returned packet can be used as a "response" from a sent packet.
	public static class JumpPowerHandler implements IMessageHandler<ClientKeyboardPacket, IMessage> 
	{
		@Override
		public IMessage onMessage(ClientKeyboardPacket message, MessageContext ctx)
		{
			//TODO this currently does nothing because I don't have an entity to give it to
			int jumpPower = message.getJumpPower();
			//response packet
			return null;
		}
	}
}

 

As you can see with my "TODO" lines in both files, the "jumpPower" value passed to the server has nowhere to go because I don't know how to get it to the right player/mob.

Edited by Icedice9
Link to comment
Share on other sites

The net message is received on the server side, so you can get the player that sent the message from the MessageContext argument
 

@Override
		public IMessage onMessage(ClientKeyboardPacket message, MessageContext ctx)
		{
			//TODO this currently does nothing because I don't have an entity to give it to
			int jumpPower = message.getJumpPower();
 
  			EntityPlayerMP player = ctx.getServerHandler().player;
			Entity riding = player.getRidingEntity();
  
			//response packet
			return null;
		}

This will get you the player, then the entity the player is riding.

EDIT:
AS for sending messages from client to server, sendTo is for server to client, but sendToServer is for client to server
 

//This would work if it is on the server to the client
WhenPigsFly.INSTANCE.sendTo(new ClientKeyboardPacket(90), (EntityPlayerMP) event.player);

//This is client to server
WhenPigsFly.INSTANCE.sendToServer(new ClientKeyboardPacket(90));

 

Edited by GeoffrySkio

Never trust the client.

Link to comment
Share on other sites

Oh, I totally goofed. The method for setting the jump power as found in the AbstractChestHorse is controlled completely by the client. No packet was needed, I just had to put the "@SideOnly(Side.CLIENT) " above the event handler code.

 

You all are amazing! Thanks again!

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello, Hi and I am making a forge 1.12.2 mod and also trying to get a custom model helmet item (I made a joker mask) and exported it to .json format and put it in the ~/namespace/models/item folder. Though, I'm struggling to get the item to render what I want it to be rendered like as I've positioned it in the 'head' window view in blockbench. How did you render out your model? I checked your code and it looks like no references to the actual model are being made. I'm new to this forge modding stuff (was a fabric modder) and I desperately need some help. Is there a way to standardly render out armor models made using blockbench? any methods that one can use? My model in JSON: ```json { "credit": "Made with Blockbench", "texture_size": [160, 160], "textures": { "0": "ydyrn:items/joker_mask_texture", "particle": "ydyrn:items/joker_mask_texture" }, "elements": [ { "from": [0, 0, 0], "to": [16, 16, 1], "faces": { "north": {"uv": [0, 1.7, 16, 14.2], "texture": "#0"}, "east": {"uv": [0, 4, 0.9, 5.6], "texture": "#0"}, "south": {"uv": [0, 5.8, 1.6, 7.4], "texture": "#0"}, "west": {"uv": [0, 1.9, 1.3, 3.5], "texture": "#0"}, "up": {"uv": [0, 3.4, 1.6, 4.2], "texture": "#0"}, "down": {"uv": [0, 9, 1.6, 9.7], "texture": "#0"} } }, { "from": [15.8, 11, 0.5], "to": [16.2, 13, 15], "faces": { "north": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "east": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "south": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "west": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "up": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"}, "down": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"} } }, { "from": [0.2, 11, 14.6], "to": [15.8, 13, 15], "faces": { "north": {"uv": [7.9, 7.9, 9.46, 8.1], "texture": "#0"}, "east": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "south": {"uv": [7.9, 7.9, 9.46, 8.1], "texture": "#0"}, "west": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "up": {"uv": [7.9, 7.9, 9.46, 7.94], "texture": "#0"}, "down": {"uv": [7.9, 7.9, 9.46, 7.94], "texture": "#0"} } }, { "from": [-0.2, 11, 0.5], "to": [0.2, 13, 15], "faces": { "north": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "east": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "south": {"uv": [7.9, 7.9, 7.94, 8.1], "texture": "#0"}, "west": {"uv": [7.9, 7.9, 9.35, 8.1], "texture": "#0"}, "up": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"}, "down": {"uv": [7.9, 7.9, 7.94, 9.35], "texture": "#0"} } } ], "display": { "thirdperson_righthand": { "translation": [-0.25, -2.75, 0], "scale": [0.3, 0.3, 0.3] }, "thirdperson_lefthand": { "translation": [-0.25, -2.75, 0], "scale": [0.3, 0.3, 0.3] }, "firstperson_righthand": { "rotation": [-9, 115, 15], "translation": [0, 1.25, -1.75], "scale": [0.5, 0.5, 0.5] }, "firstperson_lefthand": { "rotation": [-9, 115, 15], "translation": [0, 1.25, -1.75], "scale": [0.5, 0.5, 0.5] }, "ground": { "rotation": [40, 0, 0], "translation": [0, -2.5, 0], "scale": [0.5, 0.5, 0.5] }, "gui": { "rotation": [0, 180, 0] }, "head": { "translation": [0, 0, 0.25], "scale": [0.82773, 0.73594, 0.9] }, "fixed": { "translation": [0, 0, 8.75] } }, "groups": [ 0, { "name": "straps", "origin": [0, 0, 0], "color": 0, "children": [1, 2, 3] } ] } ```
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
    • In your config folder, delete the sophisticatedcore-common.toml file and test it again
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
  • Topics

×
×
  • Create New...

Important Information

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