HI

Discussion in 'Spigot Help' started by Newpredator, May 1, 2015.

  1. Hellow, i´m am Newpredator i have an a dude for java, i´m New in this language and i don´t know put 2 commands in the same class pls respond me :D
     
  2. Please fix your English, even Google translate would make a better post (no offense). Also please use a descriptive title for your thread, not just "Hi". Try something like "Multiple Commands Help"

    Ok, and now to answer your question:
    Code (Text):
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
        switch(cmd.getLabel().toLowerCase())
        {
            case "firstcommand":
                //Do Command Stuff Here
                return true;
            case "secondcommand":
                //Do Command Stuff Here
                return true;
        }
        return false;
    }
     
  3. Don't use the label plz <3
     
  4. or
    Code (Text):
    public boolean aCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("blah") {
            // stuff
        }
    }
    Code (Text):
    public boolean anotherCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("blah2") {
            // stuff
        }
    }
    I do not believe the use of getLabel() is recommended.
     
  5. Not found, I put /info and respond with /info

    Code (Text):
    package me.newpredator;

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class UtilCommands
      extends JavaPlugin
    {
      public static final Logger log = Logger.getLogger("Minecraft:");
     
      public void onEnable()
      {
        log.log(Level.INFO, "Plugin Activated");
        if (!Config.configFile.exists()) {
          Config.save();
        }
        Config.load();
      }
     
      public void onDisable() {}
     
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
          switch(cmd.getLabel().toLowerCase())
          {
              case "GETNAME":
                  Player player = (Player)sender;
                  sender.sendMessage("");
                  sender.sendMessage(Config.MSG);
                  sender.sendMessage("");
                  sender.sendMessage(ChatColor.GREEN + sender.getName());
                  player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
                  return true;
              case "INFO":
                  Player player1 = (Player)sender;
                  sender.sendMessage("");
                  sender.sendMessage(Config.INFO);
                  sender.sendMessage("");
                  sender.sendMessage(ChatColor.GREEN + sender.getName());
                  player1.playSound(player1.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
                  return true;
          }
          return false;
      }
    }
     
     
  6. I put the command in the chat (/info) and this contest /info WTF

    Code (Text):

    package me.newpredator;

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class UtilCommands
      extends JavaPlugin
    {
      public static final Logger log = Logger.getLogger("Minecraft:");
      public void onEnable()
      {
        log.log(Level.INFO, "Plugin Activated");
        if (!Config.configFile.exists()) {
          Config.save();
        }
        Config.load();
      }
      public void onDisable() {}
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
          switch(cmd.getLabel().toLowerCase())
          {
              case "GETNAME":
                  Player player = (Player)sender;
                  sender.sendMessage("");
                  sender.sendMessage(Config.MSG);
                  sender.sendMessage("");
                  sender.sendMessage(ChatColor.GREEN + sender.getName());
                  player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
                  return true;
              case "INFO":
                  Player player1 = (Player)sender;
                  sender.sendMessage("");
                  sender.sendMessage(Config.INFO);
                  sender.sendMessage("");
                  sender.sendMessage(ChatColor.GREEN + sender.getName());
                  player1.playSound(player1.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
                  return true;
          }
          return false;
      }
    }
     
     
  7. Try putting your case strings in lower case, after all you do put the command to lower case.
     
  8. Good luck on learning ;)
     
  9. Why not use this?

    Code (Text):
    public boolean onCommand (blah blah blah) {
    if (cmd.getName().equalsIgnoreCase("firstcommand")) {
    // Code for 1st command
    } else if (cmd.getName().equalsIgnoreCase("secondcommand")){
    // Code for 2nd Command
    } else {
    return false;
    }
    }
    It's much easier and simpler to use.
     
    #11 Ethernet22, May 1, 2015
    Last edited: May 1, 2015
  10. Switch statements are smaller, simpler, and look better
     
  11. In my opinion it's quite the opposite; however this is my opinion and you have yours too.

    To the OP, make sure you check if the sender is an instance of a Player when casting to one. Currently, if you were to run the command in the console you would get an error.
     
  12. Well what about the errors from both your ifs? You didn't close the second set of brackets.
     
    • Winner Winner x 1
  13. Oops, apologies for that. Fixed it. (Not used to typing outside my IDE without auto-complete :p) That was an accident and just a suggestion; not the actual code he should use himself, but rather he should learn and type the code himself instead.
     
    #15 Ethernet22, May 1, 2015
    Last edited: May 1, 2015
  14. Not found help

    Code (Text):
    package me.newpredator;

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class UtilCommands
      extends JavaPlugin
    {
      public static final Logger log = Logger.getLogger("Minecraft:");
     
      public void onEnable()
      {
        log.log(Level.INFO, "Plugin Activated");
        if (!Config.configFile.exists()) {
          Config.save();
        }
        Config.load();
      }
     
      public void onDisable() {}
     
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
          switch(cmd.getLabel().toLowerCase())
          {
              case "getname":
                  Player player = (Player)sender;
                  sender.sendMessage("");
                  sender.sendMessage(Config.msg);
                  sender.sendMessage(ChatColor.GREEN + sender.getName());
                  sender.sendMessage("");
                  player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
                  return true;
              case "info":
                  Player player1 = (Player)sender;
                  sender.sendMessage("");
                  sender.sendMessage(Config.info);
                  sender.sendMessage("");
                  sender.sendMessage(Config.youtube);
                  player1.playSound(player1.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
                  return true;
          }
          return false;
      }
    }