Jail Plugin

Discussion in 'Plugin Development' started by mehboss, Dec 28, 2016.

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

    mehboss

    Alright so I already got a format in mind for my jail plugin. I just don't have one for a timer. If I wanted to jail someone for 1 hour and then have it auto unjail them to my specified location (spawn) how would I check to see the amount of time they wanted to jail the person for and then do it.

    For example:
    /jail bob 1h - how would I check for the 2nd argument, change it into time format and then setup the time task, any ideas?
     
  2. Offline

    mcdorli

    If your format is like the following:

    Code:
    1h2m3s
    
    Then there are multiple solutions.

    First, to get the hour you can spit at "h", and then the first argument will be the hour, for minutes you split at "h" and "m" and the middle one will be the minutes, and lastly but not least, for seconds you remove "s" and split at "m", and the second string will contain the seconds.

    Make sure you check if the string even contains the value you're looking for before.

    The second solution is to use a RegEx, something like

    Code:
    (\d+)h?(\d+)?m?(\d+)?s?
    
    Then use the match groups, the first will contain the hours, the second the minutes and the third the seconds.

    Third, and by my opinion the best solution is to simply change your format to something like

    12:60:60

    And just split at the colons.
     
  3. Offline

    oriamrm

    @mcdorli
    I don't think that's what he meant...

    You can use BukkitRunnable:
    Code:
    new BukkitRunnable(){
                public void run(){
                          //Do Stuff
                }      
    }.runTaskLater(Bukkit.getPluginManager().getPlugin("TheImmuneSystem"),  72000);
    However, There is a problem using BukkitRunnable; It count the time based on the server ticks, wich means that if you are dealing with long times (An Hour, for example), small lags in the server can slow down the ticks, thus releasing the player later then it should.

    P.S
    Lol, x2 Tennants!
    And sorry for my bad English...
     
  4. Offline

    mcdorli

    That also wouldn't work, because what if the server restarts? He shouldn't use a timer for this.

    Try using my method above, convert the time to milliseconds, add it to the current time, save it to the config, then in a timer, check if any of the current bans have expired, and if it is, remove it from the config and unban the player.

    This also isn't perfect, because for example if you set a ban to 1 hour long on 12 March at 1:59, then the time change will mess the system up.
     
  5. I haven't much experience with time so I'll help you out with the args
    args.
    To check for arguments you should do this:

    If this is the command /jail PLAYER TIME

    Getting player from first arg:
    Player target = Bukkit.getPlayerExact(args[0]);

    Getting second arg:

    I'm not sure if this works:
    Code:
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM");
    simpleDateFormat.format(args[1]);
     
  6. Offline

    oriamrm

    What about using BukkitRunnable and every 60s or so save the timer in a .ynl file?
     
  7. Offline

    Jakeeeee

    Why not use Bukkit#getBanList then add the player to it?
    ex: Bukkit.getBanList(BanList.Type.NAME or IP).addBan(target, reason, unbanDate, banner);

    or

    You can:
    - Turn the entered date into seconds then multiply it by 1000 (making it milliseconds)
    - Add that number to System#currentTimeMilliseconds (Giving the unban date in milliseconds)
    - Save that to a file or another database with the users name, ip, uuid, reason, and unban date in milliseconds
    - When the player joins check if System#currentTimeMilliseconds is greater then the unban date in milliseconds, if currentTimeMilliseconds is greater the player is "unbanned" and has severed their time.
     
    Last edited: Jan 1, 2017
  8. Offline

    mehboss

    @mcdorli @oriamrm @JohnyTheCarrot @Jakeeeee
    I would like it to get the time and release that player once they have done their time. If they leave I would like the timer to "stop", and resume once they join back on the server. Also, I would, indeed, like if it would still save the jailed times if the server restarts.

    (CONFIG WITH PLAYERS UUID)
    This config will have current people who are jailed.

    After restart or when that player leaves:
    If (players uuid) = (getConfigUUIDS) --> resume timer

    I just need a way to stop the "timer"
     
  9. Offline

    oriamrm

    Oh, I've got an idea:
    Always run a BukkitRunnable, and iterate thru a List<Player>. of the players in jail. Rmove them from the list when they disconnect, and add the back when the join the server.
    Also if you want to be able to turn off the server and still save proggress, save the timer every 1m or so, and also save a List<Player> of players in jail.
     
  10. Offline

    mehboss

    @oriamrm
    How would I get the bukkit runnable time left to save it every 1m?
     
  11. Offline

    Zombie_Striker

    @mehboss @oriamrm
    No need to create a timer to save. Just use the onDisable. When the server is shutting down, save the time left for all the players in the list.
     
  12. Offline

    mythbusterma

    @Zombie_Striker

    There is a good reason. Servers can crash (especially non vanilla ones), so having to most up to date values can be quite helpful. Saving every minute or two is a good idea.
     
    Zombie_Striker likes this.
  13. Offline

    mehboss

    What would I use for the time left to run a command once the time is up, and how would I make it so if they leave or if the server restarts it will "pause" or save it when it registers they have left or on the onDisable and resume once they join back?
     
  14. Offline

    mythbusterma

    @mehboss

    If you need to run a command when the time is up, you either need to schedule a task to run when it's done, and reschedule the ones you read out of the config when the server is started again, or just check every few seconds if the task needs to be done.
     
  15. Offline

    mehboss

    Ah I seen this before.. So like example:
    Code:
    if cheese burger not done
    (continue task)
    heeh
     
  16. Offline

    mythbusterma

    @mehboss

    Try:

    Code:
    repeat once every 10 seconds:
       if the time to release them is now or is in the past
           release them
           remove them from tracked players
    
     
  17. Offline

    mehboss

    @mythbusterma @Zombie_Striker
    Thanks I get that I just still don't know how to auto restart the timer where it was last left off before they left or before the server restarted.

    So on the onDisable I'll check if the task has finished and if it didn't then How will I get the time left that the person needs to do and then save it to resume once the server starts again?
     
  18. Offline

    mythbusterma

    @mehboss

    Just save all the remaining timers, and have a periodic task run at the the start of the server, releasing people when their time is up.
     
  19. Offline

    mehboss

    I don't know how to save all the remaining timers and how to resume them where they were last saved. Could you help me or give me some wikis?
     
  20. Offline

    mythbusterma

    @mehboss

    It's just like anything else in the config file. You store a player's UUID and their time of release. Then, when the server starts again, you just read those numbers in.
     
  21. Offline

    mehboss

    @Zombie_Striker @mythbusterma
    How would I make it so it splits /jail mehboss 1h and does:
    Code:
    if (split equals m) {
    time = get time before unit * 60 * 1000
    //gets in milliseconds
    }
    if (split equals h) {
    time = get time before unit * 60 * 60 * 1000
    //gets in milliseconds
    }
    After that, how would I setup the variables so I can do:
    Code:
    onCommand {
        if (command equals jail) {
            if (args.length !=2) {
                correct usage
            }
    else {
         if (player is null) {
            send message
            return false
    }
        if (player is sender) {
           send message
           return false
    }
        if (player isn't null) {
           adds to array
    
      =================DON'T KNOW HOW TO DO ==============
    if (split equals m) {
    time = get time before unit * 60 * 1000 (sets time variable to this)
    //if it equals m it will make the hours into milliseconds
    return true;
    }
    if (split equals h) {
    time = get time before unit * 60 * 60 * 1000
    //if it equals h it will make the hours into milliseconds
    return true;
    }
       run task later {
           run() {
         if (p.isOnline() {
              remove player from array
              send message
              teleport player
                    }
                else {
                   remove from array
                    }
               } time {
            }
        }
    }
    ====================================================
    
    For now I don't care if it pauses the timer when the player leaves.
     
    Last edited: Jan 22, 2017
  22. Online

    timtower Administrator Administrator Moderator

    @mehboss I am personally thinking regex.
    Then with "all numbers+1 letter"
    You can do that in 1 go or for each time unit on its own.
     
  23. Offline

    mehboss

    @timtower
    How would I split the unit and the number, get the unit so you know what to change it to and then change the number to milliseconds?
     
    Last edited: Jan 22, 2017
  24. Online

    timtower Administrator Administrator Moderator

    @mehboss Unit will be the last char of the string.
    Value the rest.
    From there it is math
     
  25. Offline

    mehboss

    @timtower
    I don't know how to split them though
     
  26. Online

    timtower Administrator Administrator Moderator

  27. Offline

    mehboss

    @timtower @Zombie_Striker
    I'm not going to use regEx as I don't even understand splitting.

    Would the following work:
    Code:
    onCommand {
    Player p = Bukkit.getPlayer(args[0]);
        if (command equals jail) {
            if (args.length !=2) {
                correct usage
            }
    else {
         if (p == null) {
            send message
            return false;
    }
        if (p != null) {
           if (p == sender) {
              send message
              return false;
    }
        if p isn't in array
           adds to array
     
    //player types /jail bob 1h
    String message = args[1];
    String split = message.split("");
    
    //1 = split[0]
    //h = split[1]
    
    String number = split[0];
    String unit = split[1];
    int timesec = number * 20;
    int timemin = timesec * 60;
    int time = null;
    
    if (unit != null) {
        if (unit.equals("s") {
        //if it equals s it will make the seconds into ticks
        int time = number * 20
       //the one "=" will set the variable "time" so it knows the player did units in seconds and then it will change the seconds into ticks
        return true;
      } else if (unit.equals("m")) {
        //if it equals m it will make the minutes into ticks
        int time = timesec * 60
       //the one "=" will set the variable "time" so it knows the player did units in minutes and then it will change the minutes into ticks
        return true;
      } else if (unit.equals("h")) {
        //if it equals h it will make the hours into ticks
        int time = timemin * 60
       //the one "=" will set the variable "time" so it knows the player did units in hours and then it will change the hour into ticks
        return true;
      }
       run task later {
           run() {
         if (p.isOnline() {
              remove player from array
              send message
              teleport player
                    } else {
                   remove from array
                    }
                  }
               } time {
            }
        }
    }
    
     
    Last edited: Jan 27, 2017
  28. Offline

    mehboss

  29. Offline

    mehboss

    bump ^
     
  30. Offline

    mehboss

    bump ^^
     
Thread Status:
Not open for further replies.

Share This Page