Solved If player used /tp and teleported

Discussion in 'Plugin Development' started by Condolent, Dec 29, 2016.

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

    Condolent

    Hi, I'm making a event where if the player teleports via /tp and successfully teleported something happens (in this case; a sound effect).

    How can I do this?
    I tried:
    Code:
        // Play sound on teleport
        @EventHandler
        public void onTp(PlayerCommandPreprocessEvent e) {
            Player p = e.getPlayer();
           
            if(e.getMessage().startsWith("/tp")) {
                if(getConfig().getBoolean("playerTP")) {
                    p.playSound(p.getLocation(), tp, 1, 0);
                } else {
                    // Do nothing
                }
            } else {
                // Do nothing
            }
        }
    But obviously that only checks if the player just typed in /tp, it doesn't check if the player successfully teleported.

    I've also tried using the PlayerTeleportEvent but that triggered when the player spawned aswell, not really what I want.

    What's the correct way of doing this?

    Nvm, fixed with:
    Code:
        // Play sound on teleport
        @EventHandler
        public void onTp(PlayerTeleportEvent e) {
            Player p = e.getPlayer();
           
            if(e.getCause() == TeleportCause.COMMAND || e.getCause() == TeleportCause.PLUGIN) {
                if(getConfig().getBoolean("playerTP")) {
                    p.playSound(p.getLocation(), tp, 1, 0);
                } else {
                    // Do nothing
                }
            } else {
                // Do nothing
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 29, 2016
Thread Status:
Not open for further replies.

Share This Page