Solved Check if a player moves 1 block and teleport them back to that block

Discussion in 'Plugin Development' started by danielh112, Nov 7, 2013.

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

    danielh112

    How would I check to see if a player on a array list moves one block and teleport them back to the block they were just on? (thank you in advance been really struggling with how to do this!!!)
     
  2. Offline

    sd5

    danielh112 Save the player's current location and use an PlayerMoveEvent to check if the player's new location is further than 1 block and if so, teleport the player to the saved location.

    Got ya Noxyro ;)
     
  3. Offline

    Noxyro

    1) Save the position he should be teleported back on
    2) Check movement with PlayerMoveEvent
    3) If his distance to the saved position is greater than one block - teleport him back

    Need a code example?

    Edit: Well... someone was faster ^^
     
  4. Offline

    danielh112

    Yes please! Thats what I was thinking but having some code will help be massively!!
     
  5. Offline

    sd5

    danielh112
    Code:java
    1. //Save the location you want the player to teleport to in the HashMap
    2. private HashMap<String, Location> loc = new HashMap<String, Location>();
    3.  
    4. @EventHandler
    5. public void onPlayerMove(PlayerMoveEvent event) {
    6. Player player = event.getPlayer();
    7. Location location = loc.get(player.getName());
    8. if(player.getLocation().distance(location) >= 1) {
    9. player.teleport(location);
    10. }
    11. }
     
  6. Offline

    Codex Arcanum

    Would be more efficient to use a repeating task, but a move event will work fine.
     
    jimuskin and drtshock like this.
  7. Offline

    danielh112

    How would u do it via a repeating task?

    Thank you i have been looking for something like
    Thank you been looking for this for ages! !

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

    xTrollxDudex

    sd5
    Use event.setTo(event.getTo())
     
  9. Offline

    Chinwe

    Don't you mean event.setTo(event.getFrom()) ? :confused:
     
    xTrollxDudex likes this.
  10. Offline

    danielh112

    Sorry I still do not understand how I would use this for it, would be able to explain or give an example please that would be much appreciated!!!
     
  11. Offline

    calebbfmv

    What you can do, which I do for me POTCO server/plugin, is create a hanging pirate effect with moving the player back and forth on a hopper, with a sign attached to it.
    Code:
     @EventHandler
        public void move(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            Location b = p.getLocation().add(0, -1, 0);
            Location s = p.getLocation().add(1, -1, 0);
            if ((b.getBlock().getType() == Material.HOPPER)
                    && s.getBlock().getType() == Material.WALL_SIGN
                    || s.getBlock().getType() == Material.SIGN_POST) {
                p.teleport(e.getFrom());
            }
        }
    
     
    danielh112 likes this.
  12. Offline

    Chinwe

    In the PlayerMoveEvent, which will set the location you will be moved to (set 'event.getTo()' by using 'event.setTo(location)') to the location you moved from ('event.getFrom()'). Essentially, you can't move, as you are teleported back to where you were every time you try to move.
     
  13. Offline

    sd5

    xTrollxDudex Well, basically the difference between your solution and mine is, that with yours, the player is teleported even if he just moves 0.0001 blocks, which results in teleporting several hundreds of times... With my solution he is only teleported after moving 1 block, so he isn't teleported that often and due to the thread description
    I thought that he looks for that thing :)
     
  14. Offline

    Chinwe

    sd5
    Unfortunately yours uses .distance incredibly often, which is quite costly due to square rooting: a better way would be to simply check the coords of the location:
    Code:
    @EventHandler
    public void onMove(PlayerMoveEvent event)
    {
      if (event.getFrom().getBlockX() == event.getTo().getBlockX() && event.getFrom().getBlockZ() == event.getTo().getBlockZ() && event.getFrom().getBlockY() == event.getTo().getBlockY())
          return;
     
      // anything here will run when he moves to a new block
     
    }
     
  15. Offline

    danielh112

    I am getting errors in the console when the player moves, I am not to sure why? Any suggestions because this codw is perfect what I need!
     
  16. Offline

    sd5

    So... where is the error? Did you remember that you have to add a location for each player to the HashMap first?
     
  17. Offline

    danielh112


    ah okay forgot to add the location for the player :) I take it is just a case of adding the player to a hash map with the location?

    Sorry for being incompetent but I still cannot get it to work to add the player to the hash map I have used..
    String name = player.getName();
    loc.put(name, player.getLocation());

    I presume it is something hear I have done wrong!

    Sorry for being incompetent but I still cannot get it to work to add the player to the hash map I have used..
    String name = player.getName();
    loc.put(name, player.getLocation());

    I presume it is something hear I have done wrong!



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

    Xyaren

    i used to check :
    (pme = PlayerMoveEvent)
    Code:java
    1. pme.getFrom().getBlock().equals(pme.getTo().getBlock())
     
  19. Offline

    Noxyro

    Code:java
    1. Map<Player, Location> yourDontMoveList = new HashMap<Player, Location>();
    2.  
    3. @EventHandler
    4. public void onPlayerMove(PlayerMoveEvent e) {
    5. if (yourDontMoveList.containsKey(e.getPlayer())) {
    6. Location loc = yourDontMoveList.get(e.getPlayer());
    7. if (e.getPlayer().getLocation().distance(loc) >= 1) {
    8. e.getPlayer().teleport(loc);
    9. }
    10. }
    11. }


    Done!
     
  20. cancel the event :D
     
  21. Offline

    xTrollxDudex

    I was implying that you use event.setTo(event.getFrom()) instead of using p.teleport().
    ysl3000
    That bugs out the screen, if you don't know.
     
  22. Offline

    drtshock

    Cancelling the event will not work. People will still move ever so slowly. I would like to reiterate that using a timer would be much more efficient than any other way you guys have pointed out.
     
    jimuskin and AndyMcB1 like this.
  23. Offline

    danielh112

    I still cannot get it to work! I really do not know why I am adding the player by doing

    yourDontMoveList.put(player, player.getLocation) (wrote in rather than copied). And do not know why I can not get this to work, I have tried everything but still nothing thank you for your help!
     
  24. Offline

    hellboyPS

    Note: Use .distanceSquared() >= 1 instead of .distance() >= 1
    Will speed it up by a factor of 2 - 5.
     
  25. Offline

    ZeusAllMighty11

    lol wat


    Code:
    List<String>frozenList = new ArrayList<String>();
     
    @EventHandler
    public void onMove(PlayerMoveEvent e)
    {
    Player p = e.getPlayer();
    int x = (int) e.getFrom().getX();
    int y = (int) e.getFrom().getY();
    int z = (int) e.getFrom().getZ();
     
    int x1 = (int) e.getTo().getX();
    int y1 = (int) e.getTo().getY();
    int z1 = (int) e.getTo().getZ();
     
    if(!(x != x1 || y != y1 || z != z1))
    return; // This means that the player moved the camera
     
    if(frozenList.contains(p.getName())
    e.setTo(e.getFrom());
    }
    
    This checks if the player moved the camera, do nothing. If the player moved a full block on any axis (x,y,z) then try to handle the event, being if the frozen list contains the player, send them back.

    No need to store locations, or player objects.. simple string.
     
  26. Offline

    GusGold

    danielh112
    Code:java
    1. public void onPlayerMove(PlayerMoveEvent e){
    2. Player player = e.getPlayer();
    3. if (myArrayList.contains(player){
    4. Location locationPre = e.getFrom();
    5. Location locationCur = e.getTo();
    6. if ((locationCur.getBlockX() != locationPre.getBlockX()) || locationCur.getBlockY() != locationPre.getBlockY() || locationCur.getBlockZ() != locationPre.getBlockZ()){
    7. //If player has moved into a different block
    8. player.teleport(locationPre);
    9. //OR
    10. e.setCancelled(true); //Can be buggy though
    11. }
    12. }
    13. }

    No need for a repeating task, saving the previous locations or hashmaps, simply check if the player in the event is in your arrayList, and if they have moved more than a block, tp them back! Simple as 3.141592....
     
  27. Offline

    hellboyPS


    Actually Im quite sure this won't work as intended. The player can manage to move very slowly, I tried this a few days ago. You actually have to store the players Location to be sure he doesn't move.
     
  28. Offline

    danielh112

    I just tried and and he did not move some I am not sure then?
     
    ZeusAllMighty11 likes this.
  29. Offline

    hellboyPS

    Okay, he did move when I tested it. Very slowly, when he steadily tried to move forward. Maybe it was because of Lag or another plugin or something.
     
  30. Offline

    ZeusAllMighty11

Thread Status:
Not open for further replies.

Share This Page