Add spaces in commands

Discussion in 'Plugin Development' started by C0lA_K1nG, Jul 7, 2012.

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

    C0lA_K1nG

    I cant add spaces in commads i have to use a _ insted of a space

    CODE:

    package me.C0lA_K1nG.MCSiri;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    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 MCSiri extends JavaPlugin{
    public final Logger logger = Bukkit.getLogger();
    public static MCSiri plugin;

    @Override
    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Has been disabled");
    }

    @Override
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + pdfFile.getDescription() + " Has been enabled");
    }

    public boolean onCommand (CommandSender sender, Command command, String label, String[] args[])
    {
    Player player = (Player) sender;

    if (args.length > 6) {
    if (label.equalsIgnoreCase("Siri kill me"))
    {
    sendMessage(player,ChatColor.GOLD + "[Siri] NO. You're my bestfriend");
    return true;
    }

    if (label.equalsIgnoreCase("Siri_Will_You_Marry_Me"))
    {
    sendMessage(player,ChatColor.GOLD + "[Siri] Yes i love you i've had this feeling eversince you joined ");
    return true;
    }

    if (label.equalsIgnoreCase("Hey_Siri"))
    {
    sendMessage(player,ChatColor.GOLD + "[Siri] Hey");
    return true;
    }

    if (label.equalsIgnoreCase("Siri_Change_Gamemode"))
    {
    sendMessage(player, ChatColor.GOLD + "[Siri] Changed {Do /siri_change_gamemode to return to your previous gamemode");

    if (player.getGameMode().equals(GameMode.CREATIVE))
    {
    player.setGameMode(GameMode.SURVIVAL);
    }
    else if (player.getGameMode().equals(GameMode.SURVIVAL))
    {
    player.setGameMode(GameMode.CREATIVE);
    return true;
    }


    return false;}
    return false;}
    return false;
    }

    public void sendMessage(Player player, String msg)
    {
    player.sendMessage(msg);
    }
    }
     
  2. Offline

    Jogy34

    the String args[] holds everything after the initial command so if you typed 'siri kill me' args[0] == "kill" and args[1] == "me"
     
  3. Offline

    C0lA_K1nG

    So like
    if (label.equalsIgnoreCase("Hey_Siri"args[1]))
    {
    sendMessage(player,ChatColor.GOLD + "[Siri] Hey");
    return true;

    Or

    if (label.equalsIgnoreCase("Hey_Siri"args[0]))
    {
    sendMessage(player,ChatColor.GOLD + "[Siri] Hey");
    return true;
     
  4. Offline

    Jogy34

    it would be
    Code:
    if(cmd.getName().equalsIgnoreCase("Hey") && args[0].equalsIgnoreCas("Siri"))
    {
    sendMessage(player,ChatColor.GOLD + "[Siri] Hey");
    return true;
    }
    //this would work if the player typed "/Hey siri" in the chat
    //The cmd.getName() is just a preferance
     
  5. Offline

    C0lA_K1nG

    Thank you
     
  6. Offline

    Jogy34

    no problem
     
  7. Offline

    C0lA_K1nG

    Wait would the plugins.yml be
    hey siri:
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    No, it is
    Code:
    hey:
    as arguments need not be declared in pluhin.yml
     
  9. Offline

    C0lA_K1nG

    Now it gives me a error on && args[0].equalsIgnoreCas("Siri"))


    Error:
    Cannot invoke equalsIgnoreCase(String) on the array type String[]
     
  10. Offline

    Jogy34

    paste your code
     
  11. Offline

    C0lA_K1nG

    package me.C0lA_K1nG.MCSiri;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    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 MCSiri extends JavaPlugin{
    public final Logger logger = Bukkit.getLogger();
    public static MCSiri plugin;

    @Override
    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Has been disabled");
    }

    @Override
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + pdfFile.getDescription() + " Has been enabled");
    }

    public boolean onCommand (CommandSender sender, Command command, String label, String[] args[])
    {
    Player player = (Player) sender;

    if(label.equalsIgnoreCase("Hey") && args[0].equalsIgnoreCas("Siri"))
    {
    sendMessage(player,ChatColor.GOLD + "[Siri] Hey");
    return true;
    }

    return false;}
    return false;}
    return false;
    }

    public void sendMessage(Player player, String msg)
    {
    player.sendMessage(msg);
    }
    }
     
  12. Offline

    Jogy34

    should be equalsIgnoreCase("Siri")
     
  13. Offline

    C0lA_K1nG

    Now i get a internal error when i do /hey or /hey siri that is listed in my plugin.yml that is:

    23:36:32 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hey'
    in plugin MCSiri v2.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    6)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    79)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:821)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)

    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
    at me.C0lA_K1nG.MCSiri.MCSiri.onCommand(MCSiri.java:32)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 12 more
    >

    Here Is My Code:

    package me.C0lA_K1nG.MCSiri;

    import java.util.logging.Logger;

    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.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MCSiri extends JavaPlugin{
    public final Logger logger = Bukkit.getLogger();
    public static MCSiri plugin;

    @Override
    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Has been disabled");
    }

    @Override
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been enabled");
    }

    @SuppressWarnings("null")
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;
    String label = null;
    if(label.equalsIgnoreCase("Hey") && args[0].equalsIgnoreCase("Siri")){
    player.sendMessage(ChatColor.GOLD + "[Siri]: Hey");
    }
    return false;
    }
    }
     
  14. Offline

    JxAxVxAx

    You Can Try Doing The Follow Steps

    1. dont use

    public final Logger logger = Bukkit.getLogger();

    change it into

    public final Logger logger = Logger.getLogger("Minecraft");




    2. you can remove

    String label = null;

    and

    SuppressWarnings("null")



    3.


    Replace


    if(label.equalsIgnoreCase("Hey") && args[0].equalsIgnoreCase("Siri")){

    With

    if(commandLabel.equalsIgnoreCase("Hey") && args[0].equalsIgnoreCase("Siri")){


    Hope It helps
     
  15. Offline

    C0lA_K1nG

    OMG THANKS SOO MUCH

    Yeah i got it to work but now i can't add anymore booleans!

    CODE:

    package me.C0lA_K1nG.MCSiri;

    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 MCSiri extends JavaPlugin{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static MCSiri plugin;

    @Override
    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Has been disabled");
    }

    @Override
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been enabled");
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("Hey") && args[0].equalsIgnoreCase("Siri")){
    player.sendMessage(ChatColor.GOLD + "[Siri]: Hey");
    }
    return false;
    }
    }

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

    one4me

    Make sure you check if the sender is a player before casting it as such. That can be achieved by adding a simple if statement
    Code:
    if(!(sender instanceof Player)) {
      return false;
    }
    
    Or you can set up your onCommand like this, so that it works from console.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      if(cmd.getName().equalsIgnoreCase("Hey") && args[0].equalsIgnoreCase("Siri")) {
        sender.sendMessage(ChatColor.GOLD + "[Siri]: Hey");
        return true;
      }
      return false;
    }
    
    And if you planning on adding more siri commands I'd suggest changing the command from "hey" to "siri" so that you don't start conflicting with other plugins.
     
  17. Offline

    C0lA_K1nG

    Do it put if(!(sender instanceof Player)) {
    return false;
    }
    above public boolean
     
Thread Status:
Not open for further replies.

Share This Page