Help! What do I do now!!

Discussion in 'Plugin Development' started by DominicGamesHD, Jun 3, 2016.

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

    DominicGamesHD

    I want to make it so they will TP to the player (I have just started bukkit API again)
    Code:
    package me.gothdom.SE.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Teleporting extends JavaPlugin {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("stp")) {
                if (args.length == 0)
                    sender.sendMessage(ChatColor.GREEN + "Usage: /stp <playername>");
            } else {
                Player target = Bukkit.getPlayerExact(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + args[0] + "is not online!");
                } else {
                    sender.sendMessage(ChatColor.AQUA + "You teleported to" + ChatColor.GOLD + args[0]);
                    target.sendMessage(ChatColor.GOLD + sender.getName() + ChatColor.GREEN + "has teleported to you!");
                }
            }
    
            return false;
        }
    }
     
  2. Offline

    teej107

    Have you tried searching through the Bukkit JavaDocs?
     
  3. Offline

    TheMagzuz

    You should cast the sender to a player object, then do
    Code:
    player.teleport(target);
    That should work
     
  4. Offline

    DominicGamesHD

    Thanks to you both :)
     
  5. Offline

    Zombie_Striker

    @TheMagzuz @DominicGamesHD
    Remember, you should not blindly cast the sender to a player. How do you always know a player will be the one sending the command? There is a reason the sender is not automatically a player.

    Check if sender is an instanceof Player before casting.
     
  6. Offline

    thomas12000

    Well that's my code if you wanna check it out ... I didn't try it if it works but here take it :p
    Code:
    if(cmd.getName().equalsIgnoreCase("stp")){
    
    Player player = (Player) sender;
    
    //Check what happens when the player does /stp
    if(args.length == 0){
    player.sendMessage("stuff");
    return true;
    }
    
    //Check if the sender is indeed a player
    if(!(sender instanceof Player){
    sender.sendMessage("You are the console");
    }
    
    //If you want your command to have a permission
    if(!(player.hasPermission("pluginame.permission")){
    player.sendMessage("You dont' have permission");
    return true;
    }
    
    //If you wanna have a word limit (sometimes gives error if you don't have that)
    if(args.length > 1){
    player.sendMessage("Too many words");
    return true;
    }
    
    Player target = Bukkit.getServer().getPlayer(args[0])
    if(target == null){
    player.sendMessage("Player not online.");
    return true;
    }
    player.teleport(target);
    player.sendMessage("Teleported to stuff");
    target.sendMessage("Player teleport to you");
    }
    Also in plugin.yml
    Code:
    name: PluginName
    main: package.PluginName.java
    version: 0.1
    
    commands:
        stp:
            description: Teleport to a player.
     
    Last edited: Jun 6, 2016
  7. Offline

    MrGeneralQ

    I can see that you are new to bukkit coding. You don't have to but you better create an if statement for each args.length for ex:

    Code:
    if(args.length == 0){
    // action when no arguments
    }
    
    if(args.length == 1){
    // action when there is one argument
    }
    // and so on...
    
    
    Also only declare the variable "target" when there is an argument. If you just put it on top it will return a NullPointerException. Use this:

    Code:
    if(args.length == 0){
    // action when no arguments
    }
    
    if(args.length == 1){
    // Here is an argument so declare here
    Player target = Bukkit.getServer().getPlayer(args[0]);
    
    
    }
    I hope you understand
     
  8. Offline

    Xerox262

    You should be using else if, since it's not possible for args.length to be both 0 and 1.

    And his ifs are fine, a bit odd but they'd work, at the start he made sure the args were not 0, then he checked they were not higher than 1, the only way the rest of the code executes is if there's only 1 argument.

    P.S. I was assuming you were replying to @thomas12000 not @DominicGamesHD if you weren't then woops :I
     
  9. Offline

    MrGeneralQ

    @Xerox262

    I do this all the time in my code. I agree it can't be 1 and 0 at the same time. But take a closer look. For ex: Do something when there is 1 argument. The other is checking if there are no arguments. So this is possible.
     
  10. Offline

    DoggyCode™

    Why would you first blind cast the command sender to Player, then later checking if it is instanceof. This is so,etching you should do at the very beginning .
     
  11. <delete>
     
    Last edited: Jun 8, 2016
  12. Offline

    N00BHUN73R

    @DominicGamesHD
    Your code literally states, If the command is "stp" then print a message, otherwise Tp the player..
    You have to add brackets to the args.length check and make sure that all of your code is inside the cmd.getName() check
     
  13. Offline

    n00bl3

    Why do you use cmd.getname() ?

    Just use label.equalsIgnoreCase("command")
    you did do String label...
     
  14. Offline

    Zombie_Striker

    @SirQuantum
    Do not spoonfeed. There should be no reason why you can't just tell the other member what to do in words. If you want to read why you should not spoonfeed, click HERE and HERE.
     
Thread Status:
Not open for further replies.

Share This Page