Locking Player Positions

Discussion in 'Plugin Development' started by danthonywalker, Jul 13, 2013.

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

    danthonywalker

    So I want to implement some kind of stun-like mechanic into a game I am working on, but I'm having trouble of ways to handle it.

    First off I don't want to completely freeze the player. I want them to be able to at least look around instead of their screen constantly twitching because they wish to move the camera. I only want them to be stuck on that position, but to at least look around, so just cancelling PlayerMoveEvent doesn't work. So I tried this:

    Code:
        if(stunLock)
            event.getPlayer().teleport(new Location(event.getPlayer().getWorld(), event.getFrom().getX(), event.getFrom().getY(), event.getFrom().getZ()));
    but that doesn't work, it does the same thing as just cancelling the event, the player is unable to look around at all. So how can I go about this? Making the player stay stuck in the same position, but be able to look around freely? Also, a way to make gravity still have effect, so if the player is in mid air when the stun happens, they still fall down to the ground (but be unable to move on the X and Z of course). Any help would be appreciated.
     
  2. Offline

    xTrollxDudex

    danthonywalker
    You need to check if the player is in a different location on move.
    Code:java
    1. //player move event
    2. Location f = event.getFrom();
    3. Location t = event.getTo();
    4.  
    5. if(f.getX() != t.getX() || f.getY() != t.getY() || f.getZ() != t.getZ()){
    6. player.teleport(f);
    7. }
     
  3. Offline

    Woobie

    danthonywalker
    Try this
    Code:
    if (((e.getTo().getX() != e.getFrom().getX()) || (e.getTo().getZ() != e.getFrom().getZ()))) {
      e.setTo(e.getFrom());
    When player is stunned, on move event, check if block below is air (or null), if it is, run that code above. For general stunning, use xTrollxDudex 's code.
     
  4. Offline

    xTrollxDudex

    Woobie likes this.
  5. Offline

    Woobie

    Always :p
     
  6. Offline

    xTrollxDudex

    Woobie
    I'm was typing on an iPod lol
     
  7. Offline

    danthonywalker

    Same problem as before, it's just not letting the player look around at all.

    Code:
        if(stunLock)
            {
            Location f = event.getFrom();
            Location t = event.getTo();
         
            if(f.getX() != t.getX() || f.getZ() != t.getZ())
                event.getPlayer().teleport(f);
            }
    EDIT: nvm, got it to work. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page