Stopping the player from moving, how?

Discussion in 'Plugin Development' started by hayer, Mar 14, 2011.

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

    hayer

    Okey, so I'm listening to the..
    Code:
    pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this);
    pm.registerEvent(Event.Type.PLAYER_TELEPORT, this.playerListener, Event.Priority.Normal, this);
    
    and when those events get fired I do..
    Code:
    public void onPlayerMove(PlayerMoveEvent event)
    {
        if(!plugin.userIdMap.containsKey(event.getPlayer().getEntityId()))
        {
            event.getPlayer().teleportTo(event.getFrom());
        }
    }
    
    public void onPlayerTeleport(PlayerMoveEvent pme)
    {
        if(!plugin.userIdMap.containsKey(pme.getPlayer().getEntityId()))
        {
            pme.getPlayer().teleportTo(pme.getFrom());
        }
    }
    
    But this only overloads the server or something - I get ALOT of "info" from the server in the terminal and the user gets disconnected.

    I was looking at the "AnjoSecurity"-code and this is how he(or she?) does it..
     
  2. Offline

    Edward Hand

    event.setCancelled(true) would cause far less strain on both server and client.
     
  3. Offline

    hayer

    Tried, didn't seem to work.

    Using "event.setCancelled(true);" only makes it seems like your lagging, but you can still move.
     
  4. Offline

    MadMonkeyCo

    PHP:
    @Override
    public void onPlayerMove(PlayerMoveEvent event)
    {
        if(!
    plugin.userIdMap.containsKey(event.getPlayer().getEntityId()))
        {
            
    event.getPlayer().teleportTo(event.getFrom());
        }
    }
    @
    Override
    public void onPlayerTeleport(PlayerMoveEvent pme)
    {
        if(!
    plugin.userIdMap.containsKey(pme.getPlayer().getEntityId()))
        {
            
    pme.getPlayer().teleportTo(pme.getFrom());
        }
    }
    EDIT: This way setCancelled(true) will work.
    -The Mad Monkey
     
  5. Offline

    hayer


    You mean like this:
    Code:
    @Override
    public void onPlayerMove(PlayerMoveEvent event)
    {
        if(!plugin.userIdMap.containsKey(event.getPlayer().getEntityId()))
        {
            event.setCancelled(true);
            //event.getPlayer().teleportTo(event.getFrom());
        }
    }
    
    @Override
    public void onPlayerTeleport(PlayerMoveEvent pme)
    {
        if(!plugin.userIdMap.containsKey(pme.getPlayer().getEntityId()))
        {
            pme.setCancelled(true);
            //pme.getPlayer().teleportTo(pme.getFrom());
        }
    }
    

    Because that didn't work.. >_>
     
  6. Offline

    MadMonkeyCo

    Yes
    -The Mad Monkey
     
  7. Offline

    hayer

    @Override and Event.Priority.Highest

    still non-worky.. :/
     
  8. Offline

    MadMonkeyCo

    Can you post all your code? Or a link to it on PasteBin?
     
  9. Offline

    hayer

  10. Offline

    MadMonkeyCo

    Might it be that MySQL isn't working? I can't find anything wrong with the code except that calling System.out.prntln didn't work unless it was called in your main class (at least that's how it worked for me).
     
  11. Offline

    hayer

    MySQL is working - System.out.println can be called from anywhere. Atleast it works for me.. :s
     
  12. Offline

    MadMonkeyCo

    Have you tried it when the setCancelled is called with no conditions (no 'if' that is)?
     
  13. Offline

    hayer

    Code:
        @Override
        public void onPlayerMove(PlayerMoveEvent event)
        {
            System.out.println("** MOVE EVENT CANCELLED!");
            event.setCancelled(true);
        }
    
    Nope, aint working - just alot of "** MOVE EVENT CANCELLED!" messages..
     
  14. Offline

    Raphfrk

    The player move event is broken. I assume what is happening is that the player can move slower than normal.

    The problem is that Bukkit only sends some of the player move events. It will only send an event if the player had moved by more than 1/16 of a block since the last time it sent event. This means if the player is moving in smaller steps than that, then you only get a few of the events.

    This pull request would probably fix it (assuming that is the problem :) ).
     
  15. Offline

    hayer

    Well, yes - how to fix?_? New to this github stuff.
     
  16. Offline

    CypherX

    Try this:

    Code:
    public void onPlayerMove(PlayerMoveEvent event)
    {
        if(!plugin.userIdMap.containsKey(event.getPlayer().getEntityId()))
        {
            event.setCancelled(true);
            if (event.isCancelled())
                event.getPlayer().teleportTo(event.getFrom());
    
        }
    }
    
    public void onPlayerTeleport(PlayerMoveEvent pme)
    {
        if(!plugin.userIdMap.containsKey(pme.getPlayer().getEntityId()))
        {
            pme.setCancelled(true);
            if (event.isCancelled())
                pme.getPlayer().teleportTo(pme.getFrom());
        }
    }
    It works in the plugin I'm developing, although the player can still slightly move by constantly walking in a specific direction. This is most likely caused by what Raphfrk pointed out.
     
  17. Offline

    hayer

    This is the code in my listener
    Code:
    @Override
        public void onPlayerMove(PlayerMoveEvent event)
        {
            if(!plugin.isLoggedIn(event.getPlayer()))
            {
                event.setCancelled(true);
                if(event.isCancelled())
                    event.getPlayer().teleportTo(event.getFrom());
            }
        }
    
    this shows how well it.. works?_?

    Code:
    15:50:56 [SEVERE] java.io.IOException: Bad packet id 177
    15:50:56 [SEVERE]     at net.minecraft.server.Packet.b(SourceFile:117)
    15:50:56 [SEVERE]     at net.minecraft.server.NetworkManager.f(SourceFile:155)
    15:50:56 [SEVERE]     at net.minecraft.server.NetworkManager.c(SourceFile:9)
    15:50:56 [SEVERE]     at net.minecraft.server.NetworkReaderThread.run(SourceFile:62)
    
    Seems like the server just spams "teleport" until it figures out that it takes to much cpu/ram and just dc's the player.
     
  18. Offline

    CypherX

    Are you using the recommended builds of Bukkit and CraftBukkit?
     
  19. Offline

    hayer

    Yes.

    Still having some problems with it, anyone got a fix for it?

    Player seems to be lagging/jumping up and down all the time.
     
  20. Offline

    CypherX

    There's no way to fully stop their movement as far as I know, that jumping up and down thing is a side effect of teleporting them back to their original position when they move.
     
  21. Offline

    eltorqiro

    The lagging/jumping you are describing is because the client does not wait for permission from the server before it moves the avatar - it does the move immediately, sends the packet to the server, then when the server sends back a reply it can then revert the movement if it needs to. This happens with just about every modern FPS game, otherwise the client would have to for wait a round trip to the server every time somebody tried to move and thus there would be horrendous input lag on the client end.
     
  22. Offline

    Evenprime

    I have/had the same problem with cancelling move events with my plugin. Maybe I can share a bit of information I've gathered so far:

    1. Only cancelling a move event by setCancelled(true) is a very bad idea, because the client isn't automatically informed about the cancelled event.. The server will pretend the client didn't move, while the client will not know that the move was cancelled. This causes the server and client desync on the players position, it appears like the player is lagging.
    2. The only workaround I've found so far is to notice the client about the cancelled event by explicitly teleporting him to the "from"-position of the move event.
    3. To prevent the player from slowly moving in a direction despite being teleported back all the time, you should store the location where he should stay and always use that for the teleports, instead of the "from" location of the event.

    The main problem is that there seems to be no way to force the server into sending the players serverside position to the client without teleporting. I wished there were a player.synchronizePositionWithServer() command or something in bukkit to achieve that without teleporting. But maybe I'm a blind idiot and such a thing exists already.
     
Thread Status:
Not open for further replies.

Share This Page