Jump to content

[1.9] Fall event does not cancel, and player motionY is not changed.


oneofthem999

Recommended Posts

Hello everyone.  So in my attempts to learn how to build new blocks, I came up with the Spring and SpringBoard class.  The idea is that when you fall on this custom block (the Spring Block), fall damage would be negated, and you will shoot up in the air the distance you fell.  In other words, it would act like a slime block, but your momentum would be conserved.  However, when I jump on the Spring block, neither the event is canceled, nor is the player sent to the skies. 

 

Here is the Spring class, which contains a constructor for the Spring Block.

 

public class Spring extends Block
{
    //Constructor
    public Spring()
    {
        super(Material.iron);
        this.setUnlocalizedName("spring");
        this.setCreativeTab(CreativeTabs.tabBlock);
        this.setResistance(2.0F);
        this.setHardness(5.0F);
        this.setLightLevel(0.3F);
    }
}

 

Here's the SpringBoard class, which contains the method containing the fall event.  This method should, if the player lands on a Spring Block, cancel the falling event, so the player takes no damage.  It should then send the player upwards by the distance the player originally fell.

 

public class SpringBoard
{
    /**
     * This method will launch the player up in the air based on the distance fell.
     */
    @SubscribeEvent
    public void goSpringBoard(LivingFallEvent event)
    {
        //Only run code on client-side
        if (!event.getEntity().getEntityWorld().isRemote)
        {
            return;
        }

        //Check if entity that fell is a player
        if (event.getEntity() instanceof EntityPlayer)
        {
            EntityPlayer player = (EntityPlayer) event.getEntityLiving();

            //Send a message of what block player fell on.
            String stuff = event.getEntity().getEntityWorld().getBlockState(event.getEntityLiving().getPosition().add(0, -1, 0)).getBlock().getLocalizedName();
            player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "You fell on a " + stuff));

            //Check if the block that the player fell on is a Spring block
            if (player.worldObj.getBlockState(event.getEntityLiving().getPosition().add(0, -1, 0)).getBlock() !=
                    Main.spring)
            {
                player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "I now return."));
                return;
            }

            else if (player.worldObj.getBlockState(event.getEntityLiving().getPosition().add(0, -1, 0)).getBlock() ==
                    Main.spring)
            {
                player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "This is a spring block!"));

                //Stop fall damage
                event.setCanceled(true);

                //Send player upward for distance of fall
                player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "" + event.getDamageMultiplier()
                        + " " + event.getDistance()));
                event.getEntity().motionY = event.getDistance();
            }
        }
    }
}

 

Finally, here's the code for the main file, though I'm not sure if it's needed.  I am aware that the registry method I am using is depreciated and outdated, but I thought it would be acceptable for not, since I'm just trying to get a baring on how the creation of new blocks is done.

 

@Mod(modid = Main.MODID, version = Main.VERSION)
public class Main
{
    public static final String MODID = "BlocksMods";
    public static final String VERSION = "1.0";
    public static Block enderBlock;
    public static Block spring;
    public static Item enderIngot;

    @EventHandler
    public static void init(FMLInitializationEvent event)
    {
        enderBlock = new EnderBlock();
        enderIngot = new EnderIngot();
        spring = new Spring();

        GameRegistry.registerBlock(enderBlock, "enderBlock");
        GameRegistry.registerItem(enderIngot, "Ender Ingot");
        GameRegistry.registerBlock(spring, "sp");

        //Events
        MinecraftForge.EVENT_BUS.register(new SpringBoard());
    }
}

 

Thank you for your time.

Link to comment
Share on other sites

!event.getEntity().getEntityWorld().isRemote

Is true for server.

You should run fall events on server so basically what I am saying: (at start)

if (event.getEntity().getEntityWorld().isRemote) return;

 

This might not be the whole problem (part of it). Any changes?

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I fixed the problem actually.  It had to do with by comparison method for check to see if the player landed on the Spring Block.  Here's the new code:

 

public class SpringBoard
{
    /**
     * This method will launch the player up in the air based on the distance fell.  Accurate to up to 50 blocks.
     */
    @SubscribeEvent
    public void goSpringBoard(LivingFallEvent event)
    {
        //Check if entity is player
        if (event.getEntityLiving() instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) event.getEntityLiving();

            //Check if event takes place in client-side
            if (!player.getEntityWorld().isRemote)
            {
                //Store position of landing block
                int x = player.getPosition().getX();
                int y = player.getPosition().getY() - 1;
                int z = player.getPosition().getZ();

                //Check if landing block is a spring block
                if (player.getEntityWorld().getBlockState(new BlockPos(x, y, z)).getBlock() instanceof Spring)
                {
                    //Using following approximation for finding velocity right before player hits spring block
                    double acceleration = 0.094;
                    double drag = 0.0028;
                    double velocity = Math.sqrt(2 * acceleration * event.getDistance());
                    velocity = velocity * (1 + drag * event.getDistance());

                    //Propel player upward with that velocity
                    player.motionY = velocity;
                    player.velocityChanged = true;
                    event.setCanceled(true);
                }
            }
        }
    }
}

 

However, this spring board only has an upper limit of 50 blocks.  After that, no matter how high I jump, I can only return to a height of 50 blocks from the spring.  This is curious, since I checked the "velocity" and "motionY" (they are equal) variables right when I land from a height of 150 blocks, and they are much higher than when I jump from a height of 50.  However, it always sends be back upwards 50 blocks, if I jump from a height > 50.

 

This is fine for me, as I can always just not use the spring board for heights > 50, but it would be nice to know how I can improve my code.

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



×
×
  • Create New...

Important Information

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