How to teleport?

Discussion in 'Plugin Development' started by Etarus, Sep 25, 2011.

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

    Etarus

    Can someone help me and give me a teleportation code?
    Because I don't know how to set up a command with arguments ingame.

    Code:
    if (label.equalsIgnoreCase("tp")) {
    How can I add arguments and how can I use them? Someone has an example please?
     
  2. Offline

    Feed_Dante

  3. Offline

    BenyTheBuff

  4. Offline

    Etarus

    is this ok to register the teleport command?

    Code:
    pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Event.Priority.Normal, this);
     
  5. Offline

    DirtyStarfish

    I would use this
    Code:
    
    		public boolean onCommand(Command sender, Command cmd, String cmdLine,
    			String[] args) {
    		if (sender instanceof Player) {
    			Player player = (Player) sender;
    
    			if (cmd.getName().equalsIgnoreCase("tp")) {
    
                                  //Teleport code here
    			}
    		}
    	}
    }
    
    Then you can use args[] for the arguments.
     
  6. Offline

    Etarus

    where i must put this in?

    in the PlayerListener or in the main?

    ok now i've made an example command wich should send a messega to the player.

    but all this player get is this command. and not the correct message.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  7. Offline

    dbizzzle

    Could you post your main/playerlistener, along with any errors you may have. Also, do you have a plugin.yml?
     
  8. Offline

    Etarus

    eTeleport.java (main):

    Code:
    package me.Etarus.eTeleport;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class eTeleport extends JavaPlugin {
    
        private final eTeleportPlayerListener playerListener = new eTeleportPlayerListener(this);
    
        Logger log = Logger.getLogger("Minecraft");
    
        public void onDisable() {
            log.info("[eTeleport] eTeleport plugin disabled.");
        }
    
        public void onEnable() {
            log.info("[eTeleport] eTeleport v." + getDescription().getVersion() + " plugin enabled");
    
            PluginManager pm = this.getServer().getPluginManager();
    
            pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Event.Priority.Normal, this);
        }
    
    }
    
    eTeleportPlayerListener (playerlistener):
    Code:
    package me.Etarus.eTeleport;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    
    public class eTeleportPlayerListener extends PlayerListener {
    
        public static eTeleport plugin;
    
        public eTeleportPlayerListener(eTeleport instance) {
            plugin = instance;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
    
                if (cmd.getName().equalsIgnoreCase("test")) {
                    player.sendMessage("Hallo");
                    return true;
                }
            }
            return false;
        }
    }
    
    plugin.yml:
    Code:
    name: eTeleport
    main: me.Etarus.eTeleport.eTeleport
    version: 0.1
    author: Etarus
    commands:
        test:
            description: Say Something
            usage: /<command>
     
  9. Offline

    dbizzzle

    onCommand goes in your main, other than that don't see any issues
     
  10. Offline

    Etarus

    ok
    how should i write it so that its work?
     
  11. Offline

    dbizzzle

    put
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command command,
    3. String label, String[] args) {
    4. if (command.getName().equalsIgnoreCase("test")) {
    5. Player player = (Player) sender;
    6. player.sendMessage("Hallo");
    7. }
    8. return false;
    9. }
    10.  

    in the main and remove your playerlistener.
     
  12. Offline

    Etarus


    Should this be in the main or is it not needed?
    Code:
    pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Event.Priority.Normal, this);
     
  13. Offline

    dbizzzle

    Look at my corrected post and that is not needed

    This is what you want to teleport the player.
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command command,
    3. String label, String[] args) {
    4. if (command.getName().equalsIgnoreCase("test") && args.length == 1) {
    5. Player player = (Player) sender;
    6. Player tele = getServer().getPlayer(args[0]);
    7. player.sendMessage("Teleport successful!");
    8. player.teleport(tele);
    9. }
    10. return false;
    11. }
    12.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  14. Offline

    Etarus

    Tank you, i'll try it.

    I have one question.The PluginManager is needed for what?
    (Sry, i'm new to java and to bukkit and try to learn sth.)
     
  15. Offline

    dbizzzle

    The plugin manage is to register events such as onPlayerChat (player listener), or onEntityDamage (entity listener), or onBlockPlace (block listener). So say you wanted to check when somebody places a block, you would have to register the Block Place event otherwise no matter what you do in your Block listener it will not register.
     
  16. Offline

    Etarus

    ok it works as it sends the message, but it say the executation command again :(

    If i type: /test
    It say:
    Code:
    Hallo
    /test
    
    ok thanks for that explanation.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  17. Offline

    dbizzzle

    just throw "return true;" in on the line after "player.sendMessage("hallo");"
     
  18. Offline

    Etarus

    oh yeah, i forgot it. thank you.

    how can i write a colored text to a player?

    //EDIT:
    how can i get the player name?
    if i write:
    Code:
    victim.sendMessage(player + " ... you.");
    then it write something like: craftbukkit(name=player) ... or something like that...
    how can i fix this?

    2nd problem solved. i changed to:
    Code:
    player.getDisplayName()
    Other problem exist:

    How to write colored messages to a player?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  19. Offline

    Feed_Dante

    API: http://jd.bukkit.org/apidocs/org/bukkit/ChatColor.html

    Something like:
    player.sendMessage(ChatColor.RED + "blahblahblahblah");
     
  20. Offline

    Etarus

    how can I set the error message if this player name does not exist?
     
  21. Iterate through all players on the server, and send the error message to the player, if the specific player doesnt match any of the online players
     
  22. Offline

    Etarus

    well...
    an example? because i don't understand anything, sorry


    And:
    How can I hook into permissions 3.1.6?
     
  23. Code:
    for(Player p : getCommandSender().getWorld().getPlayers() ) {
       if(p.getDisplayName() == playername_in_command) {
          do_something
       }
    }
    Might not be entirely correct since thats from my head, but I wish you can get the idea
     
  24. Offline

    dbizzzle

    Maybe
    Code:java
    1.  
    2. Player pla = getServer().getPlayer(args[0]);
    3. List online = plugin.getServer().getOnlinePlayers();
    4. if (online.contains(pla)) {
    5. do somethin;
    6. }
    7.  

    May or may not work, not sure
     
  25. Offline

    K900

    Better way:
    Code:
    if (getServer().getPlayer(name) != null) {
     // Player exists
    }
     
    varesa likes this.
  26. Offline

    Etarus

    Thanks, it works.

    Next question:
    How can I make commands only for OP's or how to hook into permissions 3.1.6?
    I tried to hook into permissions but it doesn't work so far...
     
  27. Offline

    dbizzzle

    for Op just do
    Code:java
    1.  
    2. Player s = (Player) sender;
    3. if (s.isOp()) {
    4. do something;
    5. }
    6.  
     
  28. Offline

    MuisYa

    For the color in the chat use:
    Code:
    player.sendMessage(ChatColor.BLUE + "Hello " + ChatColor.WHITE + "how are you doing?");
    You could better try to hook into a superperms system, like BukkitPermissions.
    Than you would need this:
    Code:
    Player s = (Player) sender;
    if (s.hasPermission("Permission Node")) {
        // than do something.
    }

    And in youre plugin.yml:
    Code:
    permissions:
        permissionnode:
            default: op
    Since youre having a permissions system integrated, and if its not found:
    Defaults to Operators only.

    Code:
    Player s = (Player) sender;
    if (!s.hasPermission("Permission Node")) {
        s.sendMessage(ChatColor.RED + "You dont have permission to do this.")
    }
    else {
        // teleport the player.
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  29. Offline

    Etarus

    How can i teleport to a specific world?
    with one arguement and the command /tpw <world> ?
     
  30. A Location object contains a World value.
    For example in my warp plugin I don't care about /warp to another world or to the same world, it's the same.
     
Thread Status:
Not open for further replies.

Share This Page