Jump to content

Recommended Posts

Posted

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!

Posted
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.

Posted
1 hour ago, Animefan8888 said:

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

I'll give it a shot and let you know what I find.

Posted (edited)

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
Posted
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.

Posted

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.

Posted
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.

Posted (edited)

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
Posted (edited)

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.

Posted

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!

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.