Switching players' movements

Discussion in 'Plugin Development' started by player11334share, Mar 23, 2020.

Thread Status:
Not open for further replies.
  1. Code:
    public class Inverter implements Listener {
        public UUID p;
        public UUID p2;
        public Inverter(UUID p, UUID p2)
        {
            this.p = p;
            this.p2 = p2;
        }
    
        @EventHandler(priority = EventPriority.HIGH)
        public boolean onMove(PlayerMoveEvent move) {
            move.setCancelled(true);
            UUID pM = move.getPlayer().getUniqueId();
            Location to = move.getTo();
            Location from = move.getFrom();
            double yDiff = to.getY()-from.getY();
            double xDiff = to.getX()-from.getX();
            double zDiff = to.getZ()-from.getZ();
            float pDiff = to.getPitch()-from.getPitch();
            float yaDiff = to.getYaw()-from.getYaw();
            if(pM.equals(p))
            {
                Location pl = Bukkit.getPlayer(p).getLocation();
                to = new Location(to.getWorld(), xDiff+pl.getX(),yDiff+pl.getY(),zDiff+pl.getZ(), yaDiff+pl.getYaw(),pDiff+pl.getPitch());
                new PlayerMoveEvent(Bukkit.getPlayer(p),pl,to);
                return true;
            }
            else
            {
                Location pl = Bukkit.getPlayer(p2).getLocation();
                to = new Location(to.getWorld(), xDiff+pl.getX(),yDiff+pl.getY(),zDiff+pl.getZ(), yaDiff+pl.getYaw(),pDiff+pl.getPitch());
                new PlayerMoveEvent(Bukkit.getPlayer(p2),pl,to);
                return true;
            }
        }
    Ok so. I've been trying to make a plugin that swaps player's movements-- if one player tries to move ahead instead of them moving ahead it'll move the other person ahead. I can tell already that this doesn't work, which it doesnt, cause it creates a infinite loop with the event bouncing back and forth between the players. I'm really unsure how I can fix this. The first thing that comes to mind is to get the player's input like wasd etc. but I'm relatively new to craftbukkit so I'm not sure how to if it is even possible.

    Edit 1:
    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;
    
    public class Inverter implements Listener {
        public UUID p;
        public UUID p2;
        public Inverter(UUID p, UUID p2)
        {
            this.p = p;
            this.p2 = p2;
        }
    
        @EventHandler(priority = EventPriority.HIGH)
        public boolean onMove(PlayerMoveEvent move) {
            UUID pM = move.getPlayer().getUniqueId();
            Location to = move.getTo();
            Location from = move.getFrom();
            double yDiff = to.getY()-from.getY();
            double xDiff = to.getX()-from.getX();
            double zDiff = to.getZ()-from.getZ();
            float pDiff = to.getPitch()-from.getPitch();
            float yaDiff = to.getYaw()-from.getYaw();
            to = new Location(to.getWorld(), from.getX(), yDiff + from.getY(), from.getZ());
            move.setTo(to);
            if(pM.equals(p))
            {
                Location pl = Bukkit.getPlayer(p).getLocation();
                to = new Location(to.getWorld(), xDiff+pl.getX(),yDiff+pl.getY(),zDiff+pl.getZ(), yaDiff+pl.getYaw(),pDiff+pl.getPitch());
                Bukkit.getPlayer(p).teleport(to);
            }
            else
            {
                Location pl = Bukkit.getPlayer(p2).getLocation();
                to = new Location(to.getWorld(), xDiff+pl.getX(),yDiff+pl.getY(),zDiff+pl.getZ(), yaDiff+pl.getYaw(),pDiff+pl.getPitch());
                Bukkit.getPlayer(p2).teleport(to);
            }
            return true;
        }
    }
    I changed it but, it doesn't work. Now instead it just locks each player's movement and pitch/yaw. I'm not really sure what to do.
     
    Last edited: Mar 23, 2020
  2. Offline

    wand555

    Not sure if it works, but maybe use vectors instead?
    Also why are you calling the same event inside the event? Obviously youre gonna get stuck
     
  3. The reason I was doing that was because I thought that was the easiest way to but actually you gave me a potentially really good idea. Maybe instead of creating a new event I can use a tp. It'll ideally be smooth enough since the player move event happens every tick or so that the player is moving, it would be called frequently enough that it shouldn't tp too much everytime keeping it smooth. As for vectors I'm not really too sure how to work with vectors and am not really smart enough to do any complex vector calculations. I'm going to try the tp, will get back soon.
     
  4. Offline

    wand555

    You can try a simple form with vectors first. Get p1 vector and apply it as p2's vector. But anyway good luck
     
  5. Offline

    Symphonic

    Do you mean velocity?
     
  6. Offline

    wand555

Thread Status:
Not open for further replies.

Share This Page