ChatBot

Discussion in 'Plugin Development' started by MaHl111, Sep 3, 2016.

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

    MaHl111

    Hello Guys,

    I making ChatBot plugin which will contains all importants things for server, like Anti-Curse, Anti-Spam, MOTDs, Answers, Announces and much more. But I'm here to ask you for help with issues in my plugin.

    Right now, I working on CommandsExecution, for some reasons, default command "/chatbot" or "/chatbot help" for help works fine, but problem is with other args commands, I try to repair all visible errors in console but it looks like commands are not executed. I'll put you script down here:

    Code:java
    1. package sk.mahl11.chatbot;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.entity.Player;
    12.  
    13. public class Commands implements CommandExecutor {
    14.  
    15. Main plugin;
    16. //FileConfiguration config;
    17. File cf;
    18.  
    19. public Commands(Main plugin){
    20. this.plugin = plugin;
    21. }
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    24. String chatbot = plugin.getConfig().getString("chatbot-name").replaceAll("&", "§");
    25.  
    26. if(sender instanceof Player){
    27. Player player = (Player) sender;
    28. if(!sender.hasPermission("chatbot") || (!sender.isOp())){
    29. sender.sendMessage(chatbot + ChatColor.RED + ChatColor.BOLD + " You dont have permission for that!");
    30. return true;
    31. }
    32. else if(cmd.getName().equalsIgnoreCase("chatbot")){
    33. if(args.length == 0 || args[0].equals("help")){
    34. //Help
    35. sender.sendMessage(ChatColor.GOLD + "---------[ " + chatbot + "]---------");
    36. sender.sendMessage("- /chatbot help");
    37. sender.sendMessage("- /chatbot motd <message>");
    38. sender.sendMessage(ChatColor.GOLD + "---------[ " + chatbot + "]---------");
    39.  
    40. }
    41. }
    42. else if(!sender.hasPermission("chatbot.admin") || (!sender.isOp())){
    43. sender.sendMessage(chatbot + ChatColor.RED + ChatColor.BOLD + " You are not Admin!");
    44. return true;
    45. }else{
    46. if(args[0].equalsIgnoreCase("reload")){
    47. //Reload Config
    48. FileConfiguration config = YamlConfiguration.loadConfiguration(cf);
    49. return true;
    50. }
    51. else if(args[0].equalsIgnoreCase("announce")){
    52. //Adding Messages
    53.  
    54.  
    55.  
    56. }
    57. else if(args[0].equalsIgnoreCase("motd")){
    58. if(args.length == 1){
    59. sender.sendMessage(chatbot + ChatColor.RED + "Please specify a message!");
    60. return true;
    61. }
    62. // I'll
    63. }
    64. }
    65. }
    66.  
    67. /* StringBuilder str = new StringBuilder();
    68.   for(int i = 1; i < args.length+1; i++){
    69.   str.append(args[I] + " ");
    70.  
    71.   }
    72.   String motd = str.toString();
    73.   plugin.getConfig().set("motd", motd);
    74.   plugin.saveConfig();
    75.   sender.sendMessage(chatbot + "MOTD is now: " + motd);*/
    76. return true;
    77. }
    78.  
    79. }
    80. [/I]


    I really don't know how fix it, I'll be happy if you help me with it :)

    Thank you! :D
     
    Last edited: Sep 3, 2016
  2. You got there a little mistake I think.
    Use an other permission (not only "chatbot") like "chatbot.default"

    - Check first if the command equals "chatbot".
    - Then check if the player is OP or has the permission
    - Now check if there are some arguments
    - Finally do what you wish for

    Also use:
    Code:
    if(player.hasPermission("something.something")) {
        //do stuff
    } else {
        //no permission-message
    }
    instead of checking if the player doesn´t have the permission and then go ahead.
    I really hope I could help you, if you got more questions: reply to me
     
  3. Offline

    Zombie_Striker

    You are getting this problem because of a logic error
    What this is saying is to check for the other commands only if the command sent is not chatbot. To fix this, fix your encapsulation.

    I highly recommend fixing your format, as that will help you (and anyone else reading your code) see errors like this.
     
  4. @Zombie_Striker yeah, what I said:
     
Thread Status:
Not open for further replies.

Share This Page