Plugin to invert the movement of a specified player

Discussion in 'Plugin Development' started by player11334share, Feb 7, 2020.

Thread Status:
Not open for further replies.
  1. This is all of my code. What it should be doing is essentially inverting the x and z movement of the specified player, instead what it does is just keep me trapped at (0 X, Y can still change ,0 Z). If I activate it anywhere else I just get teleported to (0,0). My guess is that it's something in the onMove() since I was not able to test the velocity part, unless jumping counts as a velocity. Please help, I have been confused for so long now.

    Edit: Just saying. It is probably really badly done and badly optimised but I also don't need it to be too optimised since the most it's ever going to be running on is 3 people on a relatively hefty server.

    Edit 2: Read common mistakes post. Shouldn't store players. Will try.
     

    Attached Files:

    Last edited: Feb 7, 2020
  2. Offline

    bowlerguy66

    @player11334share You're being teleported to 0, 0 because when you do fromX - toX you're getting a very small number as the from and to locations are very small in distance. Then you plug in the numbers to a new location object, which puts you at 0, 0.

    Also, thinking about the logistics of the problem, whenever a player moves and/or changes velocity, you're instantly inverting it which would mean that the players velocity would always be flipping back and forth which would ultimately keep you stuck in one spot.

    One way to solve this might be to make the player go in the opposite direction they're looking but that would still give you problems as a player who presses their backwards key would go forward in the way they are looking.

    Good luck!
     
  3. Code:
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerVelocityEvent;
    import org.bukkit.event.*;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.util.Vector;
    import java.util.UUID;
    
    /**
    * need to switch the following:
    *      PlayerMoveEvent
    */
    
    public class Inverter implements Listener {
        public UUID p;
        public Inverter(UUID p)
        {
            this.p = p;
        }
    
        @EventHandler(priority = EventPriority.HIGHEST)
        public boolean onMove(PlayerMoveEvent move) {
            if(move.getPlayer().getUniqueId().equals(p)) {
                Location from = move.getFrom();
                Location to = move.getTo();
                double xDiff = from.getX() - to.getX();
                double zDiff = from.getZ() - to.getZ();
                to.setX(from.getX()+xDiff);
                to.setZ(from.getZ()+zDiff);
                return true;
            }
            return false;
        }
    }
    
    This code does work. My player does have their movement changed and they do move ahead when I press S back when I press W, left when I press D etc. etc. etc. The reason I am making this post is because the movement is incredibly choppy. It feels like the event is being checked every 10 ticks. Also when I'm in the air and I press a button I fall down really really slowly and when I press no button I actually fall down at normal speeds. Please let me know if there is any way to improve this.
     
Thread Status:
Not open for further replies.

Share This Page