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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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