#cube gravity
1 messages · Page 1 of 1 (latest)
so i have
@EventHandler
public void onMove(final PlayerMoveEvent event) {
final UUID playerID = event.getPlayer().getUniqueId();
final Deque<Location> locations = this.lastLocations.computeIfAbsent(playerID, key -> new ArrayDeque<>());
if (locations.size() == 2) locations.poll();
locations.add(event.getTo());
}
public Vector getLastMoveVector(final UUID playerID) {
final Deque<Location> locations = this.lastLocations.get(playerID);
if (locations == null) return null;
final Location last = locations.poll();
final Location secondLast = locations.poll();
if (last == null || secondLast == null) return null;
final Vector vector = last.toVector().subtract(secondLast.toVector());
vector.setY(vector.getY() / 2);
locations.add(last);
locations.add(secondLast);
return vector;
}
to get vector of the player, then I use length() from that vector to setVelocity to my cube (slime)
it works great but the only issue is that when you are moving the cube forward and since I also have PlayerInteractEntityEvent#onRightClick() method I can give the cube additional Y which raises the cube in the air, the problem is that no matter how much you raise it the cube continues going forward instead of for example hitting the head of a player when falling down and staying behind
this is a problem because when another player kicks the cube, no matter how high the player that's in front will just block it even though its ex 5 blocks above him, no need to hit it or anything
I recorded it from the old plugin and the new one to demonstrate:
old https://i.imgur.com/udNSW2j.mp4
new https://i.imgur.com/MFwSDvq.mp4
old method for power was to update a HashMap with the player's uuid and sqrt of PlayerMoveEvent event.getFrom() - event.getTo()
well first off, you probably should not use the moveEvent like this
PlayerMoveEvent is extremely expensive
it gets called even if the player moves his mouse, from a quick look you don't need that functionality so you should definately add a check to see if the player even moved
unless im mistaken and you need the Yaw, Pitch updates
if thats the case you should still add some kind of rate limiting
I don't need yaw nor pitch in that event, I'm just getting the last 2 locations
I'm acquiring Player#getDirection() later on in the code