Solved Target selector for custom commands?

Discussion in 'Plugin Development' started by cxfredeper, May 15, 2016.

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

    cxfredeper

    As far as I know 1.7.x versions' /kill command won't accept arguments, it only kills yourself. So I'm trying to make a small plugin overriding the original kill command, allowing 1.8-like arguments.
    However I couldn't find a way to enable target selectors, as "@a" will be treated as a player called "@a" which apparently doesn't exist.
    So far I've been trying to code the target selectors myself, but I wonder if there's a better way.

    Sorry, it should be "doesn't exist".

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: May 15, 2016
  2. Offline

    OhYes

    Try this:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if(!(sender instanceof Player)){
    sender.sendMessage("You cannot use commands as the Console.");
    }else{
    Player p = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("kill")){
    if(args.length == 0){
    p.setHealth(0);
    p.sendMessage(ChatColor.RED + "Killed " + p.getName());
    }else if(args.length == 1){
    if(args[0].equalsIgnoreCase("@a")){
    for(Player allplayers : Bukkit.getOnlinePlayers()){
    allplayers.setHealth(0);
    }
    p.sendMessage(ChatColor.RED + "Killed ALL players");
    }else{
    try{
    Player target = Bukkit.getPlayer(args[0]);
    target.setHealth(0);
    p.sendMessage(ChatColor.RED + "Killed " + target.getName());
    }catch(Exception e){
    p.sendMessage(ChatColor.RED + "Player not online, or does not exist.");
    return false;
    }
    }
    }
    }
    }
    return false;
    }
    That should work.
     
  3. Offline

    Zombie_Striker

    @OhYes
    Thanks Captain Spoonfeeder. But like every singe post that tries to spoonfeed, you made some mistakes.

    1. You are checking if the sender is a player or console, and then sending them a message even if it is not the command you're looking for.
    2. Your formatting is terrible. It's hard to read and makes it even harder to understand where each statement ends.
    3. You are using try/catch for nullpoints. You want to try to avoid errors, not expect them.
    4. You are not null checking target.
    5. You never return true. Only return false when the command failed at something. If it worked as planned, return true.
    Please, don't spoonfeed and just tell the person what they need to do. Don't do their job for them.
     
    Gonmarte likes this.
  4. Offline

    cxfredeper

    Thanks for the replies.

    In fact, that's what I meant by "code the target selectors myself".
    Maybe I worded my question wrong in the first place, but I'd like to know if there's any way that would, for example, tell bukkit to treat "@a" as a target selector, not as a regular string.

    And by the way it's "target selectorS", which means I'll need to do all four of them.
     
    Last edited: May 15, 2016
  5. Offline

    Zombie_Striker

    @cxfredeper
    Nope. '@a' is just a string, so you will have to manually check for it.
     
  6. Offline

    Gonmarte

  7. Offline

    OhYes

    I.. hm.. I- don't seem to give a shit. If someone asks for help, I'll give it to them. "Spoonfeed" As long as it helps them Idgaf. I guess with your reasoning I should just respond to anyone I'm trying to help with "learn java" seeing as you're not allowed to do their job for them.
     
    Last edited: May 15, 2016
  8. Offline

    cxfredeper

    @Zombie_Striker
    Okay, thanks again.
    Wasn't really expecting a way to directly use target selectors, but nice to confirm that there is none.

    ————————————
    You are free to continue your arguments about spoonfeeding.
     
  9. Offline

    OhYes

    Hm, if you're doing all 4 of them, I'd recommend for the @p you would need to make a loop that checks the nearby players in a radius that increases until it finds a player. Which, would be getNearbyPlayers().size() make sure it's equal to 1, so you're only getting one player. Then you should assign that nearby player to a Player variable, then kill that player. As for @r it would be easier. Make a random that loops through all online players, and choose one from random, then kill that player. I can't remember what the last selector is. But yeah, that should sort you for those two.
     
    cxfredeper likes this.
  10. Offline

    cxfredeper

    @OhYes
    Thanks a lot. I was just getting a little stuck on @p.
    By the way, the other two was @a and @e. Finished coding them a second ago.
     
Thread Status:
Not open for further replies.

Share This Page