Solved Problem with permissions

Discussion in 'Plugin Development' started by _Stamp_, Dec 30, 2015.

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

    _Stamp_

    Its me again with yet again another problem xD
    So I went away learned more of java and I wanted to add some permissions to my plugin however when I went to add another command with permissions it messed all the permissions up, when I do /warn any player can now do it when before only people with the permission could and /clearchat just doesnt work at all

    Code of the admin command section
    Code:
        //ADMIN COMMANDS....
       
        if (label.equalsIgnoreCase("warn") && sender instanceof Player && sender.hasPermission("simplecommands.warn")){
            Player p = (Player) sender;
            if (args.length == 1) {
            boolean playerFound = false;
               
                for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                    if(target.getName().equalsIgnoreCase(args[0])) {
                       
                        target.sendMessage(RED + "" + BOLD + "You have been warned, do that again and you will be punished by an admin!");
                        p.sendMessage(GOLD + "" + GOLD + "You have successfuly warned  " + YELLOW + "" + BOLD + target.getName() + GOLD + "" + BOLD + "!");
                        playerFound = true;
                        break;
            }   
        }
           
                if (playerFound == false) {
                     p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(RED + "" + BOLD + "              ERROR            ");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                       
                }
               
            } else p.sendMessage(DARK_RED + "" + BOLD + "You used incorrect arguments!");
           
        }else{
            sender.sendMessage(RED + "You do not have permission to use this command");
        return true;
       
       
       
    }
        if(label.equalsIgnoreCase("clearchat") && sender instanceof Player && sender.hasPermission("simplecommands.clearchat")){
            Player p = (Player) sender;
            Bukkit.broadcastMessage("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    " + GOLD + "Chat cleared by "+ p.getDisplayName() + " !");
        }else{ sender.sendMessage(RED + "You do not have permission to use this command");
            return true;
        }
       
            return false;
    }
    }
    
    plugin.yml

    Code:
    name: SimpleCommands
    description: A plugin that contains simple commands.
    version: 5.5
    author: _Stamp_
    main: me._Stamp_.SimpleCommands.SimpleCommandsMain
    commands:
     
      sn:
        description: Shows the server name!
        usage: /<command>
     
      welcome:
        description: Says welcome!
        usage: /<command>
     
      wb:
        description: Welcomes back the player!
        usage: /<command>
     
      ty:
        description: Thanks a player!
        usage: /<command> <name>
     
      brb:
        description: Says be right back!
        usage: /<command>
     
      sch:
        description:  Shows how to use this plugin and shows help.
        usage: /<command> 
     
      warn:
        description : Warn a player (ADMIN COMMAND)
        usage: /<command> <player>
        permissions:
               simplecommands.warn:
     
      clearchat:
         description: Clear chat (ADMIN COMMAND)
         usage: /<command>
         permissions:
               simplecommands.clearchat:
              
       
    
    (I know your not meant to have caps in your package part but I started this ages ago and I haven't found any problems with using that package so far ;D)
     
  2. Offline

    mythbusterma

    @_Stamp_

    No capital letters, no underscores. It's bad practise. Just fix it, it's not hard.

    Can you please format that code? It's impossible to read.
     
  3. Offline

    PresentingTulip

    Like @mythbusterma said, your code is messy. Variables always begin with a lowercase letter, and if its a two word variable, you have to make the first letter of the second word uppercase. So like for example: int moneyEarned, instead of MoneyEarned.

    To answer your question, you have to nest the if statement checking for permission: like this:

    Code:
    if(command.getName().equalsIgnoreCase("somecommand")){
    
    if (player.hasPermission("yourplugin.somepermssion")){
    //do stuff
    
    }
    
    
    }
     
  4. Offline

    mcdorli

    1.: Use cmd.getName) instead of label
    2.: Please, learn how to write D.R.Y. code
    3.: You should store the permissions in a different section in the plugin.yml file liek
    Code:
    commands:
      command1:
        #Stuff here
    
    permissions:
      command1.permission:
        #Stuff here
    4.: java naming conventions
    5.: Please, for the love of God, don't mix one line elses if multiple line ifs.
    6.: Reinventing the wheel. getPlayer(String name) returns a player, and null if it couldn't find one with that name.
    7.: You really static imported chatcolor? You use it that much?
    
     
  5. Offline

    WolfMage1

    Ok so, first things first, please for the love of god format your code, unformatted code is painful to read.

    Code:
    if(label.equalsIgnoreCase("warn") && sender instanceof Player && sender.hasPermission("simplecommands.warn")) {
    //blah blah blah
    
    Use cmd.getName().equalsIgnoreCase("") instead of label.equalsIgnoreCase("")

    Code:
            Player p = (Player) sender;
            if (args.length == 1) {
            boolean playerFound = false;
            
                for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                    if(target.getName().equalsIgnoreCase(args[0])) {
                    
                        target.sendMessage(RED + "" + BOLD + "You have been warned, do that again and you will be punished by an admin!");
                        p.sendMessage(GOLD + "" + GOLD + "You have successfuly warned  " + YELLOW + "" + BOLD + target.getName() + GOLD + "" + BOLD + "!");
                        playerFound = true;
                        break;
            } 
        }
    
    1. I'd do if(args.length >= 2) (allows them to put in a reason for it) and then just loop through the arguments and add them to a string builder.
    2. I wouldnt loop through all the online players just to find 1 guy, just create a new player Player t = Bukkit.getServer().getPlayer(args[0]) check if the target is null, if they are send them a message saying blahblah123 isnt online, if they are warn them.
    3. You can just put § infront of a colour to translate it to a minecraft colour code http://minecraft.gamepedia.com/Formatting_codes

    EDIT:
    Oh uhmmm, I guess I should have refreshed the forums before writing out a massive post xD
     
    Last edited: Dec 30, 2015
  6. Offline

    mcdorli

    You know probably what constructors are
    Code:
    mainClassName plugin;
    public ClassName(MainClassName plugin) {
        this.plugin = plugin;
    }
    
    
    You can then simply refer to it. Yould learn about how to use constructors, they are one of the basic things you will need.
     
    WolfMage1 likes this.
  7. Offline

    _Stamp_

    Right, I have tried to do everything you guys said, I have looked into what D.R.Y means it basically means don't repeat yourself, right? Also what part of my formatting is bad? I have tried to improve it anyway
    Anyway heres my plugin.yml and my code now
    Code
    Code:
    package me.stamp.simplecommands;
    
    import static org.bukkit.ChatColor.*;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    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 SimpleCommandsMain extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
      
    public void onEnable(){
        PluginDescriptionFile pdffile = this.getDescription();
        this.logger.info(pdffile.getName() + "Has Been Enabled!");
      
    }
      
    public void onDisable(){
        PluginDescriptionFile pdffile = this.getDescription();
        this.logger.info(pdffile.getName() + "Has Been Disabled!");
          
    }
    public boolean onCommand(CommandSender sender,Command cmd ,String command,String[] args){
        if(cmd.getName().equalsIgnoreCase("sch") && sender instanceof Player){
            Player p = (Player) sender;
            p.sendMessage(DARK_RED + "" + BOLD + "=================================");
            p.sendMessage(GOLD + "" + BOLD + "              Simple Commands            ");
            p.sendMessage(DARK_RED + "" + BOLD + "=================================");
            p.sendMessage(AQUA + "" + BOLD + "You can define the server name in your server.properties");
            p.sendMessage(BLUE + "" + BOLD + "Player commands");
            p.sendMessage(AQUA + "" + ITALIC + "sch - Displays help for this plugin");
            p.sendMessage(AQUA + "" + ITALIC + "sn - Displays the servers name");
            p.sendMessage(AQUA + "" + ITALIC + "welcome - Says a welcome message from your chat");
            p.sendMessage(AQUA + "" + ITALIC + "wb - Says welcome back");
            p.sendMessage(AQUA + "" + ITALIC + "ty - Thanks a player (/ty <PlayerName>");
            p.sendMessage(AQUA + "" + ITALIC + "brb - Says be right back");
            p.sendMessage(RED + "" + BOLD + "Admin Commands");
            p.sendMessage(RED + "" + ITALIC + "warn - Warns a player (/warn <PlayerName>");
            p.sendMessage(RED + "" + ITALIC + "clearchat - Clears chat");
            return true;
        }
      
        if (cmd.getName().equalsIgnoreCase("sn") && sender instanceof Player){
            Player p = (Player) sender;
            p.sendMessage(GOLD + "The server name is " + Bukkit.getServerName());
            return true;
          
        }
      
        if (cmd.getName().equalsIgnoreCase("welcome") && sender instanceof Player){
            Player p = (Player) sender;
            p.chat(GOLD + "Welcome! I hope you have a good time at " + YELLOW + Bukkit.getServerName() + GOLD + " :D");
            return true;
      
        }
      
        if (cmd.getName().equalsIgnoreCase("wb") && sender instanceof Player){
            Player p = (Player) sender;
            p.chat(GOLD + "Welcome back! :D");
            return true;
         }
            if (cmd.getName().equalsIgnoreCase("ty") && sender instanceof Player){
            Player p = (Player) sender;
            int length = args.length;
            if (length == 1) {
            boolean playerFound = false;
              
                for (Player playerToThank : Bukkit.getServer().getOnlinePlayers()) {
                    if(playerToThank.getName().equalsIgnoreCase(args[0])) {
                      
                        playerToThank.sendMessage(GOLD + "" + BOLD + "Thanks ~" + YELLOW + "" + BOLD + p.getName());
                        p.sendMessage(GOLD + "" + GOLD + "You have successfuly thanked  " + YELLOW + "" + BOLD + playerToThank.getName() + GOLD + "" + BOLD + "!");
                        playerFound = true;
                        break;
            }  
        }
    
                if (playerFound == false) {
                     p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(RED + "" + BOLD + "              ERROR            ");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(BLUE + "" + ITALIC + "Heres some things that might of went wrong...");
                        p.sendMessage(AQUA + "" + ITALIC + "The name you entered is wrong.");
                        p.sendMessage(AQUA + "" + ITALIC + "The player you entered is offline.");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(DARK_PURPLE + "" + BOLD + "     Command you entered...      ");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(LIGHT_PURPLE + "" + BOLD + "/ty " + args[0]);
                        p.sendMessage(LIGHT_PURPLE + "" + BOLD + "(Please note that this plugin only shows the variable after the command :D)");
                  
                }
              
            } else p.sendMessage(DARK_RED + "" + BOLD + "You used incorrect arguments!");
        return true;
      
        }
        if(cmd.getName().equalsIgnoreCase("brb") && sender instanceof Player){
            Player p = (Player) sender;
            p.chat(GOLD + "" + ITALIC + "Will be right back!");
            return true;
        }
      
      
        //ADMIN COMMANDS....
      
        if (cmd.getName().equalsIgnoreCase("warn") && sender instanceof Player){
            if (!sender.hasPermission("simplecommands.warn")){
            Player p = (Player) sender;
            if (args.length >= 2) {
            boolean playerFound = false;
              
                for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                    if(target.getName().equalsIgnoreCase(args[0])) {
                      
                        StringBuilder message = new StringBuilder("");
                        for (String part : args) {
                            if (!message.toString().equals(""))
                                message.append(" ");
                            message.append(part);
                        }
                        target.sendMessage(message.toString());
                      
    
                        break;
            }  
        }
          
                if (playerFound == false) {
                     p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(RED + "" + BOLD + "              ERROR            ");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                      
                }
              
            } else p.sendMessage(DARK_RED + "" + BOLD + "You used incorrect arguments!");
      
        }else{
            sender.sendMessage(RED + "You do not have permission to use this command");
      
        return true;
      
        }
        }
    
        if(cmd.getName().equalsIgnoreCase("clearchat") && sender instanceof Player)
            if(!sender.hasPermission("simplecommands.clearchat")){
            Player p = (Player) sender;
            Bukkit.broadcastMessage("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    " + GOLD + "Chat cleared by "+ p.getDisplayName() + " !");
        }else{ sender.sendMessage(RED + "You do not have permission to use this command");
            return true;
        }
    
          
    
        return false;
    }
    }
    
    And heres my plugin.yml
    Code:
    name: SimpleCommands
    description: A plugin that contains simple commands.
    version: 7.1
    author: _Stamp_
    main: me.stamp.simplecommands.SimpleCommandsMain
    commands:
      sn:
        description: Shows the server name!
        usage: /<command>
      welcome:
        description: Says welcome!
        usage: /<command>
      wb:
        description: Welcomes back the player!
        usage: /<command>
      ty:
        description: Thanks a player!
        usage: /<command> <name>
      brb:
        description: Says be right back!
        usage: /<command>
      sch:
        description:  Shows how to use this plugin and shows help.
        usage: /<command>
      warn:
        description : Warn a player (ADMIN COMMAND)
        usage: /<command> <player>
      clearchat:
         description: Clear chat (ADMIN COMMAND)
         usage: /<command>
    permissions:
         warn.permission:
             simplecommands.warn:
         clearchat.permission:
             simplecommands.clearchat:
             
    
      
    
    EDIT:The permissions are still broke and now /clearchat displays only '/clearchat' in chat
     
  8. Offline

    Zombie_Striker

    Please don't steal micreaft's logger. Instead, use getLogger();
    Bukkit already logs your plugin. There is no need to log it twice.

    You don't need to create a player instance when sending a command. CommandSender does have a .sendmessage command.
     
  9. Offline

    _Stamp_

    Thanks Zombie_Striker, I have changed all the things you talked about however, I couldn't see how to use p.chat("") without the p, I tried using sender but it didn't work

    UPDATE: Now no permissions work :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 1, 2016
  10. Offline

    BobTheHamster9

    I create a separate class for my permissions.
    In the class you would create the permission
    Code:
    public Permission Example = new Permission("Example.Example.Example");
    You could register the permissions in onEnable()
    Code:
    Bukkit.getServer().getPluginManager().addPermission(new Permissions().Example);
    Then check if the player has the permission or not
    Code:
    if(!sender.hasPermission(new Permissions().Example){
    
    }
     
  11. Offline

    mythbusterma

    @BobTheHamster9

    Interesting idea, but that field is a constant, and as such should be public, static, and final. Also, why wouldn't you just check for a constant String?
     
  12. Offline

    _Stamp_

    So what would you guys recommend?
    I've tried a few things but non work, as I have posted above and I will put again heres my latest code.
    (Btw I'm semi-new to permissions, this is like the 3rd time I have tried it)
    Code:
    package me.stamp.simplecommands;
    import static org.bukkit.ChatColor.*;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    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 SimpleCommandsMain extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
    public void onEnable(){
        PluginDescriptionFile pdffile = this.getDescription();
        this.logger.info(pdffile.getName() + "Has Been Enabled!");
    }
    public void onDisable(){
        PluginDescriptionFile pdffile = this.getDescription();
        this.logger.info(pdffile.getName() + "Has Been Disabled!");
        
    }
    public boolean onCommand(CommandSender sender,Command cmd ,String command,String[] args){
        if(cmd.getName().equalsIgnoreCase("sch") && sender instanceof Player){
            Player p = (Player) sender;
            p.sendMessage(DARK_RED + "" + BOLD + "=================================");
            p.sendMessage(GOLD + "" + BOLD + "              Simple Commands            ");
            p.sendMessage(DARK_RED + "" + BOLD + "=================================");
            p.sendMessage(AQUA + "" + BOLD + "You can define the server name in your server.properties");
            p.sendMessage(BLUE + "" + BOLD + "Player commands");
            p.sendMessage(AQUA + "" + ITALIC + "sch - Displays help for this plugin");
            p.sendMessage(AQUA + "" + ITALIC + "sn - Displays the servers name");
            p.sendMessage(AQUA + "" + ITALIC + "welcome - Says a welcome message from your chat");
            p.sendMessage(AQUA + "" + ITALIC + "wb - Says welcome back");
            p.sendMessage(AQUA + "" + ITALIC + "ty - Thanks a player (/ty <PlayerName>");
            p.sendMessage(AQUA + "" + ITALIC + "brb - Says be right back");
            p.sendMessage(RED + "" + BOLD + "Admin Commands");
            p.sendMessage(RED + "" + ITALIC + "warn - Warns a player (/warn <PlayerName>");
            p.sendMessage(RED + "" + ITALIC + "clearchat - Clears chat");
            return true;
        }
        if (cmd.getName().equalsIgnoreCase("sn") && sender instanceof Player){
            Player p = (Player) sender;
            p.sendMessage(GOLD + "The server name is " + Bukkit.getServerName());
            return true;
        
        }
        if (cmd.getName().equalsIgnoreCase("welcome") && sender instanceof Player){
            Player p = (Player) sender;
            p.chat(GOLD + "Welcome! I hope you have a good time at " + YELLOW + Bukkit.getServerName() + GOLD + " :D");
            return true;
        }
        if (cmd.getName().equalsIgnoreCase("wb") && sender instanceof Player){
            Player p = (Player) sender;
            p.chat(GOLD + "Welcome back! :D");
            return true;
         }
            if (cmd.getName().equalsIgnoreCase("ty") && sender instanceof Player){
            Player p = (Player) sender;
            int length = args.length;
            if (length == 1) {
            boolean playerFound = false;
            
                for (Player playerToThank : Bukkit.getServer().getOnlinePlayers()) {
                    if(playerToThank.getName().equalsIgnoreCase(args[0])) {
                    
                        playerToThank.sendMessage(GOLD + "" + BOLD + "Thanks ~" + YELLOW + "" + BOLD + p.getName());
                        p.sendMessage(GOLD + "" + GOLD + "You have successfuly thanked  " + YELLOW + "" + BOLD + playerToThank.getName() + GOLD + "" + BOLD + "!");
                        playerFound = true;
                        break;
            } 
        }
                if (playerFound == false) {
                     p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(RED + "" + BOLD + "              ERROR            ");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(BLUE + "" + ITALIC + "Heres some things that might of went wrong...");
                        p.sendMessage(AQUA + "" + ITALIC + "The name you entered is wrong.");
                        p.sendMessage(AQUA + "" + ITALIC + "The player you entered is offline.");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(DARK_PURPLE + "" + BOLD + "     Command you entered...      ");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(LIGHT_PURPLE + "" + BOLD + "/ty " + args[0]);
                        p.sendMessage(LIGHT_PURPLE + "" + BOLD + "(Please note that this plugin only shows the variable after the command :D)");
                
                }
            
            } else p.sendMessage(DARK_RED + "" + BOLD + "You used incorrect arguments!");
        return true;
        }
        if(cmd.getName().equalsIgnoreCase("brb") && sender instanceof Player){
            Player p = (Player) sender;
            p.chat(GOLD + "" + ITALIC + "Will be right back!");
            return true;
        }
        //ADMIN COMMANDS....
        if (cmd.getName().equalsIgnoreCase("warn") && sender instanceof Player){
            if (!sender.hasPermission("simplecommands.warn")){
            Player p = (Player) sender;
            if (args.length >= 2) {
            boolean playerFound = false;
            
                for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                    if(target.getName().equalsIgnoreCase(args[0])) {
                    
                        StringBuilder message = new StringBuilder("");
                        for (String part : args) {
                            if (!message.toString().equals(""))
                                message.append(" ");
                            message.append(part);
                        }
                        target.sendMessage(message.toString());
                    
                        break;
            } 
        }
        
                if (playerFound == false) {
                     p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                        p.sendMessage(RED + "" + BOLD + "              ERROR            ");
                        p.sendMessage(DARK_RED + "" + BOLD + "=================================");
                    
                }
            
            } else p.sendMessage(DARK_RED + "" + BOLD + "You used incorrect arguments!");
        }else{
            sender.sendMessage(RED + "You do not have permission to use this command");
        return true;
        }
        }
        if(cmd.getName().equalsIgnoreCase("clearchat") && sender instanceof Player)
            if(!sender.hasPermission("simplecommands.clearchat")){
            Player p = (Player) sender;
            Bukkit.broadcastMessage("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    " + GOLD + "Chat cleared by "+ p.getDisplayName() + " !");
        }else{ sender.sendMessage(RED + "You do not have permission to use this command");
            return true;
        }
        
        return false;
    }
    }
    
    //And heres my plugin.yml btw this line isnt in my code.
    
    name: SimpleCommands
    description: A plugin that contains simple commands.
    version: 7.1
    author: _Stamp_
    main: me.stamp.simplecommands.SimpleCommandsMain
    commands:
      sn:
        description: Shows the server name!
        usage: /<command>
      welcome:
        description: Says welcome!
        usage: /<command>
      wb:
        description: Welcomes back the player!
        usage: /<command>
      ty:
        description: Thanks a player!
        usage: /<command> <name>
      brb:
        description: Says be right back!
        usage: /<command>
      sch:
        description:  Shows how to use this plugin and shows help.
        usage: /<command>
      warn:
        description : Warn a player (ADMIN COMMAND)
        usage: /<command> <player>
      clearchat:
         description: Clear chat (ADMIN COMMAND)
         usage: /<command>
    permissions:
         warn.permission:
             simplecommands.warn:
         clearchat.permission:
             simplecommands.clearchat:
            
    
    Also as I said above, /clearchat only displays '/clearchat' nothing else, that usually means I have to return true, right? Well I tried that and no luck :(

    Thanks in advance!

    ~Stamp
     
  13. Offline

    Zombie_Striker

    https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html
    So don't use static imports.
    Stop stealing minecrafts logger! Use .getLogger instead.

    I'll allow it, but you should not have to have "Main" in your class name.
    Bukkit already logs your plugins for you. You do not need this.
    You seem to be using "p.sendmess" and "sender.sendmess" interchangably. Stick to either using only "p.sendmess" or (recommened) "sender.sendmess" if both the console and 'p' can access that command.
     
  14. Offline

    _Stamp_

    Right, I have did all that and heres my new code!
    Code:
    package me.stamp.simplecommands;
    
    
    
    import java.util.logging.Logger;
    
    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;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class SimpleCommands extends JavaPlugin {
    public final Logger logger = Bukkit.getLogger();
       
    public void onEnable(){
    }
    public void onDisable(){
           
    }
    public boolean onCommand(CommandSender sender,Command cmd ,String command,String[] args){
        if(cmd.getName().equalsIgnoreCase("sch") && sender instanceof Player){
            sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "================================="); 
            sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "              Simple Commands            "); 
            sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "================================="); 
            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "You can define the server name in your server.properties");
            sender.sendMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "Player commands");
            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.ITALIC + "sch - Displays help for this plugin");
            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.ITALIC + "sn - Displays the servers name");
            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.ITALIC + "welcome - Says a welcome message from your chat");
            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.ITALIC + "wb - Says welcome back");
            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.ITALIC + "ty - Thanks a player (/ty <PlayerName>");
            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.ITALIC + "brb - Says be right back");
            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Admin Commands");
            sender.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC + "warn - Warns a player (/warn <PlayerName>");
            sender.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC + "clearchat - Clears chat");
            return true;
        }
       
        if (cmd.getName().equalsIgnoreCase("sn") && sender instanceof Player){
           
            sender.sendMessage(ChatColor.GOLD + "The server name is " + Bukkit.getServerName());
            return true;
           
        }
       
        if (cmd.getName().equalsIgnoreCase("welcome") && sender instanceof Player){
           
           Bukkit.getPlayerExact(getName()).chat(ChatColor.GOLD + "Welcome! I hope you have a good time at " + ChatColor.YELLOW + Bukkit.getServerName() + ChatColor.GOLD + " :D");
           return true;
       
        }
       
        if (cmd.getName().equalsIgnoreCase("wb") && sender instanceof Player){
           
            Bukkit.getPlayerExact(getName()).chat(ChatColor.GOLD + "Welcome back! :D");
            return true;
        }
            if (cmd.getName().equalsIgnoreCase("ty") && sender instanceof Player){
           
            int length = args.length;
            if (length == 1) {
            boolean playerFound = false;
               
                for (Player playerToThank : Bukkit.getServer().getOnlinePlayers()) {
                    if(playerToThank.getName().equalsIgnoreCase(args[0])) {
                       
                        playerToThank.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "Thanks ~" + ChatColor.YELLOW + "" + ChatColor.BOLD + sender.getName());
                        sender.sendMessage(ChatColor.GOLD + "" + ChatColor.GOLD + "You have successfuly thanked  " + ChatColor.YELLOW + "" + ChatColor.BOLD + playerToThank.getName() + ChatColor.GOLD + "" + ChatColor.BOLD + "!");
                        playerFound = true;
                        break;
            }   
        }
    
                if (playerFound == false) {
                    sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "================================="); 
                       sender.sendMessage(ChatColor.RED + "" +ChatColor. BOLD + "              ERROR            "); 
                       sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "================================="); 
                       sender.sendMessage(ChatColor.BLUE + "" + ChatColor.ITALIC + "The name you entered is wrong."); 
                       sender.sendMessage(ChatColor.AQUA + "" + ChatColor.ITALIC + "The player you entered is offline.");
                       sender.sendMessage(ChatColor.DARK_RED + "" +ChatColor. BOLD + "=================================");
                       sender.sendMessage(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "     Command you entered...      ");
                       sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "=================================");
                       sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "/ty " + args[0]);
                       sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "(Please note that this plugin only shows the variable after the command :D)");
                   
                }
               
            } else sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "You used incorrect arguments!");
        return true;
       
        }
        if(cmd.getName().equalsIgnoreCase("brb") && sender instanceof Player){
           
            Bukkit.getPlayerExact(getName()).chat(ChatColor.GOLD + "" + ChatColor.ITALIC + "Will be right back!");
            return true;
        }
       
       
        //ADMIN COMMANDS....
       
        if (cmd.getName().equalsIgnoreCase("warn") && sender instanceof Player){
            if (!sender.hasPermission("simplecommands.warn")){
           
            if (args.length >= 2) {
            boolean playerFound = false;
               
                for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                    if(target.getName().equalsIgnoreCase(args[0])) {
                       
                        StringBuilder message = new StringBuilder("");
                        for (String part : args) {
                            if (!message.toString().equals(""))
                                message.append(" ");
                            message.append(part);
                        }
                        target.sendMessage(message.toString());
                       
    
                        break;
            }   
        }
           
                if (playerFound == false) {
                    sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "================================="); 
                       sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "              ERROR            "); 
                       sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "================================="); 
                       
                }
               
            } else sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "You used incorrect arguments!");
       
        }else{
            sender.sendMessage(ChatColor.RED + "You do not have permission to use this command");
       
        return true;
       
        }
        }
    
        if(cmd.getName().equalsIgnoreCase("clearchat") && sender instanceof Player)
            if(!sender.hasPermission("simplecommands.clearchat")){
           
           Bukkit.broadcastMessage("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    " + ChatColor.GOLD + "Chat cleared by "+ Bukkit.getName() + " !");
        }else{ sender.sendMessage(ChatColor.RED + "You do not have permission to use this command");
           return true;
        }
    
           
    
        return false;
    }
    }
    
    
     
  15. Offline

    Zombie_Striker

    @_Stamp_
    For the commands {SCH, SN, Welcome, wb, ty, brb, warn} you never actually do anything that deals with the sender being a player. Because of this, you do not even need to check if the sender is a player.
    This may be your issue. This line never closes. This may be your issue. If you are using an IDE, go to any lines that are marked with an error and fix those problems. If you're not using an IDE (you're using Notepad or something similar), then please use one. It will help you show any issues your code may have.

    Also, try formatting your code, that may help you see any other problems.
     
  16. Offline

    _Stamp_

    I think my problem is in the plugin.yml, how would I set it up with permissions in?
     
  17. Offline

    BobTheHamster9

    Your using the wrong ChatColor
    You should be using org.bukkit.ChatColor
     
  18. Offline

    _Stamp_

    Fixed, I changed it to the bukkit one
     
  19. Offline

    JoaoBM

    @_Stamp_ Problem solved? If not any errors on console?
     
  20. Offline

    _Stamp_

    Sadly no, but I still think the problem is in my plugin.yml could anyone give a example of how to list the permissions?
    I have tried looking at the bukkit one but its way to confusing all that default stuff.

    EDIT - Yes, I have been messing around a bit and saw I did a '!' before of sender.hasPermission
    I removed that and it works fine now, just need to look into how to use a string builder for my /warn
     
    Last edited: Feb 15, 2016
Thread Status:
Not open for further replies.

Share This Page