Errors Again

Discussion in 'Plugin Development' started by GodzillaFlame42, Jul 13, 2016.

Thread Status:
Not open for further replies.
  1. Hey, i have this problem now where there are red lines in
    String name = args[0];
    if(Bukkit.getServer().getPlayer(
    name) != null) {
    Player target = Bukkit.getServer().getPlayer(name);
    and i dont know how to fix them. Please help me

    Here is my code to it
    Code:
    package Inventory;
    
    import me.godzilla.Main.Errors;
    
    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 Clear implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            String command = cmd.getName();
           
            if(command.equalsIgnoreCase("clear") || command.equalsIgnoreCase("ci") || command.equalsIgnoreCase("clearinventory")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                   
                    if(args.length == 0) {
                        player.sendMessage(ChatColor.AQUA + "Your inventory has been cleared!");
                        player.getInventory().clear();
                    } else if(args.length == 1) {
                        if(player.hasPermission("iz.clear") || player.hasPermission("iz.ci") || player.hasPermission("iz.clearinventory"))
                        String name = args[0];
                        if(Bukkit.getServer().getPlayer(name) != null) {
                            Player target = Bukkit.getServer().getPlayer(name);
                           
                            player.sendMessage(ChatColor.GRAY + target.getName() + ChatColor.GRAY + "'s " + ChatColor.AQUA + "has been cleared!");
                            player.sendMessage(ChatColor.AQUA + "Your inventory has been cleared!");
                           
                            target.getInventory().clear();
                        } else {
                            player.sendMessage(ChatColor.AQUA + Errors.getErrorMessage(Errors.PLAYER_DOES_NOT_EXIST));
                        }
                    } else {
                        player.sendMessage(ChatColor.AQUA + Errors.getErrorMessage(Errors.NO_PERMISSIONS));
                    }
                }
            }
    
            return false;
        }
    
    }
     
  2. Offline

    tylersyme

    The if statement directly above the String name variable needs proper brackets. You should really never use an if statement without brackets even if it can work.
     
  3. Offline

    Lordloss

    @tylersyme I have to disagree with that. In this case with many if/else foldings it is totally true, then it will just confuse people, and make it less readable.

    But there are cases with one-liners of code where it is pretty useful, and improves readability. In example i like to use them in the top of EventListeners as quick escaping.

    Code:
    blockPlace (BlockPlaceEvent e) {
    
    if (e.getBlock().getType() != Material.GLASS) return;
    
    //stuff
    }
    But thats just my opinion.
     
    N00BHUN73R likes this.
Thread Status:
Not open for further replies.

Share This Page