Freezing players-SOLVED

Discussion in 'Plugin Development' started by Smurfy129, Jul 7, 2014.

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

    Smurfy129

    Ok guys so I have this in a separate listener class.

    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            if(plugin.subCmds.playerMove.contains(player.getDisplayName())) {
                player.teleport(player);
            }
        }
    
    And so when the player is in the array it works great that when they move it won't let them. But I want them to be able to look around. Is there a way to add another if statement to see if its a actual x,y,z movement vs a look around?

    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            if(player.moves(x, y, z)) {
                if(plugin.subCmds.playerMove.contains(player.getDisplayName())) {
                    player.teleport(player);
                }
            }
        }
    Something like that
     
  2. Offline

    JasonDL13

    In PlayerMoveEvent, the Pitch and Yaw changing (the head rotation) also count. So you can check if event.getFrom() X doesn't equal event.getTo() X. Same with Y and Z. If you're confused reply and I'll help you more.
     
  3. Offline

    Smurfy129

    Ok I understand what you are saying but i'm having trouble with the code. Could you get me started with the first if statement please
     
  4. Offline

    JasonDL13

    Smurfy129 I wouldn't normally do this but you seem to know what you are doing

    Code:java
    1. //Let's assume e is PlayerMoveEvent
    2.  
    3. //getTo() is the Location the player is going to
    4. //getFrom() is the Location the player is coming from
    5.  
    6. //If To-X doesn't equal From-X
    7. if(e.getTo().getX() != e.getFrom().getX()) {
    8. //The player has move on his X coords. Cancel the event.
    9. e.setCancelled(true); //Forgot how to spell cancelled
    10. }
    11.  
    12. //Do something similar to this on Y and Z.
     
  5. Offline

    Deleted user

    Smurfy129 JasonDL13
    Just a note, because I'm afraid that Smurfy will get the wrong impression:

    It's perfectly fine to use an || (or) check in this situation. Copy/pasting that code block and changing the getX, getY, and getZ will work, but will be less 'pretty'.

    I can't speak for efficiency however.
     
  6. Offline

    TheMrGong

    Problem is if they move and try looking they will above get canceled. Try teleporting to the location the player is coming from but with the pitch/yaw in the location the player is going to.
     
  7. Offline

    JasonDL13

    zombiekiller753 Yeah, you can do that. I would like to keep it more organized and not have 3 different conditions in an if statement. But since this is only one-line of code it doesn't really matter
     
  8. Offline

    HungerCraftNL

    PHP:
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent e) {
      
    Player p e.getPlayer();
      
    p.teleport(e.getFrom());
    }
     
  9. Offline

    teej107

    Smurfy129 You could set the player walk speed to 0. You would have to change it back though unless you want to keep players glued.
     
  10. Offline

    coasterman10

    Using PlayerMoveEvent would work but would also make movement rather choppy to the client and is somewhat of a cop-out. I have found a good way is to use Player#setWalkSpeed to change their walk speed to 0, and then back to 0.2 to unfreeze them. You should also use a Jump Boost effect to prevent them from jumping.

    I found a good solution on this post: https://forums.bukkit.org/threads/h...t-having-it-look-glitchy.148549/#post-1686316
     
  11. Offline

    MrlolDestructo

    These are all great solutions above, but I'm going to offer another solution, sort of like JasonDL13 's:

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e) {
    3. if((e.getFrom().getBlockX() != e.getTo().getBlockX()) || (e.getFrom().getBlockZ() != e.getTo().getBlockZ())) {
    4. e.setCancelled(true);
    5. }
    6. }


    This will just cancel the move if they move to a different X position or Z position, and they can move around slightly on the block, or jump, giving it a bit of a better look. Hope this helps. :)

    EDIT: Thanks to dsouzamatt for fixing my code.
     
    dsouzamatt likes this.
  12. Offline

    dsouzamatt

    That looks good, just make sure you use "or" instead of "and" on line 3.
     
    MrlolDestructo likes this.
  13. Offline

    Smurfy129

    Thanks everyone I got it figured out picked the best way and it works great!
     
Thread Status:
Not open for further replies.

Share This Page