Plugin Help How to check if player name and IP match the playername and IP in config file

Discussion in 'Plugin Help/Development/Requests' started by Caedus, Dec 24, 2015.

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

    Caedus

    Hi,

    Basically i've set it so that when a user is banned, the username and IP are logged to a config file like so:

    playername: IP

    Now, i'm trying to get the plugin to check the ip of a new player at event PlayerJoinEvent, and match it vs the IPs stored in the config file (this is to check whether a user who has been banned is simply logging in on another account).

    I'm drawing a blank on how to do that, as you can see in the PlayerJointEvent section in my code:

    Code:
    package me.Caedus;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerKickEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class IPWarn extends JavaPlugin implements Listener{
       
        @Override
        public void onEnable(){
            getLogger().info("IPWarn Plugin initialized");
      
            
            final FileConfiguration config = this.getConfig();
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            config.options().copyDefaults(true);
           
            saveConfig();
            reloadConfig();
           
        }
       
       
        @EventHandler
        public void onBan(PlayerKickEvent e){
            if(e.getReason().contains("ban")){
               
               
               
                Player p = (Player) e.getPlayer();
               
                String address = p.getAddress().toString();
               
                if(address.charAt(0) == '/'){
                    String newAddress = address.substring(1);
                    p.sendMessage("Hi "+p.getDisplayName()+", your IP is: "+newAddress+" (substring1)");
                   
                   
                    getConfig().set(p.getName(), newAddress);
                   
                   
                    saveConfig();
                   
                }   
       
            }
           
           
        }
       
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Player p = (Player) e.getPlayer();
           
            String address = p.getAddress().toString();
           
            if(address.charAt(0) == '/'){
                String newAddress = address.substring(1);
               
                if(getConfig().get(p.getName(), newAddress) == p.getAddress() ){
                    p.sendMessage("ok");
                }    else {
                    Bukkit.broadcastMessage("nope");
                }
               
            }
       
           
        }
       
    
       
       
       
       
       
       
    
    }
    
     
  2. Offline

    MrTigreroux

    So, if i understand, you would check if a player who join the server has an account with his IP banned.
    Therefore, you can't use playername: IP but i recommend a list with all IPs banned.
    That's how i would do that:
    Code:
    @EventHandler
        public void onBan(PlayerKickEvent e) {
            if(e.getReason().contains("ban")) {
                Player p = (Player) e.getPlayer();
                String address = p.getAddress().toString().substring(1);
                p.sendMessage("Hi "+p.getDisplayName()+", your IP is: "+address+"."); // Useless because the player is kicked.
                List<String> BannedIPs;
                if(getConfig().getStringList("BannedIPs") != null) BannedIPs = getConfig().getStringList("BannedIPs");
                BannedIPs.add(address);
                getConfig().set("BannedIPs", BannedIPs);
                saveConfig();
            }
        }
      
    @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Player p = (Player) e.getPlayer();
            String address = p.getAddress().toString().substring(1);
            if(getConfig().getStringList("BannedIPs") != null && getConfig().getStringList("BannedIPs").contains(address)) p.sendMessage(ChatColor.RED+"Your IP has been banned on this server.");
            else Bukkit.broadcastMessage(ChatColor.GREEN+"You are clean.");
        }
     
  3. Offline

    pie_flavor

    @Caedus @MrTigreroux You would do this:
    Code:
    for (String s : getConfig().getKeys(false)) {
        if (getConfig().getString(s).equals(ipString)) {
            doWhatever();
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page