BAN COMMAND

Discussion in 'Plugin Development' started by WATWtomanik3, Mar 25, 2015.

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

    WATWtomanik3

    Hello guys i made a simple ban command for WATW Network. When you want to ban a player that is not online server gets an error. How can i make that console is quiet but the player gets just an message for example "That player is not online"

    CODE :

    Code:
    // BAN COMMAND \\
            if (commandLabel.equalsIgnoreCase("ban")) {
                if (!(sender.hasPermission("watw.ban"))) {
                    sender.sendMessage(nopermission);
               
                    } else {
                   
                    if (args.length == 0) {
                        sender.sendMessage(noargs);
    
                    }
                   
                    if (args.length == 1) {
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        target.kickPlayer("§8You have been banned by " + "§4" + sender.getName());
                        target.setBanned(true);
                   
                    }
                   
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                   
                    if (target == null) {
                        sender.sendMessage(noplayer);
                       
                        }
                    }
                }
     
  2. Offline

    Funergy

    @WATWtomanik3 Use a try and catch
    put the ban stuff into the try and in the catch you put the message

    EDIT: Isn't really handy if you put the whole ban stuff into the try and catch. Because that wouldn't output an error if something else doesnt work. So if you use a try and catch make sure its not much that is in the "try"
    But an if statement will be your goal
     
    Last edited: Mar 25, 2015
  3. Offline

    Gamecube762

    If the player is not online, Bukkit#getPlayer() will return null. A simple if statement checking if target is null will work.

    if (target is null) then sendMessage("player is not online!");
     
  4. Offline

    WATWtomanik3

    1. if (target == null) {
    2. sender.sendMessage(noplayer);
    3. }
    4. }

    I already done that check my code, and it doesnt work.
     
  5. Offline

    Gamecube762

    For some reason I din't see that part, but you would want that null check within if-statement above it.
    Code:
                   if (target == null) {
                        sender.sendMessage(noplayer);
                     
                        }
                    }
    This should be within the if-statement on line 13 and remove the 2nd "Player target = ...." as it is not needed.
     
  6. Offline

    Funergy

    @Gamecube762 @WATWtomanik3 Thats why I said
    Code:
    Player p;
    try{
    p = Bukkit.getServer().getPlayer(args[0]);
    }catch(NullPointerException e){
    sender.sendMessage("");
    }
     
  7. Offline

    WATWtomanik3

    That sadly doesnt work too, can you change the code for me ? Because i dont get what you mean ?
     
  8. Offline

    Gamecube762

    @Funergy An if-statement is cleaner and simpler than a try/catch for multiple reasons.
    Code:
    Player p = Bukkit.getPlayer("Gamecube762");
    if (p == null) sender.sendMessage("Gamecube762 is not online!!");
     
  9. Offline

    WATWtomanik3

    @Funergy

    Tried your way out doesnt work sadly.

    Code:
            // UNBAN COMMAND \\
            if (commandLabel.equalsIgnoreCase("unban")) {
                if (!(sender.hasPermission("watw.unban"))) {
                    sender.sendMessage(nopermission);
                   
                } else {
                   
                    Player p = (Player) sender;
                    OfflinePlayer target = p.getServer().getOfflinePlayer(args[0]);
                    if (args.length == 0) {
                        sender.sendMessage(noargs);
                       
                       
                    }
                   
                    if (args.length == 1) {
                        target.setBanned(false);
                        p.sendMessage(target + "is now unbanned.");
                       
                       
                    }
                   
                    try{
                        p = Bukkit.getServer().getPlayer(args[0]);
                        }catch(NullPointerException e){
                        sender.sendMessage(ChatColor.RED + "That player cannot be found.");
                        }
                       
                    }
                }
     
  10. Offline

    caderape

    @WATWtomanik3 so, for your ban command from the greatest server ever, WATW network, you cannot kick an offline player. Weird no ? You also have to check if the bukkit.getplayer(args[0]) != null before to do anything with it.
     
  11. Offline

    WATWtomanik3

    @caderape thanks for being soo friendly and sorry for bothering you.
     
  12. Offline

    SuperOriginal

    @Funergy Assigning a null value to a variable won't throw a NPE, however, trying to call a method with it will.
     
Thread Status:
Not open for further replies.

Share This Page