How do I add Target in my PlayerMoveEvent? Please Help!

Discussion in 'Plugin Development' started by FlashTheDev, Jul 28, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    FlashTheDev

    You know how you normally do Bukkit.getServer().getOnlinePlayers(args[0]); to get target. Well I can't find out how to do that in an Event. Please Help!

    Code:
    package me.flashthedev.freeze;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    public class Freeze implements CommandExecutor, Listener {
      
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player target =
          
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
          
            return false;
        }
    }
     
    Last edited by a moderator: Jul 29, 2017
  2. Offline

    RRGamingZ

    Judging by what you have there, i think you're trying to freeze a player you specify in a command...
    What i usually do is (in the onCommand method) check if the player args[0] is null, if not then add the online player to an arraylist. If he was already added, i remove him from the array list.

    Then, all the players in the arraylist will be subjected to event.setCancelled(true);.
    Just gonna give a little code here:

    *Remember to create an arraylist beforehand*
    onCommand:
    //Instanceof checks, permission checks, null check etc {
    if (cmd.getName().equalsIgnoreCase("freeze")) {
    if (args.length < 0) {
    //args to player
    if (!this.myarraylist.contains(p.getName())) {
    // add to arraylist, send message to command sender etc
    return true;
    } else
    {
    // remove from arralyist, send message to command sender etc
    return true;
    }

    event:
    Player p = event.getPlayer();
    if (myarraylist.contains(p.getName())) {
    e.setCancelled(true);
     
  3. @RRGaming true but using event.setCancelled is a rather poor way to freeze. There's another method that is better called event.setTo(event.getFrom); this method is better than the last but if you still want the player's heads moving around. While they're frozen there's a method for that too.
     
  4. Offline

    Machine Maker

    @FlashTheDev
    1. Are you trying to assign the target variable to the player that moved? Most events that extend PlayerEvent (all events that have anything to do with a player) have a method called getPlayer() which returns the player associated with the event, in this case, the player that moved.

    2. To prevent him from moving, do what @Mindlessmink suggested and set the to location to the from location.
    Code:
    event.setTo(event.getFrom());
    3. If you want the player's head to be able to move, you'll need to create a new location that for the X, Y, and Z values you used the values from event.getFrom(), but you want to keep the values from event.getTo() from the pitch and yaw.
    Code:
    Location newTo = new Location(event.getTo().getWorld(), event.getFrom().getX(), event.getFrom().getY(), event.getFrom().getZ(), event.getTo().getYaw(), event.getTo().getPitch());
    event.setTo(newTo);
     
  5. Offline

    RRGamingZ

    Oh, did not know that. I usually use set speed to 0 or cancel movement... Thanks anyways
     
  6. @X1machinemaker1X
    Simpler point 3:
    Code:java
    1. event.setTo(event.getFrom().setDirection(event.getTo().getDirection()))
     
Thread Status:
Not open for further replies.

Share This Page