Set entity gravity

For a plugin I’m writing I’d like to fine-tune the players gravity, so I can for example set it to let’s say 0.75, so the player can jump fences.
Manipulating the gravity in such a way would also require the FallDamage to be corrected (multiplied with gravity?).

So what would be the best way to implement it with Sponge as it is right now?

Of course I could fix the player-speed every tick by tracking the y delta, and moving the player back by a small amount, but I don’t think that would run very smooth.

SethBling has actually done exactly what i want but as bukkit plugin

Would it be viable to add something like this to Sponge itself? would other plugin developers event find a use for it?

Its done by a simple task. I just decompiled the jar provided in that video’s link.

You can try using the Sethbling’s method, just adapt the code to Sponge, I’ve done it using Mixin to offer better performances but it’s still difficult to implement and a bit buggy sometimes. For now my sources are under a private license but I will try to help you if you need some help.
Fixing the player speed while tracking the y delta works fine for players on my side but is a bit buggy for mobs. At least for players it’s smooth.
I will try to recover some pieces of my code to help you if you want.
I would really really love that SpongeAPI supports custom gravity (and maybe other movement modifications) per entity.

I don’t really have the knowledge to decompile the bukkit plugin and I’ve never even looked at the Mixin system, so since Yeregorix seems to have no problem I guess I’ll just go with manipulating the y delta.

I just wanted to see if someone could come up with a better idea.

I’ll report back once I’ve played around with it for a bit :slight_smile:

From my point of view the best idea would be to add the functionality in SpongeAPI :smile:

1 Like

Ok so I played around and noticed, the MoveEntityEvent produced strange results for me.

Running

public static void tickGravity(Entity ent) {
  Vector3d newv = ent.getVelocity();
  UUID uuid = ent.getUniqueId();
        
  if (gravityModifiers.containsKey(uuid) && velocities.containsKey(uuid) && !ent.isOnGround() && !ent.getVehicle().isPresent()) {
    Vector3d oldv = velocities.get(uuid);
    Vector3d d = newv.sub(oldv);
	        
    double gravity = gravityModifiers.get(uuid);
            
    double dy = d.getY();
    if (Math.abs(dy)>0.01) {
      newv = new Vector3d(newv.getX(), oldv.getY()+dy*gravity, newv.getZ());
      ent.setVelocity(newv);
    }
  }
  velocities.put(uuid, newv);
}

Worked pretty good scheduled every half tick, but setting a gravity of 0.5 allows me to jump 3x the hight and higher gravity values result in lower gravity :thinking:

Well since it’s not too important I think I’ll leave it at that and hope Sponge will get support for that :slight_smile:

I think that you should not apply your gravity modifier when the acceleration is upward. Try replacing Math.abs(dy)>0.01 with just dy<-0.01.

wont I just fall vanilla then after hitting the peak of the jump? doesn’t really sound right

ed: i only have that in because it for some reason freaked out around 0, accellerating sideways and stuff

Here is my code using Mixin https://pastebin.com/CDa9MytS, https://pastebin.com/wSxr4U7N. It’s not perfect but I hope it can help you.