How should I tempban

Discussion in 'Spigot Plugin Development' started by Kyllian, Apr 23, 2017.

  1. Hey, I did some googeling, But I couldn't find the best way for a code to tempban someone. I tought I should store the date and time the player will be unbanned on? I dont know if that would work
  2. Store the currentTimeMillis. When user joins, check if the currentTime is greater then the storedTime + The Seconds that the user got banned.
    • Informative Informative x 1
  3. But, What if the server goes on and off. And wouldnt that reset every day?
  4. You have to store the tempBanned players. Load it on startup. Save on disable.
  5. Careful. If you store the time in milliseconds, you must add milliseconds to it, not seconds.
    • Agree Agree x 1
  6. Yep, But wouldnt it reset every day, because every day only has 86400000 miliseconds?
  7. Or you could do simple math, (seconds * 1000). Saves you some math :p
    • Useful Useful x 1
  8. Not sure. You could also store the date + time (Example: 24-04-2017 16:50) and when user joins, check the date and time.
  9. The System.currentTimeMillis() returns the amount of miliseconds since the Epoch. That is 1970, Januar 1st. To store a tempban I would recommand the following code:
    Code (Text):
    long timeBan = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10);
    //store your data
    //check when trying  to login
    long time = //get data
    if(System.currrentTimeMillist()-time > 0){
    //ban over
    // remove ban data
    You can change the TimeUnit to minutes, hours, days and whatver you like
    • Agree Agree x 1
  10. You have to use "System.currentTimeMillis();". This will give you the difference in milli seconds between current time and 1st Jan. 1970
    With this time you can calculate everything with Tempban.
  11. Store the players info in a MySQL database with a SimpleDateFormat and when they log on, check to see if the current time is teh same time or later then when there were supposed to be unbanned and if so, unban them.