Cant figure out What is wrong!

Discussion in 'Plugin Development' started by LuckyLuckiest, Dec 23, 2016.

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

    LuckyLuckiest

    Hey guys, I did try to do /regen <health/feed> command, I succeed on making the code alone :p I searched in the internet how to make a Command, but I did went sooo far that I didnt figure out what is wrong!
    There is no errors in the console, but when I do /regen health or /regen feed it dont work, it give me the Usage that I did made, Could any one help ?
    Code:
    package com.exoticcube.essentials.Commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandBarsRegen implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String label, String[] args) {
           
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be a player to use this command!");
               
            } else {
            Player p = (Player) sender;
            if(label.equalsIgnoreCase("regen")) {
                if(args.length <= 1) {
                    p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                            + ChatColor.GRAY + " Usage: " + ChatColor.RED
                            + "/regen <health/feed>");
                        } else {
                            if(args[0].equalsIgnoreCase("health")) {
                            Player target = Bukkit.getPlayerExact(args[1]);
                            if (target == null) {
                                if (p.hasPermission("exoticcube.regen.health")) {
                                p.setHealth(20.0);
                                p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                        + ChatColor.GRAY + " You healed" + ChatColor.GOLD + args[1]);
                                }
                            }
                        }
                    }
                } else {
                    if(args[0].equalsIgnoreCase("feed")) {
                        Player target = Bukkit.getPlayerExact(args[1]);
                        if (target == null) {
                        if (p.hasPermission("exoticcube.regen.feed")) {
                            p.setFoodLevel(20);
                            p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                    + ChatColor.GRAY + " You feeded" + ChatColor.GOLD + args[1]);
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
    
     
  2. Offline

    Ganga

    Are your p.sendMessage("...."); inside the else{} getting displayed?

    EDIT: Did you even write the playername as an argument?? Your Command says /regen <...> instead of /regen <health..> <player>
     
    Last edited: Dec 23, 2016
  3. Offline

    MrGeneralQ

    Your code is very messy , try return statements instead of brackets.

    There is alot todo. It's hard to say eveything since I'm on The train.

    But you are looking for a player without checking The length of Your args. If it doesn't Fins The player you will have nullPointerException

    And The order of Your brackets is messed up a bit
     
  4. Offline

    RenditionsRule

    You have put "if(args.length <= 1)". This will trigger if you have 1 argument or less, such as '/regen health' or '/regen feed'. Rework your logic a bit.
     
  5. Offline

    Ganga

    Code:
    Player target = Bukkit.getPlayerExact(args[1]);
                            if (target == null) {
    As I get it, it means that if your second argument is your own Playername it will regen your own health. If its any other player it will do nothing. In conclusion you could delete
    Player target = Bukkit.getPlayerExact(args[1]);
    if (target == null) {}
     
  6. Offline

    LuckyLuckiest

    Im new to coding, so Im sorry if I did alot of mistakes :(

    Once players do /regen health <playername> and the player is not online, the guy who executed the command will get a message saying that the player is not online!

    Could I please now what does return true; return false; return; return null; means

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 23, 2016
  7. Offline

    MrGeneralQ

    No worries bud! Everybody has to Learn. How much you know about Java already?
     
  8. Offline

    LuckyLuckiest

    I did found that, once I do /regen health <playername> or /regen feed <playername> it works, but once I do /regen health or /regen feed it dont heal or feed the player who did the command!
    I did learn Java, but not that much! I know Bukkit more then Java, I knew how to make commands, How to use Listeners, and Chat managments!

    Also, I did something to the code, I dont know if it is good!
    Code:
    package com.exoticcube.essentials.Commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandBarsRegen implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String label, String[] args) {
          
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be a player to use this command!");
              
            } else {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("regen")) {
                if(args.length != 2) {
                    p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                            + ChatColor.GRAY + " Usage: " + ChatColor.RED
                            + "/regen <health/feed>");
                            } else {
                                if(args[0].equalsIgnoreCase("health")) {
                                if (p.hasPermission("exoticcube.regen.health")) {
                                p.setHealth(20.0);
                                p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                        + ChatColor.GRAY + " You healed " + ChatColor.GOLD + args[1]);
                            }
                        }
                    }
                } else {
                    if(args[0].equalsIgnoreCase("feed")) {
                        if (p.hasPermission("exoticcube.regen.feed")) {
                            p.setFoodLevel(20);
                            p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                    + ChatColor.GRAY + " You feeded " + ChatColor.GOLD + args[1]);
                        }
                    }
                }
            }
            return false;
        }
    }
    
    Also, how to make players cant do the command if a player is offline, and how to give permission to the player to give access <playername> ?
     
    Last edited: Dec 23, 2016
  9. Offline

    MrGeneralQ

    You can check if playerByname == null
     
  10. Offline

    LuckyLuckiest

    Could I please request for the correction of the code ?
    I really need it and fast :(
    I will make every command alone :(
    No one wants to help me, so I will ever command alone :(
    You guys dont want to help me :(

    Please help :(

    EDIT: I did two commands now, I hope they are correct :)
    /feed :
    Code:
    package com.exoticcube.essentials.Commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffectType;
    
    public class CommandFeed implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String label, String[] args) {
          
            Player p = (Player) sender;
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be a player to use this command!");
                return true;
              
            } else {
                if(cmd.getName().equalsIgnoreCase("feed")) {
                    if (!(p.hasPermission("exoticcube.feed"))) {
                        p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                + ChatColor.RED + " You don't have permission to do this command!");
                        return true;
                      
                    } else {
                        p.setFoodLevel(20);
                        PotionEffectType Hunger = null;
                        p.removePotionEffect(Hunger);
                        p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                + ChatColor.GRAY + " Your " + ChatColor.GOLD + "hunger bar " + ChatColor.GRAY + "is refilled!");
                    }
                }
            }
            return false;
        }
    }
    
    
    /heal :
    Code:
    package com.exoticcube.essentials.Commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandHeal implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String label, String[] args) {
        
            Player p = (Player) sender;
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be a player to use this command!");
                return true;
            
            } else {
                if(cmd.getName().equalsIgnoreCase("heal")) {
                    if (!(p.hasPermission("exoticcube.heal"))) {
                        p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                + ChatColor.RED + " You don't have permission to do this command!");
                        return true;
                    
                    } else {
                        p.setHealth(20.0);
                        p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.GOLD + "ExoticCube" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "]"
                                + ChatColor.GRAY + " Your " + ChatColor.GOLD + "health bar " + ChatColor.GRAY + "is refilled!");
                    }
                }
            }
            return false;
        }
    }
    
    I still didnt test them, but I hope they work :)
    Please correct me!
     
    Last edited: Dec 23, 2016
Thread Status:
Not open for further replies.

Share This Page