Ban system

Discussion in 'Plugin Development' started by SirLittlemonster7, Apr 3, 2013.

Thread Status:
Not open for further replies.
  1. Hello, I am making a ban system and I get an internal error trying to kick the person.

    What is happening here?
    Full code:
    Code:
            if (cmd.equalsIgnoreCase("ban")) {
                if (args.length == 1) {
                    if (player.hasPermission("l.admin")) {
                        banned.add(args[0]);
                        Bukkit.broadcastMessage(ChatColor.GOLD + args[0] + ChatColor.GRAY + " Was just banned by: " + player.getName());
                        Bukkit.getPlayer(args[0]).kickPlayer("Banned. Appeal at www.littleforums.net");
     
                    }
                }
            }
            if (cmd.equalsIgnoreCase("unban")) {
                if (args.length == 1) {
                    if (player.hasPermission("l.admin")) {
                        banned.remove(args[0]);
                        Bukkit.broadcastMessage(ChatColor.GOLD + args[0] + ChatColor.GRAY + " Was just unbanned by: " + player.getName());
                    }
                }
            }
            if (cmd.equalsIgnoreCase("banlist")) {
                player.sendMessage(ChatColor.GOLD + "Total Ban Amount: " + banned.size());
            }
            if (cmd.equalsIgnoreCase("unbanall")) {
                if (player.getName().equalsIgnoreCase("littlemonster7") || player.getName().equalsIgnoreCase("trollzpvp")) {
                    banned.clear();
                    player.sendMessage(ChatColor.GRAY + "Unbanned ALL players.");
                }
            }
    Code thats getting error:
    Code:
            if (cmd.equalsIgnoreCase("ban")) {
                if (args.length == 1) {
                    if (player.hasPermission("l.admin")) {
                        banned.add(args[0]);
                        Bukkit.broadcastMessage(ChatColor.GOLD + args[0] + ChatColor.GRAY + " Was just banned by: " + player.getName());
                        Bukkit.getPlayer(args[0]).kickPlayer("Banned. Appeal at www.littleforums.net");
     
                    }
                }
            }
    How do I make sure that if they are online. it will add kick them.. but if they are not a player (like not online) it will just add them to the banned list?
     
  2. Offline

    WolfGangSen

    2 things first

    1 POST THE ERROR.
    2 POST THE WHOLE METHOD

    Why?

    1 because it tells us what kind of mistake to look for.

    2
    because i have no idea what half your code is.
    cmd.equalsIgnoreCase("ban")

    cmd is clearly a string (unless you created your own class)
    but where are you getting it from? is it possible its null.
    is player actually a Player class and how are you getting it.
    etc

    but anyway I'm going to take a guess

    this is all in the wrong order.
    banned.add(args[0]);
    Bukkit.broadcastMessage(ChatColor.GOLD + args[0] + ChatColor.GRAY + " Was just banned by: " + player.getName());
    Bukkit.getPlayer(args[0]).kickPlayer("Banned. Appeal at www.littleforums.net");

    firstly your adding something to a ban list without checking that it even makes sense.
    secondly your putting it in chat straight away.
    third your trying to kick something that can return null if the argument is bad.

    also I'm going to guess that "Bukkit" is a reference to the main plugin.

    you need to do something more like this

    Code:java
    1.  
    2. Player banTarget = Bukkit.getPlayer(args[0]);
    3. if(banTarget != null)
    4. {
    5. Bukkit.broadcastMessage(ChatColor.GOLD + banTarget.getName() + ChatColor.GRAY + " Was just banned by: " + player.getName());
    6. banned.add(banTarget.getName());
    7. banTarget.kickPlayer("Banned. Appeal at [url]http://www.littleforums.net']www.littleforums.net[/url]");
    8. }
    9.  
     


  3. WolfGangSen


    Implemented code. But now if its a player that is not found it returns a pretty bad error:

    Code:
    03.04 17:05:42 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    03.04 17:05:42 [Server] INFO at me.littlekits.net.main.onCommand(main.java:3358)
    03.04 17:05:42 [Server] INFO Caused by: java.lang.NullPointerException
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.ThreadServerApplication.run(SourceFile:573)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.MinecraftServer.run(MinecraftServer.java:409)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.MinecraftServer.q(MinecraftServer.java:476)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.DedicatedServer.r(DedicatedServer.java:225)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.MinecraftServer.r(MinecraftServer.java:580)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.DedicatedServerConnection.b(SourceFile:30)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.ServerConnection.b(SourceFile:35)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.PlayerConnection.d(PlayerConnection.java:113)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.NetworkManager.b(NetworkManager.java:292)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.Packet3Chat.handle(Packet3Chat.java:44)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.PlayerConnection.a(PlayerConnection.java:840)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.PlayerConnection.chat(PlayerConnection.java:885)
    03.04 17:05:42 [Server] INFO at net.minecraft.server.v1_5_R1.PlayerConnection.handleCommand(PlayerConnection.java:967)
    03.04 17:05:42 [Server] INFO at org.bukkit.craftbukkit.v1_5_R1.CraftServer.dispatchCommand(CraftServer.java:514)
    03.04 17:05:42 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    03.04 17:05:42 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    03.04 17:05:42 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'ban' in plugin pKits v1.0
    03.04 17:05:42 [Server] SEVERE null
    03.04 17:05:39 [Server] INFO [Mod]-David < To perm
    03.04 17:05:38 [Server] INFO [Mod]-David < Can I have /ban
    03.04 17:05:38 [Server] SEVERE Connection reset
    03.04 17:05:38 [Disconnect] User CP3Beast has disconnected, reason: disconnect.quitting
    03.04 17:05:38 [Server] INFO Loaded 6 package(s) into the cache.
    03.04 17:05:38 [Server] INFO Plugin is now ready to be used.
    03.04 17:05:38 [Server] INFO Authenticated with the specified Secret key.
    03.04 17:05:37 [Server] INFO PartyPvP: Reload complete.
    03.04 17:05:37 [Server] INFO Server permissions file permissions.yml is empty, ignoring it
    03.04 17:05:37 [Server] INFO Enabling EssentialsSpawn v2.9.6
    03.04 17:05:37 [Server] INFO Enabling EssentialsChat v2.9.6
    03.04 17:05:37 [Server] INFO Enabling Buycraft v5.3
    03.04 17:05:37 [Server] INFO Essentials: Using GroupManager based permissions.
    03.04 17:05:37 [Server] INFO Enabling Essentials v2.9.6
    03.04 17:05:37 [Server] INFO WEPIF: Using the Bukkit Permissions API.
    03.04 17:05:37 [Server] INFO Fake Permissions version 3.1.6 is enabled!
    03.04 17:05:37 [Server] INFO Enabling Permissions v3.1.6
    03.04 17:05:37 [Server] INFO 0 regions loaded for 'LittlesPvPServer_the_end'
    03.04 17:05:37 [Server] INFO 43 regions loaded for 'LittlesPvPServer'
    03.04 17:05:37 [Server] INFO Loaded configuration for world 'LittlesPvPServer_the_end'
    03.04 17:05:37 [Server] INFO (LittlesPvPServer_the_end) Fire spread is UNRESTRICTED.
    03.04 17:05:37 [Server] INFO (LittlesPvPServer_the_end) Lava fire is blocked.
    03.04 17:05:37 [Server] INFO (LittlesPvPServer_the_end) Lighters are PERMITTED.
    03.04 17:05:37 [Server] INFO (LittlesPvPServer_the_end) TNT ignition is PERMITTED.
    03.04 17:05:37 [Server] INFO Loaded configuration for world 'LittlesPvPServer'
    03.04 17:05:37 [Server] INFO (LittlesPvPServer) Fire spread is UNRESTRICTED.
    03.04 17:05:37 [Server] INFO (LittlesPvPServer) Lava fire is blocked.
    03.04 17:05:37 [Server] INFO (LittlesPvPServer) Lighters are PERMITTED.
    03.04 17:05:37 [Server] INFO (LittlesPvPServer) TNT ignition is PERMITTED.
    03.04 17:05:37 [Server] INFO Enabling WorldGuard v5.7
    03.04 17:05:37 [Server] INFO Enabling pKits v1.0
    03.04 17:05:37 [Server] INFO Announcer v1.3.3 is enabled!
    03.04 17:05:37 [Server] INFO Enabling Announcer v1.3.3
    03.04 17:05:37 [Server] INFO WEPIF: Using the Bukkit Permissions API.
    03.04 17:05:37 [Server] INFO Enabling WorldEdit v5.4.2
    03.04 17:05:37 [Server] INFO GroupManager version 2.0 (2.9.6) (Phoenix) is enabled!
    03.04 17:05:37 [Server] INFO Enabling GroupManager v2.0 (2.9.6) (Phoenix)
    03.04 17:05:37 [Server] INFO Enabling pEconomy v1.0
    03.04 17:05:37 [Server] INFO Loading EssentialsSpawn v2.9.6
    03.04 17:05:37 [Server] INFO Loading EssentialsChat v2.9.6
    03.04 17:05:37 [Server] INFO Loading Buycraft v5.3
    
    Code:
            if (cmd.equalsIgnoreCase("ban")) {
                if (args.length == 1) {
                    if (player.hasPermission("l.admin")) {
                        Player banTarget = Bukkit.getPlayer(args[0]);
                        if (banTarget != null) {
                            Bukkit.broadcastMessage(ChatColor.GOLD + banTarget.getName() + ChatColor.GRAY + " Was just banned by: " + player.getName());
                            banned.add(banTarget.getName());
                            banTarget.kickPlayer("Banned. Appeal at [url]http://www.littleforums.net']www.littleforums.net[/url]");
                        } else {
                            Bukkit.broadcastMessage(ChatColor.GOLD + banTarget.getName() + ChatColor.GRAY + " Was just banned by: " + player.getName());
                            banned.add(banTarget.getName());
                        }
                    }
                }
            }
    Oh sorry, it copied more than the error.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. If you want to check if a player is online you could add a event (onPlayerJoin maybe) and do getServer().getOnlinePlayers(); or first
    Get the banned list and then check if player.isbanned
     
  5. I am not useing the banlist. Im using a list I made. (its easier)
     
  6. Oh ok well then check your list like that
     
  7. Offline

    WolfGangSen

    see this bit here?

    could you tell me which line is 3358?

    also

    you should possibly think about breaking your plugin up into classes. Its nicer when you dont have to scroll through 1000's of lines.
     
Thread Status:
Not open for further replies.

Share This Page