Help with ip blacklisting

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Jul 17, 2018.

  1. How do i make it so when a player tries to join and if the ip starts with numbers from config list it blocks it?
    This is what i have:
    Code (Text):
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            if (e.getPlayer().getAddress().toString().startsWith(null, getConfig().getStringList("blacklist").size())) {
                e.getPlayer().kickPlayer(ChatColor.RED + "Your IP is blacklisted!");
        }
    Code (Text):
    #Blacklisted ips
    blacklist:
    - 79
     
  2. Erm, why are you calling .startsWith(null, configString.size()?
    Surely you want a for loop on your string list from your config and just call .startsWith(configString)
     
  3. i tried just with the configstring but it is underlined and it asks for there to be sometihng infront of it.
    also i just found out nullpointerexeptoion on console:
    Code (Text):
    17.07 11:16:15 [Server] ERROR Could not pass event PlayerJoinEvent to UPGamers v3.0
    17.07 11:16:15 [Server] INFO org.bukkit.event.EventException: null
    17.07 11:16:15 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerList.onPlayerJoin(PlayerList.java:346) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerList.a(PlayerList.java:166) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.LoginListener.b(LoginListener.java:159) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.LoginListener.e(LoginListener.java:57) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.NetworkManager.a(NetworkManager.java:233) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.ServerConnection.c(ServerConnection.java:140) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:845) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]
    17.07 11:16:15 [Server] INFO Caused by: java.lang.NullPointerException
    17.07 11:16:15 [Server] INFO at java.lang.String.startsWith(String.java:1405) ~[?:1.8.0_171]
    17.07 11:16:15 [Server] INFO at me.thesilentpro.net.Main.onPlayerJoin(Main.java:156) ~[?:?]
    17.07 11:16:15 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
    17.07 11:16:15 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
    17.07 11:16:15 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
    17.07 11:16:15 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
    17.07 11:16:15 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-d0a3620]
    17.07 11:16:15 [Server] INFO ... 14 more
     
  4. Are you sure?... Have you taken a look at this link?
    https://beginnersbook.com/2013/12/java-string-startswith-method-example/
     
  5. Isn't that exactly what you're trying to do? Get a boolean based on the start of a string?
     
  6. I really dont see how this helps...
     
  7. Well... You're turning the IP into a string... And seeing if it starts with 79 right? Isn't that exactly what that link explains with different strings?
     
  8. But how do use the getconfig.getstringlist with starts with? Link just shows normal strings: " "
     
  9. Well, it's a list. Either loop through the list or get a specific string value. It literally returns a List<String>