How to disable commands from console and show 'Player Not Avalible' when player is offline.

Discussion in 'Plugin Development' started by GRPGamer, Oct 31, 2012.

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

    GRPGamer

    Hello,

    How can I completely disable commands to run from the console.

    Also, how can I show a message to the sender when the player targets is offline. So if I type /send Ben, if Ben is offline, it will say something like 'Ben is not online!'.

    Thanks very much,

    George
     
  2. Offline

    Hoolean

    Last edited by a moderator: May 29, 2016
  3. Offline

    GRPGamer

    Brilliant! Thank you, such a quick response, usually I have to wait ages!

    Would I be able to do

    PHP:
    public boolean onCommand(CommandSender senderCommand cmdString labelString[] args){
        if (
    sender instanceof Player) {
              
    Player player = (Playersender;
              
    //All my commands etc.
            
    if(commandLabel
            
    } else {
              
    sender.sendMessage("You must be a player!");
              return 
    false;
            }
            
    // do something
            
    return false;
    }
    Or would I need to do that for every command?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
    MrBluebear3 likes this.
  4. Offline

    Hoolean

    No, you only need one onCommand, so the method you suggested would work perfectly :D
     
  5. Offline

    cman1885

    if(Bukkit.getPlayer(args[0])!=null){
    continue....
    }else{
    sender.sendMessage("That player is not online")
    }
     
  6. Offline

    GRPGamer

    So I would be able to add all my if(commandLabel.equalsIgnoreCase) inside there, and not add that to every one? (Just confirming xD)
     
    MrBluebear3 likes this.
  7. Offline

    Hoolean

    I already told him how to do that...

    Indeed you could!

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

    GRPGamer

    Ok, thanks.
     
  9. Offline

    cman1885

    No, you told him how to check if the sender is a player. I told him how to check if an argument matches an online player.
     
  10. Offline

    Hoolean

    Because you must of followed the link and checked -.-

    It said how to do it on that page :/
     
  11. Offline

    cman1885

    Nope, didn't check.
     
    MrBluebear3 likes this.
  12. Offline

    GRPGamer

    Thanks so much MrBlueBear, I've managed to completely tidy my code. The plugin looks so much better in the code. I will easily be able to add more commands! Thanks!

    Got a error. I imputed all the info, but it still is messing up.

    Here is my code:

    PHP:
    package me.GRPGamer.ServerStupidity;
     
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class 
    ServerStupidity extends JavaPlugin{
        public final 
    Logger logger Logger.getLogger("Minecraft");
        public static 
    ServerStupidity plugin;
     
        @
    Override
        
    public void onDisable()  {
            
    PluginDescriptionFile pdfFile this.getDescription();
            
    this.logger.info(pdfFile.getName() + " Has Now Been Disabled!");
        }
     
        @
    Override
        
    public void onEnable()  {
            
    PluginDescriptionFile pdfFile this.getDescription();
            
    this.logger.info(pdfFile.getName() + " Version " pdfFile.getVersion() + " Has Now Been Enabled!");
            
    getConfig().options().copyDefaults(true);
            
    saveConfig();
        }
     
        public 
    boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args){
            if (
    sender instanceof Player) {
                  
    Player player = (Playersender;
                  
    Player targetPlayer player.getServer().getPlayer(args[0]);
            if (
    targetPlayer == null) {
                  
    player.sendMessage(ChatColor.RED args[0] + " is not online!");
                  return 
    false;
            }
    //Slap Command
                  
    if(commandLabel.equalsIgnoreCase("slap")){
                          if(
    player.hasPermission("serverstupidity.slap")) {
                              if(
    args.length == 0){
                                  
    player.sendMessage(ChatColor.RED "Please Specify a Player!");
                              }if(
    args.length 2){
                                  
    player.sendMessage("Too many arugments!");
                              }if(
    args.length == 1){
                                  
    targetPlayer.sendMessage(ChatColor.GREEN getConfig().getString("SlapRecieve").replaceAll("%sender%"player.getName()));
                                  
    targetPlayer.damage(1);
                                  
    player.sendMessage(ChatColor.GREEN getConfig().getString("SlapSend").replaceAll("%victim%"targetPlayer.getName()));
                        }
                    }
                }
             
    //Punch Command
                
    if(commandLabel.equalsIgnoreCase("punch")){
                        if(
    player.hasPermission("serverstupidity.punch")) {
                            if(
    args.length == 0){
                                
    player.sendMessage(ChatColor.RED "Please Specify a Player!");
                            }if(
    args.length 2){
                                
    player.sendMessage("Too many arugments!");
                            }if(
    args.length == 1){
                                
    targetPlayer.sendMessage(ChatColor.GREEN getConfig().getString("PunchRecieve").replaceAll("%sender%"player.getName()));
                                
    targetPlayer.damage(2);
                                
    player.sendMessage(ChatColor.GREEN getConfig().getString("PunchSend").replaceAll("%victim%"targetPlayer.getName()));
                        }
                    }
                }
    //Poke Command
                
    if(commandLabel.equalsIgnoreCase("poke")){
                        if(
    player.hasPermission("serverstupidity.poke")) {
                            if(
    args.length == 0){
                                
    player.sendMessage(ChatColor.RED "Please Specify a Player!");
                            }if(
    args.length 2){
                                
    player.sendMessage("Too many arugments!");
                            }if(
    args.length == 1){
                                
    targetPlayer.sendMessage(ChatColor.GREEN getConfig().getString("PokeRecieve").replaceAll("%sender%"player.getName()));
                                
    player.sendMessage(ChatColor.GREEN getConfig().getString("PokeSend").replaceAll("%victim%"targetPlayer.getName()));
                            }
                        }
                }
                
    //Burn Command
                
    if(commandLabel.equalsIgnoreCase("burn")){
                        if(
    player.hasPermission("serverstupidity.burn")) {
                            if(
    args.length == 0){
                                
    player.sendMessage(ChatColor.RED "Please Specify a Player!");
                            }if(
    args.length 2){
                                
    player.sendMessage("Too many arugments!");
                            }if(
    args.length == 1){
                                
    targetPlayer.sendMessage(ChatColor.GREEN getConfig().getString("BurnRecieve").replaceAll("%sender%"player.getName()));
                                
    targetPlayer.setFireTicks(1000);
                                
    player.sendMessage(ChatColor.GREEN getConfig().getString("BurnSend").replaceAll("%victim%"targetPlayer.getName()));
                            }
                        }
                }
    //Kill Command
                
    if(commandLabel.equalsIgnoreCase("kill")){
                        if(
    player.hasPermission("serverstupidity.kill")) {
                            if(
    args.length == 0){
                                
    player.sendMessage(ChatColor.RED "Please Specify a Player!");
                            }if(
    args.length 2){
                                
    player.sendMessage("Too many arugments!");
                            }if(
    args.length == 1){
                                
    targetPlayer.sendMessage(ChatColor.GREEN getConfig().getString("KillRecieve").replaceAll("%sender%"player.getName()));
                                
    targetPlayer.setHealth(0);
                                
    player.sendMessage(ChatColor.GREEN getConfig().getString("KillSend").replaceAll("%victim%"targetPlayer.getName()));
                            }
                        }
                }
            }
            return 
    false;
        }
    }
    And when I type only /slap ingame, it shows this.

    [​IMG]

    And when I type in a command in the console, it doesen't show anything. Please help and see what I have done wrong.

    Also, how can I set up the commands so it works as ss slap, as when I tried changing it in both the .java and the plugin.yml, it says Unknown command ingame. Yes, I refreshed before exporting.

    Any Ideas anyone? Please? :(

    Please help, really want to get this done :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
    MrBluebear3 likes this.
  13. Offline

    one4me

    GRPGamer
    Currently you only continue if the sender is an instance of Player, which is why the commands do not work from the console. Second, you use args[0] before checking to see if the array is the correct length, which is why you are receiving the ArrayIndexOutOfBoundsException. Not that I agree with using a static instance of your plugin, but try the code below. I changed it a bit, so tell me if you don't understand anything.
    Code:
    package me.GRPGamer.ServerStupidity;
     
    import java.util.logging.Logger;
    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 ServerStupidity extends JavaPlugin {
      public final Logger logger = getLogger();
      public static ServerStupidity plugin;
      public void onDisable() {
      }
      public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();
      }
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        /*Slap Command*/
        if(cmd.getName().equalsIgnoreCase("slap")) {
          if(!sender.hasPermission("serverstupidity.slap")) {
            sender.sendMessage(ChatColor.RED + "You do not have permission to do that.");
            return true;
          }
          if(args.length < 1  || args.length > 2) {
            sender.sendMessage(ChatColor.RED + "Usage: /slap <player>");
            return true;
          }
          Player target = Bukkit.getPlayerExact(args[0]);
          if(target == null) {
            sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
            return true;
          }
          target.sendMessage(ChatColor.GREEN + getConfig().getString("SlapRecieve").replaceAll("%sender%", sender.getName()));
          target.damage(1);
          sender.sendMessage(ChatColor.GREEN + getConfig().getString("SlapSend").replaceAll("%victim%", target.getName()));
        }
        /*Punch Command*/
        else if(cmd.getName().equalsIgnoreCase("punch")) {
          if(!sender.hasPermission("serverstupidity.punch")) {
            sender.sendMessage(ChatColor.RED + "You do not have permission to do that.");
            return true;
          }
          if(args.length < 1  || args.length > 2) {
            sender.sendMessage(ChatColor.RED + "Usage: /punch <player>");
            return true;
          }
          Player target = Bukkit.getPlayerExact(args[0]);
          if(target == null) {
            sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
            return true;
          }
          target.sendMessage(ChatColor.GREEN + getConfig().getString("PunchRecieve").replaceAll("%sender%", sender.getName()));
          target.damage(2);
          sender.sendMessage(ChatColor.GREEN + getConfig().getString("PunchSend").replaceAll("%victim%", target.getName()));
        }
        /*Poke Command*/
        else if(cmd.getName().equalsIgnoreCase("poke")) {
          if(!sender.hasPermission("serverstupidity.poke")) {
            sender.sendMessage(ChatColor.RED + "You do not have permission to do that.");
            return true;
          }
          if(args.length < 1  || args.length > 2) {
            sender.sendMessage(ChatColor.RED + "Usage: /poke <player>");
            return true;
          }
          Player target = Bukkit.getPlayerExact(args[0]);
          if(target == null) {
            sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
            return true;
          }
          target.sendMessage(ChatColor.GREEN + getConfig().getString("PokeRecieve").replaceAll("%sender%", sender.getName()));
          sender.sendMessage(ChatColor.GREEN + getConfig().getString("PokeSend").replaceAll("%victim%", target.getName()));
        }
        /*Burn Command*/
        else if(cmd.getName().equalsIgnoreCase("burn")) {
          if(!sender.hasPermission("serverstupidity.burn")) {
            sender.sendMessage(ChatColor.RED + "You do not have permission to do that.");
            return true;
          }
          if(args.length < 1  || args.length > 2) {
            sender.sendMessage(ChatColor.RED + "Usage: /burn <player>");
            return true;
          }
          Player target = Bukkit.getPlayerExact(args[0]);
          if(target == null) {
            sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
            return true;
          }
          target.sendMessage(ChatColor.GREEN + getConfig().getString("BurnRecieve").replaceAll("%sender%", sender.getName()));
          target.setFireTicks(1000);
          sender.sendMessage(ChatColor.GREEN + getConfig().getString("BurnSend").replaceAll("%victim%", target.getName()));
        }
        /*Kill Command*/
        else if(cmd.getName().equalsIgnoreCase("kill")) {
          if(!sender.hasPermission("serverstupidity.kill")) {
            sender.sendMessage(ChatColor.RED + "You do not have permission to do that.");
            return true;
          }
          if(args.length < 1  || args.length > 2) {
            sender.sendMessage(ChatColor.RED + "Usage: /kill <player>");
            return true;
          }
          Player target = Bukkit.getPlayerExact(args[0]);
          if(target == null) {
            sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
            return true;
          }
          target.sendMessage(ChatColor.GREEN + getConfig().getString("KillRecieve").replaceAll("%sender%", sender.getName()));
          target.setHealth(0);
          sender.sendMessage(ChatColor.GREEN + getConfig().getString("KillSend").replaceAll("%victim%", target.getName()));
        }
        return true;
      }
    }
    
    A few notes:
    - cmd.getName() is used instead of commandLabel so that aliases will work.
    - I removed the check to see if the sender is an instance of Player so that the commands work from the console, however if you want to disable a command from the console, just add if(!(sender instanceof Player)) return true; before whichever command you want disabled from console.
     
  14. Offline

    GRPGamer

    How can I make the commands like ss slap? and how can I make it so that when I type /ss, it shows a list of commands, and info etc., which is also a alias to serverstupidity.
     
  15. Offline

    one4me

    You would have to add the command "serverstupidity" with an alias of "ss" in your plugin.yml. You would then check if(args[0] equalsIgnoreCase("slap")).

    For the list of commands/info part, you would do:
    if(args.length == 0) {sender.sendMessage("Whatever you want");}

    Does that help, or do you need a more detailed example?
     
  16. Offline

    GRPGamer

    Could you be abit more detailed?
     
  17. Offline

    one4me

    Alright, and could you post your plugin.yml?
     
  18. Offline

    GRPGamer

    Sure, I'll inbox you everything.
     
Thread Status:
Not open for further replies.

Share This Page