Kick a player after a recent time

Discussion in 'Spigot Plugin Development' started by techniktuts, May 23, 2015.

  1. Hello I am making a plugin righz now, which I am coding for me and my friends .

    Can someone tell me how to ban a player after recent time and unban him when the next reallife day started?
     
  2. Can you try and rephrase this, your aim is hard to understand.
     
  3. I am pretty sorry I had to change my Samsung Input Language to English again first ^_^ So basically when a player joins on the server he has x minutes to play. After this x minutes he will get banned and will be unbanned when the next reallife day starts. And I don't know how to do that ( ._.)
     
  4. PS: The usage is the followin: Me and my friends make a Pvp project but you can just farm the X minutes until you will get kicked so nobody can farm 10 hours when the others just played like 2 hours or something like that.
     
  5. You can either do this with schedulers but that can get laggy, also may not adjust correctly on reset. For unbanning I would do time stamps (System.currentTimeMillis() I think); on ban save it to a list/map or to a file if there will be a reset and then check when player logs if the current time is 24hrs greater than the ban time. That is less server intensive than having a scheduler running to unban person X
     
    • Like Like x 1
  6. Ok thank you for your help so far. But the player shouldn't get unbanned after 24 hours, but rather when the next day started. Is there any method to get the reallife time from the server? Then I could just check if the next day started.
     
  7. Well i'm just going to point out one flaw.

    When is a real day? There are different timezones.
    Day in america is night in the Asian continent.
     
    • Agree Agree x 1
  8. Europe UTC +1 so the Timezone of Berlin
     
  9. Code (Text):
    new BukkitRunnable(){
        private int oldDay = getDay();
        @Override
        public void run(){
            int day = getDay();
            if(day != oldDay){
                oldDay = day;
                //TODO
            }
        }
        private int getDay(){
             return new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
        }
    }.runTaskTimer(yourPlugin, 0, 20L * 60)
    This code check every minute if the IRL day has changed ;)
    EDIT : This is working with your server time
     
    #9 DevHill, May 23, 2015
    Last edited: May 23, 2015
    • Like Like x 1
  10. Thanks a lot I will try it when I am programming again!
     
  11. When you ban a player, you could save their UUID to a file along with the day, month, and year. Then when they try to log in again you just need to check if:

    (current day > their ban day) || (current month > their ban month) || (current year > their ban year)

    If you're using Java8, you can use the new time api:
    Code (Text):
    import java.time.LocalDateTime;

    LocalDateTime.now().getMonthValue()
    LocalDateTime.now().getYear()
    LocalDateTime.now().getDayOfMonth()
    If none of the values are greater than what was set when the player was banned, don't allow the player to log in.
     
    • Like Like x 1
  12. Thanks a lot this is also very helpful, because I am using Java 8 ^_^
     
  13. Or you can use the built in spigot Banlist#addBan() method
     
    • Like Like x 1
  14. Can I save the bans in a file with the ban ()-method?
     
    • Like Like x 1
    • Agree Agree x 1
  15. That's really cool! This will be very useful help!
     
  16. I was curious about how to get the next midnight so I tested the Date class a little bit. In order to get the next midnight, you can use:
    Code (Text):
    import java.util.Date;

    Date now = new Date();
    Date unbanDate = new Date(now.getYear(), now.getMonth(), now.getDate()+1, 0, 0, 0);
    Basically, it's the current year, the current month, current day + 1, hour 0, minute 0, and second 0.

    When creating the unbanDate with the day+1, it will automatically fix the month and year to be correct. By that, I mean if a player gets banned on December 31, 2015, the unban date will automatically fix itself to be January 1, 2016.
     
    • Like Like x 1
  17. Code (Text):
    Date now = new Date();
                        now.getTime();
                        int hours = now.getHours();
                        int minutes = now.getMinutes();
                        int seconds = now.getSeconds();
                        int hoursleft = 23-hours;
                        int minutesleft = 59-minutes;
                        int secondsleft = 60-seconds;
                        int secondsLeftForBan = (hoursleft*60*60) + (minutesleft*60) + (secondsleft);
    An easy and effective way for tempbanning a player for x-seconds... (/tempban <player> secondsLeftForBan). Worked for me :D
     
    • Like Like x 1
  18. 24 hours :p
     
  19. Is there a plugin online that I can download?