Teleport player onPlayerPortal [SOLVED]

Discussion in 'Plugin Development' started by douglas_srs, Jan 2, 2012.

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

    douglas_srs

    Hi guys, I would like to teleport a player to his latest location - 1, so he will back 1 block, I hava a hashmap:

    Code:
    Map<String,Location> locs = new HashMap<String,Location>();
    Then I save the locations onPlayerMove:

    Code:
        public void onPlayerMove(PlayerMoveEvent event)
        {
            Player player = event.getPlayer();
            Location local = player.getLocation();
            local = player.getLocation();
            locs.put(player.getName(), local);
        }
    Then on the event onPlayerPortal I make this to teleport the player back:

    Code:
    public void onPlayerPortal(PlayerPortalEvent event)
        {
            World world = event.getFrom().getWorld();
    
            if (world.getEnvironment() == Environment.NORMAL) {
    
            Player player = event.getPlayer();
    
            if (event.getTo().getWorld().getEnvironment().equals(Environment.NETHER)) {
            if (WorldPlus.instance.worldNames.contains(event.getTo().getWorld().getName()) ) {
                if (!player.hasPermission("worldplus.nether")) {
                        player.sendMessage(WorldPlus.instance.plugintag + "Voce nao pode ser teleportado para o nether!");
                        event.setTo(locs.get(player.getName()));
                        }
            }
            }
    
            if (event.getTo().getWorld().getEnvironment().equals(Environment.THE_END)) {
            if (WorldPlus.instance.worldNames.contains(event.getTo().getWorld().getName()) ) {
                if (!player.hasPermission("worldplus.theend")) {
    
                    player.sendMessage(WorldPlus.instance.plugintag + "Voce nao pode ser teleportado para o theend!");
                    event.setTo(locs.get(player.getName()));
                }
            }
            }
            }
        }
    But I noticed that it's teleporting to the same location I'm... on the portal! so it's trying to teleport every 1 sec, and it's spamming... so, how do I teleport the player to the block in his back to get out of the portal, be it nether or theend?

    Thanks
     
  2. You could always cancel the event. (event.setCancelled(true)) That way the player will never warp.

    The problem with your onPlayerMove is that it will always trigger when you move your mouse or walk even the slightest of millimeter. Walking on the blocks of the portal will store that location in your hashmap. What you could do is just teleport the player to his current location but minus one (or two just in case the player strafe in to the portal with his back to the portal block) in the direction he/she is looking.

    But cancelling the event is probably the easiest way to disable portals.
     
  3. Offline

    douglas_srs

    Yes, I've already used the event.setCancelled(true), but it wasn't teleporting the player back with player.teleport(loc), then I decided to instead of cancelling it I just change the destination, but now I can't figure out how to save the right players location when just walking and subtract -1 block so it will teleport back to the right destination.

    Any help with this? =p
     
  4. The cancelling of the event is something you do have to do so players won't find ways of entering those places.

    Also you don't have to save the location. Just grab the current location of the player and subtract one (location.subtract(x, y, z)) from that and teleport the player to that new location. You just have to figure out which coordinate you have to subtract (or even add depending on the way the player walks into the portal.)
     
  5. Offline

    douglas_srs

    Exactly what I want! But I'm new in java and I'm starting with bukkit so I don't have Idea how to check player direction and subtract the location based on his looking direction.... Anyone can help me with this?
     
  6. Never used that piece of the code too. But I think it's the yaw from a location (location.getYaw())

    Just try and see what the results are. :)
     
  7. Offline

    bergerkiller

    You can't use teleport in PLAYER_MOVE events, and player portal enter event is a player move event. You need to schedule the teleportation using a synchronized delayed task. Not sure if that's possible, but set the location to teleport to in the event.
     
  8. Offline

    douglas_srs

    All I wanna do is to block player teleportation from natural Nether/End portals if he has not a permission, but if I only block it and it's a End Portal he will fall into the lava under the portal and die, do I'm trying to teleport him back to his back block :p

    Thank you all for your help, special thanks to you @bergerkiller for helping in the Mail system :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
Thread Status:
Not open for further replies.

Share This Page