@deprecated needs enumbody?

Discussion in 'Spigot Plugin Development' started by blohod, Jun 29, 2015.

  1. Why does @Deprecated need EnumBody to complete blockstatement?

    Code (Text):
    public class Command implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                Integer XCoord = -43;
                Integer ZCoord = 242;
               
                @Deprecated
                TeleportCommand().execute(player, XCoord, 200, ZCoord);
            }
            return true;
        }

    }
    also could someone please explain this
    Code (Text):
    onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args)
     
    #1 blohod, Jun 29, 2015
    Last edited: Jun 29, 2015
  2. Hi there.

    Your snippet doesn't teleport the player which is specified by the command arguments. It teleports the player who called the command. And in fact I'm not even sure if it teleports him properly. Here's a snippet of what you're looking to do :

    Code (Text):
    public class Command implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(args.length < 4) {
                sender.sendMessage(ChatColor.DARK_RED + "Error not enough arguments!");
                return false;
            }
            String targetName = args[0];
            Player target = Bukkit.getPlayer(targetName);
            if(target == null) {
                sender.sendMessage(ChatColor.DARK_RED + "Error player not found!");
                return false;
            }
       
            int x;
            int y;
            int z;

            try {
                int x = Integer.parseInt(args[1]);
                int y = Integer.parseInt(args[2]);
                int z = Integer.parseInt(args[3]);
            } catch(NumberFormatException e) {
                sender.sendMessage(ChatColor.DARK_RED + "Error could not parse one of the coord arguments!");
                return false;
            }
       
            Location tpLocation = new Location(target.getWorld(), x, y, z);
            target.teleport(tpLocation);
            sender.sendMessage(ChatColor.GREEN + "You have teleported " + targetName + " to " + x + ", " + y + ", " + z + "!");
            target.sendMessage(ChatColor.GREEN + "You have been teleported by " + sender.getName());
        }
    }
    Also don't forget that you'll need to register this command somewhere. For example in your plugin's onEnable method with :
    Code (Text):
    getCommand("teleport").setExecutor(new Command());
    Regarding your second question, it's sadly too big a subject to answer here. If you wish to stop having a hard time writing your own code and stop relying on copy-pasting, I recommend starting from the basics and learn some theory on Java. Here are a few good resources to get started:

    - http://www.amazon.com/Head-First-Java-2nd-Edition/dp/0596009208/
    - http://tinyurl.com/ptsgqoa
    - http://math.hws.edu/javanotes/

    Hope that helps.
     
    #2 CoKoC, Jun 29, 2015
    Last edited: Jun 29, 2015
  3. thanks, but I do want to teleport the player that called the command
     
  4. So you're asking what's the issue with @Deprecated right?

    The issue is that @Deprecated does need an enum body. If you don't know what an enum is, an enum is a way to define @(phrase) or to define variables. I haven't worked with deprecated yet, but you might need to import something.

    My other solution (only if this is for getting rid of warnings, which I'm not sure if it is) would be using @SuppressWarnings("deprecated")
     
  5. Code (Text):
    public class Command implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(! (sender instanceof Player)) {
                sender.sendMessage(ChatColor.DARK_RED + "You need to be a player to use this command!");
                return false;
            }
            Player target = (Player) sender;

            if(args.length < 3) {
                sender.sendMessage(ChatColor.DARK_RED + "Error not enough arguments!");
                return false;
            }
       
            int x;
            int y;
            int z;

            try {
                int x = Integer.parseInt(args[1]);
                int y = Integer.parseInt(args[2]);
                int z = Integer.parseInt(args[3]);
            } catch(NumberFormatException e) {
                sender.sendMessage(ChatColor.DARK_RED + "Error could not parse one of the coord arguments!");
                return false;
            }
       
            Location tpLocation = new Location(target.getWorld(), x, y, z);
            target.teleport(tpLocation);
            target.sendMessage(ChatColor.GREEN + "You teleported to " + x ", " + y + ", " + z + "!");
        }
    }
     
  6. ...

    If you're trying to make your plugin look totally custom by using the import, just don't. Just use Command and import org.bukkit.command.Command
     
  7. The reason it's forcing the full path of the class is because your class uses the same name (Command). If you were to leave of the full path Java and your IDE will try to use your class there instead of the correct one, which wont work because your Command class isn't what that method requires. Trying to import the correct one will throw errors because of name conflict and wont compile. So, you have to specify exactly which class to use by specifying where it is via full path.

    Change the name of your class to something like MyCommand, and then you wont have any conflicts there. ;)

    Ignore this. He doesnt know what he's talking about.
     
    • Agree Agree x 1
  8. Actually smartass I do know what I'm talking about. He's putting the input in instead of the enum. Just put the enum and it'll make everything ten times easier

    And is there a reason you didn't include the imports…
     
  9. What the hell are you talking about? There is no enum there. There is no input there. The coordinates are hardcoded in (probably for testing purposes)..

    He can't import org.bukkit.command.Command because his class is named Command. I already said this in my last reply.

    -------

    Anyways. To answer the OP's original question, you dont put @Deprecated in code like this. The annotation is reserved for methods, not statements.

    OP can solve this by teleporting the player the correct way by using player.teleport method from the API (like CoKoC suggested) instead of executing the vanilla TeleportCommand from NMS.
     
  10. There is no input, I made a spelling error. I meant to say import. Screw auto correct

    Command is the enum here from the command class (org.bukkit.command.Command) or if I didn't word it correctly, the variable definition.

    If his freaking class is called command, here's an idea. Change the name to CommandTP
     
  11. @Target(value={CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE})

    @Deprecated's targets include all of the above. It's not strictly used for methods, and if the statement returned a variable, one could use it for the method call, it would just require a dummy local variable to be created.