Jump to content

Recommended Posts

Posted
I did a double jump code but it only works sometimes
Can someone tell me what I did wrong?
It works but not all the time.

 

private static HashMap<String, Integer> jumps = new HashMap<String, Integer>();
    private static int keyvalue;

    public static void jump(EntityPlayer p) {
        int x = p.getPosition().getX();
        int y = p.getPosition().getY();
        int z = p.getPosition().getZ();
        World world = p.getEntityWorld();
        BlockPos pos = new BlockPos(x, y - 1, z);
        if (world.getBlockState(pos).getBlock() instanceof BlockAir) {
            if (jumps.get(p.getName()) == 1 && p.motionY < 0.07 ) {
                p.setVelocity(0.0F, 1.0F, 0.0F);
                System.out.println("aaaa: " + jumps.get(p.getName()));
                jumps.put(p.getName(), jumps.get(p.getName()) + 1);
                System.out.println("bbbb: " + jumps.get(p.getName()));
            } else if(jumps.get(p.getName()) >= 2) {
                
            }
        }
        if (!(world.getBlockState(pos).getBlock() instanceof BlockAir)) {
            jumps.put(p.getName(), 0);
//            System.out.println("cccc: " + jumps.get(p.getName()));
        }
    }
    
    @SubscribeEvent
    public static void getKeyJump(LivingJumpEvent e) {
        if (e.getEntity() instanceof EntityPlayer) {
            EntityPlayer p = (EntityPlayer) e.getEntity();
            int key = Keyboard.getEventKey();
            keyvalue = key;
            jumps.put(p.getName(), 1);
        }
    }
    
    @SideOnly(Side.CLIENT)
    @SubscribeEvent
    public static void a(PlayerTickEvent e) {
        EntityPlayer p = (EntityPlayer) e.player;
        if (Keyboard.isKeyDown(keyvalue)) {
            jump(p);
        }
    }

Posted
3 hours ago, Niinnnnnn said:

int key = Keyboard.getEventKey();
keyvalue = key;

What is this? If you want to know what the jump key is you must use KeyBindings. They are all public.

 

3 hours ago, Niinnnnnn said:

@SideOnly(Side.CLIENT)
    @SubscribeEvent
    public static void a(PlayerTickEvent e) {
        EntityPlayer p = (EntityPlayer) e.player;
        if (Keyboard.isKeyDown(keyvalue)) {
            jump(p);
        }
    }

Don't use keyboard.isKeyDown, use KeyBinding#isKeyDown.

 

3 hours ago, Niinnnnnn said:

private static HashMap<String, Integer> jumps = new HashMap<String, Integer>();

There are so many things wrong with this:

  • You are leaking memory with this map since you never clean it up
  • Player names can and will change
  • Using strings as a key for a hashmap is a bad choice. Use a UUID
  • You do not need this map at all. Use capabilities.
  • This will persist between different saves since you never clean it.

Don't use a HashMap. Use capabilities when you need to store something per player.

 

Instead of all this tick/hashmap hackery use a KeyInputEvent to detect when the jump key is pressed and then perform the jump.

 

3 hours ago, Niinnnnnn said:

int x = p.getPosition().getX();
int y = p.getPosition().getY();
int z = p.getPosition().getZ();
BlockPos pos = new BlockPos(x, y - 1, z);

Why? You can do this in a much simplier way:

p.getPosition().down()

 

3 hours ago, Niinnnnnn said:

if (world.getBlockState(pos).getBlock() instanceof BlockAir) {

And what if the block isn't an instance of BlockAir but is supposed to behave just like air, like RailCraft's residual heat? Use World#isAirBlock.

 

3 hours ago, Niinnnnnn said:

 if (!(world.getBlockState(pos).getBlock() instanceof BlockAir)) {
            jumps.put(p.getName(), 0);

This will fire when the jump event fires which is probably not what you want. You probably want to check that Entity.onGround is true in some kind of tick event.

Also your double jump will only work if jumps[playername] == 1 yet you set it to 0 here thus making sure it won't work ever again. And you know why it won't work? Because when the tick event fires next and checks for the block below the player it is not going to be an air block. Thus it will set the jumps to 0 again even though you've jumped.

 

Ad for your issue - use the debugger to find out what is wrong. However I suspect that if you fix everything I've mentioned the issue will fix itself.

Posted
12 minutes ago, Niinnnnnn said:

I do not know why it turned up this.

Ok. Which part of what he said did you not understand this part 

 

16 hours ago, V0idWa1k3r said:

This will fire when the jump event fires which is probably not what you want. You probably want to check that Entity.onGround is true in some kind of tick event.

Also your double jump will only work if jumps[playername] == 1 yet you set it to 0 here thus making sure it won't work ever again. And you know why it won't work? Because when the tick event fires next and checks for the block below the player it is not going to be an air block. Thus it will set the jumps to 0 again even though you've jumped.

Or this part

16 hours ago, V0idWa1k3r said:

Ad for your issue - use the debugger to find out what is wrong. However I suspect that if you fix everything I've mentioned the issue will fix itself.

 

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

This will fire when the jump event fires which is probably not what you want. You probably want to check that Entity.onGround is true in some kind of tick event.

Also your double jump will only work if jumps[playername] == 1 yet you set it to 0 here thus making sure it won't work ever again. And you know why it won't work? Because when the tick event fires next and checks for the block below the player it is not going to be an air block. Thus it will set the jumps to 0 again even though you've jumped.

 

this

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.