Page 1 of 1
How can I use the getHostAddress() correctly?
Posted: Tue Mar 20, 2012 7:38 am
by offluffy
So I posted a similar question on a betterPHP java tut on youtube, but I don't think that'll be answered honestly. lmao. Any my question has changed slightly anyways. At first, I was trying to use:
// First logical guess.
event.getPlayer().getAddress().toString()
// Tried this as well.
event.getPlayer().getAddress().getHostName()
but then figured out it was supposedly this:
event.getPlayer().getAddress().getAddress().getHostAddress()
Although this still doesn't work for me. I'm using it in the onPlayerLogin event. I'm just trying to get a string like "24.160.200.100", just any valid IP in the expected IPv4 format. I don't care to have the port in it. I've also tried a prePlayerLogin event, and no better result. I'd appreciate any help! I'm rather new to Bukkit modding if that isn't too obvious.
-OffLuffy
Re: How can I use the getHostAddress() correctly?
Posted: Tue Mar 20, 2012 10:01 pm
by jacek
From the PlayerPreLoginEvent you can do
String ip = event.getAddress().getHostAddress();
which will give you what you are after.
Re: How can I use the getHostAddress() correctly?
Posted: Tue Mar 20, 2012 10:15 pm
by offluffy
Thanks! I'll give it a shot and tell ya how it works out.
Re: How can I use the getHostAddress() correctly?
Posted: Fri Mar 23, 2012 12:10 am
by offluffy
So I've been trying this, but I've having a problem where my PlayerPreLoginEvent doesn't want to function at all. I have it registered in the main class, and it's set up almost just like the PlayerLoginEvent short of the code executed in the listener. I don't know why, but I can't even get it to log a line of text after it. There are no errors in the console at the moment. Here's what I have for it:
package me.offluffy.blip;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPreLoginEvent;
public class PreLoginListener implements Listener {
private Blip plugin;
public PreLoginListener(Blip plugin){
this.plugin = plugin;
}
@EventHandler
public void onPrePlayerLogin(PlayerPreLoginEvent event){
// Buncha code I removed until it is deemed relevent
}
}
So that's what I have for that class, and to register it in the main class, I used this line just above the line to register the login listener:
this.getServer().getPluginManager().registerEvents(new PreLoginListener(this), this);
I'm probably doing something incredibly stupid here, but I'd appreciate the help. This is one of the last things that needs to be sorted out before the plugin is ready XD If all this looks right to you, I'll post what was in the PreLoginListener before I posted it up there to see if there's something wrong with that. I removed the priority and ignoredCancelled from the listener to see if that'd help, but I've tried it with and without either one.
Re: How can I use the getHostAddress() correctly?
Posted: Fri Mar 23, 2012 8:04 pm
by jacek
Is your server in online-mode ? The PlayerPreLoginEvent is fired when the server checks the session Id with minecraft.net which it does not do if the server does not have online-mode = true
Re: How can I use the getHostAddress() correctly?
Posted: Fri Mar 23, 2012 8:16 pm
by offluffy
Curses and blasphemy. 'Tis not. Although I suppose I can put it in online for the duration of my testing. I will want to plugin to work for server that don't use online mode though (such as the one I actually manage outside of my test server). Is it possible to use the PlayerLoginEvent instead? I've read somewhere you can use:
event.getAddress().getAddress().getHostAddress()
Re: How can I use the getHostAddress() correctly?
Posted: Fri Mar 23, 2012 8:21 pm
by jacek
The PlayerLoginEvent is more or less the same so you should be able to yeah.
Re: How can I use the getHostAddress() correctly?
Posted: Fri Mar 23, 2012 8:24 pm
by offluffy
Thanks XD I'll definitely be crediting you and/or this forum when I compile a final version of this plugin XD If I even release it. Really just wanted it for the server I manage. And I don't trust the other plugins that have a tendency to mistakenly ban people and you have a buncha people trying to appeal to the bans and what-not T.T
Re: How can I use the getHostAddress() correctly?
Posted: Fri Mar 23, 2012 11:43 pm
by offluffy
So, I tried using the PlayerLoginEvent, and for now I just had it set up to get the ip (getAddress().getAddress().getHostAddress()) and to get the player name, and then log the player and IP. That's it. However, when I do that, I get this mess:
2012-03-23 05:33:53 [SEVERE] Could not pass event PlayerLoginEvent to Blip
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:303)
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
at net.minecraft.server.ServerConfigurationManager.attemptLogin(ServerConfigurationManager.java:229)
at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:102)
at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:94)
at net.minecraft.server.Packet1Login.handle(SourceFile:68)
at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:48)
at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:61)
at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
Caused by: java.lang.NullPointerException
at me.offluffy.blip.LoginListener.onPlayerLogin(LoginListener.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301)
... 12 more
Since it says something it wrong with line 24, this is line 24 in LoginListener.java
String ip = playerName.getAddress().getAddress().getHostAddress();
I would venture to guess I'm doing this wrong XD Since I did figure out this line from a forum post that wasn't in english. Imma mess around with this line a bit, but if you have an idea as to what is wrong before I can fix it (if I fix it), please let me know XD
EDIT:
Scratch that I suppose. As it so happens, I have to make sure the Result is ALLOWED before I can get the IP (via getKickMessage()), like this:
if (event.getResult().equals(Result.ALLOWED)) {
ip = event.getKickMessage();
}
Re: How can I use the getHostAddress() correctly?
Posted: Sat Mar 24, 2012 6:37 pm
by jacek
Yep if they are not allowed to connect they won't be on the server so getPlayer() will return null