Everything is in the title, I want to get a String of the player's ip from its Player object. Thanks in advance !
PHP: public void onPlayerLogin(PlayerLoginEvent e){Player p = e.getPlayer();String sIp1 = p.getAddress().getHostName();//orString sIp2 = p.getAddress().getHostString(); }
I don't see the getHostString method, but getHostName exists. Anyway, it writes in my server console "Could not pass event PLAYER_LOGIN to SBTLogin , java.lang.NullPointerException, at the line I've written this : String ipAddr = p.getAddress().getHostName();
Are you connecting to localhost when you test that? I've had issues with null IPs when I connect with localhost.
Same error when I connect to my internet ip or local ip. Here is the source of my playerListener class : PHP: package tboss.SBTLogin;import org.bukkit.entity.Player;import org.bukkit.event.player.PlayerListener;import org.bukkit.event.player.PlayerLoginEvent;public class SBTLoginPlayerListener extends PlayerListener { public static SBTLogin plugin; public SBTLoginPlayerListener(SBTLogin instance) { plugin = instance; } public void onPlayerLogin(PlayerLoginEvent event) { Player player = event.getPlayer(); String userparam = "user=".concat(player.getName()); String ip = plugin.getIp(player); String ipparam = "&ip=".concat(ip); String parameters = userparam.concat(ipparam); String loginURL = "http://www.my-server.com/game/checkserver.php"; String result = HttpPostSender.sendPostRequest(loginURL, parameters); // On envoie la requête et on récupère la réponse if(!result.equals("1")) event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, result); System.out.println("[SBTLogin] Params: ".concat(parameters)); }} Here is the getIp function from the plugin class : PHP: public String getIp(Player p) { String ipAddr = p.getAddress().getHostName(); return ipAddr; } And everything works fine and I get no error if I replace this line String ip = plugin.getIp(player); by this line String ip = "127.0.0.1"; Anyway I need to get this ip and not to use 127.0.0.1 anytime...
Any pseudo corresponds to an ip. I want to verify if the person witch tries to connect has the correct ip. It uses for this a php script located on the internet.
Try having someone else to log onto your server, it could just be an issue with you logging in from your local machine or on the same network.
Soo... this is the code: PHP: public void onPlayerJoin(PlayerJoinEvent e){ Player p = e.getPlayer(); InetSocketAddress IPAdressPlayer = p.getAddress(); String sfullip = IPAdressPlayer.toString(); String[] fullip; String[] ipandport; fullip = sfullip.split("/"); String sIpandPort = fullip[1]; ipandport = sIpandPort.split(":"); String sIp = ipandport[0]; p.sendMessage("You logged in with the ip: " + sIp); }
Why not getHostName() ? Anyway, it does a NullPointerException at this line : String sfullip = IPAdressPlayer.toString(); It seems not to be able to use getAddress() while the player is still logging in. Any other way to get the ip ? I don't use the function onPlayerJoin(PlayerJoinEvent e), I use onPlayerLogin(PlayerLoginEvent event) because I need not to accept the player to join if the php script returns something else than "1".
I asked someone else to connect, and I had the same issue. I need a solution, everything else is ready to launch the launcher and the plugin ! I already finished the launcher and the web pages !
Found a way here : http://forums.bukkit.org/threads/solved-another-bukkit-npe-onplayerlogin-strings.43329/ Thank you EvilSeph and Ribesg.