PlayerMoveEvent issue

Discussion in 'Plugin Development' started by Jackson12, Jun 29, 2015.

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

    Jackson12

    Hey, So i am trying to make a "PlayerMoveEvent", When the player moves it cancels the teleportation but, i can't seem to get this to work, here is my code.
    Code:
    if (e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) {
                if (patching.contains(e.getPlayer().getName())) {
                    patching.remove(e.getPlayer().getName());
                    e.getPlayer().sendMessage(ChatColor.RED + "Teleportatation Cancelled!");
                    return;
                }
            }
     
  2. Offline

    Xerox262

    e.getFrom().getBlockX() == e.getTo().getBlockX() How are they ever going to equal themselves if a player moves?
     
  3. Offline

    Jackson12

    I changed it to
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e){
            final Player p = e.getPlayer();
            if(playermove.contains(p)){
                p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Teleportation cancelled!");
                Bukkit.getScheduler().cancelAllTasks();
                playermove.remove(p);   
            }
        }
        }
    but, how would i make it to where players cant walk but can turn.
     
    Last edited: Jun 30, 2015
  4. @Jackson12 Check if the X, Y, or Z is changing. If so, cancel the event.
     
  5. Code:
    if(event.getFrom().getX() != event.getTo().getX() || event.getFrom().getZ() != event.getTo().getZ())
                {
                }

    @Jackson12
    You almost had it. This allows players to jump and move there mouse(look around).
     
  6. Offline

    Jackson12

    Ok, Thanks ill give it a try

    This is what i tryed, but when ever i type /spawn and try to move it stops me from moving while saying "teleportation cancelled!" until it teleports me to spawn then it stops.
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e){
            final Player p = e.getPlayer();
            if(e.getFrom().getX() != e.getTo().getX() || e.getFrom().getZ() != e.getTo().getZ()){
                if(playermove.contains(p)){
                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Teleportation cancelled!");
                    playermove.remove(p);
                    e.setCancelled(true);
                }
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  7. @Jackson12

    So what you're trying to do is create a command with a teleportation warmup. Like you run /spawn and you have to stay still before you are able to teleport?

    Can we see the /spawn code?
     
  8. Offline

    Jackson12

    Code:
    if (cmd.getName().equalsIgnoreCase("spawn")) {
                if (getConfig().getConfigurationSection("Spawn") == null) {
                        p.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
                        return true;
                }
                    final CommandSender s = sender;
                   
                spawn = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
                    int spawn = 5;
                    @Override
                    public void run() {
                        if(!(spawn == 0)){
                            playermove.add(p);
                            s.sendMessage(ChatColor.GRAY + "" + ChatColor.BOLD + String.valueOf(spawn));
                            p.getWorld().playSound(p.getLocation(), Sound.WOOD_CLICK, 1, 1);
                            spawn = spawn-1;
                        }else{
                            s.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "You have been teleported to spawn!");
                            int spawnX = getConfig().getInt("Spawn" + ".X");
                            int spawnY = getConfig().getInt("Spawn" + ".Y");
                            int spawnZ = getConfig().getInt("Spawn" + ".Z");
                            int spawnYaw = getConfig().getInt("Spawn" + ".Yaw");
                            int spawnPitch = getConfig().getInt("Spawn" + ".Pitch");
                            Object world = getConfig().get("Spawn" + ".World");
                          
                            Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitch);
                            p.teleport(spawn);
                            Bukkit.getScheduler().cancelAllTasks();
                        }
                    }
                }, 0L, 20L);
        }
     
  9. @Jackson12

    You set the player into the playermove list every second, meaning that if the player were to move, which removes him from the list, you add him back in within a second, causing them to stop moving again.

    In the beginning of your task, check to see if the player is in the list. If so, continue, if not, cancel the task.

    And also, using Bukkit.getScheduler().cancelAllTasks() will mess tons of stuff up if multiple people are teleporting at once, cause it will cancel all ongoing tasks.
     
  10. Offline

    Jackson12

    @HeyAwesomePeople
    How would i add them to the list without it keep adding them

    EDIT: I fixed it, But i have another problem.

    When the player moves it says "Teleportation cancelled!" but when they move and jump to cancel teleportation it says "Teleportation cancelled!" 3-5 times. How would i make it so it only says it once.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  11. Offline

    Ward1246

    Try this: Take the code that sends that message, and the other parts in that code, and move it to the top. Sometimes with my plugins, I have the same error, and it's because an event like that isn't close enough to the top, it doesn't get called first. If you can't move it or this doesn't work, I will have to look into this problem, that normally fixes it for me.
     
  12. Offline

    tytwining

    Please do not post something after you post. If you forget something there is a "edit" button. But what you could do is add a cooldown in some way...
     
  13. Offline

    Jackson12

    Ward1246 Didn't work, Thanks for trying though
     
  14. Offline

    Ward1246

    Huh, well I do have one last idea. Similar to what @tytwining said, try a cool down. In this case, I would try a scheduler. Look here for one that you want: http://wiki.bukkit.org/Scheduler_Programming Some on here, like a repeating task that cancels itself may be helpful. If not, then try a different method for a cool down.
     
  15. Use the report button instead of making an entire post about it. Keeps the thread on topic.
     
  16. Offline

    schwabfl

    They can equal themselves if the player changes the direction he is looking at, which is considered player movement
     
  17. Offline

    tytwining

    I didn't make an entire post about it, and who is the one turning it off topic? The person that commented on this topic to yell at me for changing the topic when you're the one making it off topic. I commented on it in my post because I had a comment to put on the topic, and why waste the moderator's time by hitting the report button?
     
  18. Because that's why there are moderators. To moderate.
    But you did...? Here it is:
    You are. That post you made was completely irrelevant to the problem the OP had.
    You started it, though. So there should be no reason to put the blame on me.

    This is the last time I'm going to be posting about this. End of discussion. Anyway, @Jackson12, like I said before:
    This should solve your problem.
     
  19. Offline

    Xerox262

    @schwabfl But that's when he's cancelling the teleportation
     
  20. Offline

    tytwining

    I believe that anyone can agree that if you wanted to tell me that then you should have sent me a private message to not make it a big public deal. As well as that, I gave them a tip for fixing their issue meaning that I did not make a post completely for that. Next time I will report that but you should not yell at me in the public about this and I am not blaming it on you I am just saying that you should not comment on it in the public thread. And if you don't answer to this then that's wonderful. I just want to make my arguments for everyone else.
     
Thread Status:
Not open for further replies.

Share This Page