Page 1 of 1
Problem with longs (Java/Bukkit)
Posted: Tue Apr 17, 2012 4:34 pm
by offluffy
I've decided to implement temp banning. And because the Unix timestamp is easier to use for calculations, I'm using that, and apparently it's best to use the "long" datatype in Java or you'll get some sort of 2038 problem. But having said that, I'm having a problem, and I'm about completely lost on how to fix it. Firstly, here's the code:
if (plugin.bans.getBoolean("User." + niceName + ".tempban")){
long uT = System.currentTimeMillis() / 1000L;
long bT = plugin.bans.getLong("User." + niceName + ".tempban.bantime");
long dur = plugin.bans.getLong("User." + niceName + ".tempban.duration");
if ((uT - bT) > dur){
plugin.log.info("User: " + playerName.getName() + " >> Temp-ban duration has expired, unbanning.");
List<String> tbList = plugin.banned.getStringList("Users");
tbList.remove(niceName);
plugin.banned.set("Users", tbList);
try{
plugin.banned.save(plugin.bannedFile);
}catch (IOException e){
e.printStackTrace();
}
plugin.log.info(playerName.getName() + " logged in from " + ip);
event.setResult(Result.ALLOWED);
return;
}
else{
//float length = (dur - (uT - bT)) / 60;
event.setKickMessage("You are temporarily banned. Check back later.");
event.setResult(Result.KICK_OTHER);
plugin.log.info (playerName.getName() + " tried logging in while temp banned.");
return;
}
}
This code for some reason returns a memory section message for the KickMessage rather than what is specified. So maybe my math is just jacked up or I'm handling the long datatype improperly. Thought I'd ask @.@ Thanks in advance!
Re: Problem with longs (Java/Bukkit)
Posted: Tue Apr 17, 2012 6:16 pm
by jacek
It looks okay to me at first glance. Are you sure you are always dividing the time by 1000 ?
What is the message you are getting ?
Re: Problem with longs (Java/Bukkit)
Posted: Tue Apr 17, 2012 11:04 pm
by offluffy
Made a bit of a dumb mistake XD I don't have a tempban node in my bans file yet @.@ I stared at the code for yours trying to figure out what dafuq was wrong with it T.T But I'll give ya the message, fix that problem, and see if it helps.
Message on kick:
Disconnected by Server
MemorySection[path='User.someone', root='YamlConfiguration']
^^^^ The test user being kicked is "someone" btw.
Re: Problem with longs (Java/Bukkit)
Posted: Wed Apr 18, 2012 7:25 am
by offluffy
Well, I was right XD But it turns out there were a few spots where the node in the yml file just didn't exist or I had it nested under a node that didn't exist @.@ I really gotta stop working on this while I'm half asleep T.T Thanks anyways XD
Re: Problem with longs (Java/Bukkit)
Posted: Thu Apr 19, 2012 3:00 pm
by jacek
Oh right ! That looks a bit like a getConfigSection().toString() to me.
You should really make sure a default gets used for everything if the config file is broken, I have found that people love to delete stuff from the .yml files for some reason.
Re: Problem with longs (Java/Bukkit)
Posted: Fri Apr 20, 2012 8:10 am
by offluffy
oh, so just check if a certain node exists than set it to something if not? I'll try to work something like that in O.o I did have a problem with my plugin breaking if there are no users saved in users.yml, so I had to insert a placeholder user and IP address so it didn't spit out console errors every time someone joined. @.@
Re: Problem with longs (Java/Bukkit)
Posted: Sat Apr 21, 2012 1:34 am
by jacek
I think you can specify a default value
String something = config.getString("path.to.the.string", "a fallback value");
you could with the old config system anyway.
offluffy wrote:I did have a problem with my plugin breaking if there are no users saved in users.yml, so I had to insert a placeholder user and IP address so it didn't spit out console errors every time someone joined. @.@
What was the error ? You probably needed to catch the case where there were no users and stop checking.
Re: Problem with longs (Java/Bukkit)
Posted: Mon Apr 23, 2012 3:18 pm
by offluffy
Oh yes, I've used that a few times before. Here's the error I was getting:
2012-03-29 08:15:49 [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:226)
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:37)
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
It was popping out an error because the plugin was trying to fetch the Configuration Section from a node, and if that node was empty, it would error out and stop executing the code. And as a temp fix, I decided to add a temporary node under it so it wouldn't completely stop the plugin from working. Come to think of it, I don't exactly know how to fix that. Because I usually fetch the configuration section and check to see what's in it. And it can't if it's empty. Got a way around that? I'd rather not just add a fake node on every run XD
Re: Problem with longs (Java/Bukkit)
Posted: Tue Apr 24, 2012 9:10 pm
by jacek
The key bit is this
Caused by: java.lang.NullPointerException at
me.offluffy.blip.LoginListener.onPlayerLogin(LoginListener.java:37)
There is an unexpected null on line 37, so see what is on line 37 and think about what could be null. It will probably be the result of getConfigSection(). you can just check it against null to see
ConfigSection thingy = config.getConfigSection(playerName);
if (thingy == null){
// add a default and return.
}
// down here we know that thingy is not null.
Re: Problem with longs (Java/Bukkit)
Posted: Wed Apr 25, 2012 4:45 am
by offluffy
I'll give that a shot then XD Later when I'm awake enough to comprehend stuff. I know it was the Conf. Section that was null, just didn't think to check for ==null before, thanks @.@