Prevent player from walking

Discussion in 'Plugin Development' started by CompuIves, Apr 14, 2012.

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

    CompuIves

    Hi guys!
    I want to prevent players from walking, but that they still can look around...
    I'm doing it now in an onPlayerMove event with:
    event.setTo(event.getFrom())
    But that prevents the player from just looking around, is there someone with a solution for this?
    Thanks in advance!
     
  2. Offline

    r0306

    Try this:

    Code:
    @EventHandler
    public void moveEvent (PlayerMoveEvent event) {
    event.setCancelled(true);
    }
     
  3. Offline

    Musaddict

    @Compulves In my plugin, I save the location I dont want them to move from, then check if they are more than 2 blocks away. If so, teleport them back to their location. Sadly, simply cancelling the event doesn't work, since looking around is considered movement.

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGH)
    3. private void onPlayerMove_Func(PlayerMoveEvent event) {
    4. Player player = event.getPlayer();
    5. Location playerLocation = null;
    6. Location signLocation = null;
    7.  
    8. if (GcEntityListener.signExists.containsKey(player)) {//if you dont want the player to move
    9. if (!GcEntityListener.signExists.get(player)) {
    10. playerLocation = player.getLocation();
    11. }
    12. }
    13.  
    14. if (!(playerLocation == null)) {
    15. signLocation = GcBlockListener.signLocation.get(player); //location to teleport back to (saved in hashmap)
    16. if (!(signLocation == null)) {
    17. if (playerLocation.distance(signLocation) > 2) {
    18. player.teleport(signLocation);
    19. player.sendMessage(ChatColor.RED + "You cannot move until your launched ball has landed.");
    20.  
    21. }
    22. }
    23. }
    24. }
    25.  
     
  4. Offline

    nicholasntp

    Just check on a move event if the x, y, or z has changed. Here's what I would do.

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGH)
    3.  
    4. public void PlayerMoves(PlayerMoveEvent event) {
    5. double fromX = event.getFrom().getX();
    6. double fromY = event.getFrom().getY();
    7. double fromZ = event.getFrom().getZ();
    8. double toX = event.getTo().getX();
    9. double toY = event.getTo().getY();
    10. double toZ = event.getTo().getZ();
    11. if (!(fromX == toX)) event.getTo().setX(fromX);
    12. if (!(fromY == toY)) event.getTo().setY(fromY);
    13. if (!(fromZ == toZ)) event.getTo().setZ(fromZ);
    14.  
    15.  
    16. }

    That should work. And I know you're using this for your Hunger Games plugin. I am almost done with mine, and I wanted to know how yours is coming along. Lol.
     
  5. Offline

    Njol

    Simply only cancel the event if the player moved, ignoring their direction:
    Code:java
    1. if (!event.getFrom().toVector().equals(event.getTo().toVector()))
    2. event.setCancelled(true);

    (A Vector is only x/y/z, thus it doesn't include the direction the player is looking)
     
  6. Offline

    CompuIves

    Thank you guys! It works!
    About my plugin, I'm using it for the server right now, I'm planning on releasing it, but that's when I have a donator system and such in it...
     
  7. Offline

    nicholasntp

    Wait, what works? My code? Cause I didnt test it and might need it. :p
     
  8. Offline

    CompuIves

    No sorry xD. I've tried your code and it didn't work :S. Musaddicts code works though
     
  9. Offline

    nicholasntp

    Huh. Mine should work :p That is interesting.
     
  10. Offline

    CompuIves

    Yeah, I don't understand it too xD. Maybe your yaw and pitch will reset when you will be teleported...
     
  11. Offline

    nicholasntp

Thread Status:
Not open for further replies.

Share This Page