Staffchat plugin

Discussion in 'Spigot Plugin Help' started by Tomdaarom, Jun 23, 2017.

  1. Hello, I made this staffchat plugin. It used 3 different messages. /mc /sc and /ac (mod Chat, Senior Chat and Admin Chat) I can use the Mod chat, but when I try to use the /sc and /ac nothing works. I don't get an error in the console either.

    Code (Text):
    package staff.chat;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main
      extends JavaPlugin
    {
      public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings)
      {
        if (command.getName().equalsIgnoreCase("mc"))
        {
          Player p = (Player)commandSender;
          String prefix = "§7<§9Mod Chat§7>";
          String message = "";
          if (p.hasPermission("staffchat.moderator.use"))
          {
            for (int i = 0; i < strings.length; i++) {
              message = message + " " + strings[i];
            }
            for (Player r : Bukkit.getOnlinePlayers()) {
              if (r.hasPermission("staffchat.moderator.see")) {
                r.sendMessage(prefix + " §f" + p.getName() + "§7:§e " + message);
              }
            }
          }
          else
          {
            return true;
          }
        }
        return false;
      }
      public boolean onCommand1(CommandSender commandSender, Command command, String s, String[] strings)
      {
        if (command.getName().equalsIgnoreCase("sc"))
        {
          Player p = (Player)commandSender;
          String prefix = "§7<§4Senior Chat§7>";
          String message = "";
          if (p.hasPermission("staffchat.senior.use"))
          {
            for (int i = 0; i < strings.length; i++) {
              message = message + " " + strings[i];
            }
            for (Player r : Bukkit.getOnlinePlayers()) {
              if (r.hasPermission("staffchat.senior.see")) {
                r.sendMessage(prefix + " §f" + p.getName() + "§7:§3 " + message);
              }
            }
          }
          else
          {
            return true;
          }
        }
        return false;
      }  
      public boolean onCommand2(CommandSender commandSender, Command command, String s, String[] strings)
      {
        if (command.getName().equalsIgnoreCase("ac"))
        {
          Player p = (Player)commandSender;
          String prefix = "§7<§5Admin Chat§7>";
          String message = "";
          if (p.hasPermission("staffchat.admin.use"))
          {
            for (int i = 0; i < strings.length; i++) {
              message = message + " " + strings[i];
            }
            for (Player r : Bukkit.getOnlinePlayers()) {
              if (r.hasPermission("staffchat.admin.see")) {
                r.sendMessage(prefix + " §f" + p.getName() + "§7:§d " + message);
              }
            }
          }
          else
          {
            return true;
          }
        }
        return false;
      }
    }
    Code (Text):
    name: BCStaffChat
    author: OfficialNL
    version: 1.0
    main: staff.chat.Main
    commands:
      mc:
     
      sc:
     
      ac:
     
     
  2. Hello!

    Is this al in one class? Because as far as I know you can only have one onCommand method in a class.
    So when you just simply put a 1 or a 2 behind the method it doesn't call the method when the player is typing a command.
    What you could do is check in the first onCommand for a second and third command. Or make 2 other classes with
    the onCommand method.

    Greets, -Thom.
     
  3. Hi Thom, I made them in different classes now, but the plugin isn't working.
    Main Class :
    Code (Text):
    package staff.chat;

    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
        {
        this.getCommand("mc").setExecutor(new Moderator(this));
        this.getCommand("sc").setExecutor(new Senior(this));
        this.getCommand("ac").setExecutor(new Admin(this));
        }
    }
     
    Code (Text):
    package staff.chat;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Admin implements CommandExecutor{
        Main plugin;
       
        public Admin(Main main)
        {
            this.plugin=main;
        }

          public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings)
          {
            if (command.getName().equalsIgnoreCase("ac"))
            {
              Player p = (Player)commandSender;
              String prefix = "§7<§5Admin Chat§7>";
              String message = "";
              if (p.hasPermission("staffchat.admin.use"))
              {
                for (int i = 0; i < strings.length; i++) {
                  message = message + " " + strings[i];
                }
                for (Player r : Bukkit.getOnlinePlayers()) {
                  if (r.hasPermission("staffchat.admin.see")) {
                    r.sendMessage(prefix + " §f" + p.getName() + "§7:§d" + message);
                  }
                }
              }
              else
              {
                return true;
              }
            }
            return false;
          }  
        }
    Code (Text):
    package staff.chat;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Moderator implements CommandExecutor{
        Main plugin;
       
        public Moderator(Main main)
        {
            this.plugin=main;
        }

          public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings)
          {
            if (command.getName().equalsIgnoreCase("mc"))
            {
              Player p = (Player)commandSender;
              String prefix = "§7<§9Mod Chat§7>";
              String message = "";
              if (p.hasPermission("staffchat.mod.use"))
              {
                for (int i = 0; i < strings.length; i++) {
                  message = message + " " + strings[i];
                }
                for (Player r : Bukkit.getOnlinePlayers()) {
                  if (r.hasPermission("staffchat.mod.see")) {
                    r.sendMessage(prefix + " §f" + p.getName() + "§7:§e" + message);
                  }
                }
              }
              else
              {
                return true;
              }
            }
            return false;
          }  
        }
    Code (Text):
    package staff.chat;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Senior implements CommandExecutor{
        Main plugin;
       
        public Senior(Main main)
        {
            this.plugin=main;
        }

          public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings)
          {
            if (command.getName().equalsIgnoreCase("sc"))
            {
              Player p = (Player)commandSender;
              String prefix = "§7<§4Senior Chat§7>";
              String message = "";
              if (p.hasPermission("staffchat.senior.use"))
              {
                for (int i = 0; i < strings.length; i++) {
                  message = message + " " + strings[i];
                }
                for (Player r : Bukkit.getOnlinePlayers()) {
                  if (r.hasPermission("staffchat.senior.see")) {
                    r.sendMessage(prefix + " §f" + p.getName() + "§7:§3" + message);
                  }
                }
              }
              else
              {
                return true;
              }
            }
            return false;
          }  
        }
     
  4. plugin.yml
    Code (Text):
    name: BCStaffChat
    author: OfficialNL
    version: 1.0
    main: staff.chat.Main
    commands:
      mc:
      sc:
      ac:
     
     
  5. You need to register the commands inside the onEnable method and then register the events.
     
  6. You don't need the if checks anymore. Each command class is defined to its own command already.

    They already registered the commands, and no events need to be registered.
     
  7. I think they should be called in the onEnable method.. Right now their not working and the only reason why not I see is because
    onEnable is not in here.
     
  8. Ohh yeah I didn't notice that lol. The registering need to be put inside the onEnable method.
    Code (Java):
    public void onEnable() {

    }
    You can't just have an empty code block and expect it to do things. Computers can't read minds :p
     
  9. Hello, Tomdaarom.

    Is your plugin working now? Or do you still need help with it.

    -Thom.