How to temporarily ban a player & Player.setBanned

Discussion in 'Plugin Development' started by jollie000l, Mar 4, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    jollie000l

    Hi

    Im trying to make a ban command, but player.setBanned(true) is deprecated. Is there an alternate or something? I do kick the player before banning them, but I don't want deprecation

    ALSO

    Is there a way to temporarily ban a player? Do you need to generate a config file that stores the banned player's date of ban? Can someone please help and explain?

    Thanks
     
    PbJBOSS likes this.
  2. Offline

    PbJBOSS

    Im trying to do the same thing and here was my idea. I have no idea how to get them to be banned. Maybe some sort of prevent access thing. But I did have an idea on how to let them back on.
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent evt){
    4. Player player = (evt.getPlayer();
    5. if(System.currentTimeMillis() - player.getLastPlayed() > TimeOfBan){
    6. player.setBanned(false);
    7. }
    8. }
    9.  

    It would in theory unban people who tried to join after their time was up they would be unbanned.
     
  3. Offline

    Minecrafter_NL

    Why is a deprecation a problem if it still works?
    you can make a HashMap of banned players and their ban time, and save & load it on a reload.
    then let the server schendule a code to decrease all ban times with 1 every minute.
    after changing the values, check if any value is 0, if it is, unban him.

    Another way is saving the player in a file with the date he will be unbanned.
    then make some system which can calculate when he should be unbanned
     
  4. Offline

    moose517

    I would do what Minecrafter_NL said. create a hash map and when people are banned add them to that hashMap along with when they should be unbanned. Then when the player tries to login to the server check if the time has passed and if so remove them from the map and let them on otherwise let them know they are still banned.
     
  5. Offline

    Barinade

    Map<String, Long> bans = new HashMap<String, Long>();
    bans.put("Barinade", System.currentTimeMillis() + banTime); //ban time 1000 = 1 second
    (Create a method to return "3h" value in seconds*1000 where h is hours, add d for days, etc.)


    Player join, if in the ban list, get the time, if it's less than the current time, remove from list, allow join

    If you're looking for a short kick (like minutes, not long-term) then use a list and runTaskLater

    Edit: Made the method.
    Code:
        public static long getTime(String time) {
            int amt = 0;
                try {
                    amt = Integer.parseInt(time.substring(0, time.length()-1));
                    switch (time.substring(time.length()-1)) {
                    case "s" : amt *= 1000; break; //milliseconds in a second
                    case "m" : amt *= 1000 * 60; break; //milliseconds in a minute
                    case "h" : amt *= (1000 * 60) * 60; break; //milliseconds in an hour
                    case "d" : amt *= ((1000*60)*60)*24; break; //milliseconds in a day
                    case "w" : amt *= (((1000*60)*60)*24)*7; break; //milliseconds in a week
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
         
            return amt;
        }
    Code:
            System.out.println("6 days in milliseconds: " + getTime("6d"));
            System.out.println("3 weeks in milliseconds: " + getTime("3w"));
            System.out.println("10 hours in milliseconds: " + getTime("10h"));
            System.out.println("7 minutes in milliseconds: " + getTime("7m"));
    
    Code:
    Console:
    6 days in milliseconds: 518400000
    3 weeks in milliseconds: 1814400000
    10 hours in milliseconds: 36000000
    7 minutes in milliseconds: 420000
    Edit2:
    Or I could blow your mind with a little recursion for multime time formats
    (Instead of 2d or 5h you can do "2d5h" which is two days and five hours)
    Code:
        public static long getMultiTime(String time) {
            if (time.equalsIgnoreCase(""));
            final char[] fms = {'s','m','h','d','w'}; //put this line in the main class, remove it from here
            List<Character> fm = new ArrayList<Character>(); //you can add this there too, if you add the loop in onEnable
            for (Character c : fms) { //this needs to be in onEnable if you add the list to the main class instead
                fm.add(c);
            }
            long t = 0;
                for (int i=0;i<time.length();i++) {
                    if (fm.contains(time.charAt(i))) {
                        try {
                            t = Integer.parseInt(time.substring(0, i));
                            switch (time.substring(i,i+1)) {
                            case "s" : t *= 1000; break; //milliseconds in a second
                            case "m" : t *= 1000 * 60; break; //milliseconds in a minute
                            case "h" : t *= (1000 * 60) * 60; break; //milliseconds in an hour
                            case "d" : t *= ((1000*60)*60)*24; break; //milliseconds in a day
                            case "w" : t *= (((1000*60)*60)*24)*7; break; //milliseconds in a week
                            }
                            t+=getMultiTime(time.substring(i+1));
                        } catch (Exception e) {
                            //invalid format
                        }
                    }
                }
            return t;
        }
    Code:
            System.out.println("2 days 7 hours " + getMultiTime("2d7h"));
            System.out.println((getTime("2d") + getTime("7h")));
            System.out.println("3 weeks 2 days 7 hours " + getMultiTime("3w2d7h"));
            System.out.println((getTime("3w") + getTime("2d") + getTime("7h"))); //previous code to get it
    Code:
    Console:
    2 days 7 hours 198000000
    198000000
    3 weeks 2 days 7 hours 2012400000
    2012400000
     
  6. Offline

    Garris0n

  7. Offline

    Minecrafter_NL

    Garris0n

    oh, i didn't knew that new way of banning :D.
    i recommend using that method too, and just ignore my post
     
  8. Offline

    Garris0n

    I'm sure a lot of people don't, it was added very recently :)
     
  9. Offline

    jollie000l

    Thanks for the response I will try to do that
     
Thread Status:
Not open for further replies.

Share This Page