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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I use Bisect-Hosting, and I host a relatively modded server.  There is a mod I desperately want to have in a server. https://www.curseforge.com/minecraft/mc-mods/minehoplite This is MineHop. It's a mod that replicates the movement capabilities seen in Source Engine games, such as Half-Life, and such.  https://youtu.be/SbtLo7VbOvk - A video explaining the mod, if anyone is interested.  It is a clientside mod, meaning whoever is using it can change anything about the mod that they want, with no restrictions, even when they join a server with the same mod. They can change it to where they can go infinitely fast, or do some other overpowered thing. I don't want that to happen. So I just want to know if there is some way to force the SERVER'S configuration file, onto whoever is joining.  I'm not very savvy with all this modding stuff. There are two config files: minehop-common.txt, and minehop.txt I don't really know much about how each are different. I just know what the commands relating to acceleration and stuff mean.    
    • My journey into crypto trading began tentatively, with me dipping my toes into the waters by purchasing my first Bitcoin through a seasoned trader. With an initial investment of $5,000, I watched as my investment grew, proving to be both fruitful and lucrative. Encouraged by this success, I decided to increase my investment to $150,000, eager to capitalize on the growing popularity of cryptocurrency, However, as cryptocurrency gained mainstream attention, so too did the number of self-proclaimed "experts" in the field. Suddenly, everyone seemed to be a crypto guru, and more and more people were eager to jump on the bandwagon without fully understanding the intricacies of this complex world. With promises of quick and easy profits, these con artists preyed on the uninformed, luring them into schemes that often ended in disappointment and financial loss. Unfortunately, I fell victim to one such scheme. Seduced by the allure of easy money, I entrusted my hard-earned funds to a dubious trading platform, granting them access to my accounts in the hopes of seeing my investment grow. For a brief period, everything seemed to be going according to plan, with regular withdrawals and promising returns on my investment. However, my hopes were soon dashed when, without warning, communication from the platform ceased, and my Bitcoin holdings vanished into thin air. Feeling helpless and betrayed, I confided in a family member about my predicament. They listened sympathetically and offered a glimmer of hope in the form of a recommendation for Wizard Web Recovery. Intrigued by the possibility of reclaiming what I had lost, I decided to explore this option further. From the moment I reached out to Wizard Web Recovery, I was met with professionalism and empathy. They took the time to understand my situation and reassured me that I was not alone in my plight. With their guidance, I embarked on a journey to reclaim what was rightfully mine. Wizard Web Recovery's expertise and dedication were evident from the start. They meticulously analyzed the details of my case, uncovering crucial evidence that would prove invaluable in our quest for justice. With each step forward, they kept me informed and empowered, instilling in me a newfound sense of hope and determination. Through their tireless efforts and unwavering support, Wizard Web Recovery succeeded in recovering my lost Bitcoin holdings. It was a moment of triumph and relief, knowing that justice had been served and that I could finally put this chapter behind me. In conclusion, My experience with Wizard Web Recovery  was nothing short of transformative. Their professionalism, expertise, and unwavering commitment to their clients set them apart as true leaders in the field of cryptocurrency recovery. I am forever grateful for their assistance and would highly recommend their services to anyone in need of help navigating the treacherous waters of cryptocurrency scams. 
    • Ok so: Two things to note: It got stuck due to my dimension type. It was previously the same as the overworld dimension tpye but after changing it , it didn't freeze during spawn generation. ALSO, APPARENTLY, the way I'm doing things, the game can't have two extremely-rich dimensions or it will make the new chunk generation be veeery VEEERY slow. I'm doing the dimension file genreation all in the data generation step now, so it's all good. Mostly. If anybody has any tips regarding how can i more efficently generate a biome-rich dimension, im all ears.
    • https://mclo.gs/qTo3bUE  
    • Check for the mods ingame - create a new world in creative mod and check the creative inventory  
  • Topics

×
×
  • Create New...

Important Information

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