Rent a region

Discussion in 'Plugin Development' started by mgbeenieboy, Sep 13, 2014.

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

    mgbeenieboy

    I'm making a region market plugin for my server and want to add a rent function. You create a sign and one of the lines means the time you will own the region. But how do I do that... What would be the best way to?

    One possibility would be a Thread.sleep(time), but that's probably not the best way.
    My next idea was converting the time in milliseconds. Because this time PLUS the current epoch time/ unix timestamp would equal the time when the renting expires. So I'd check with while(true) when this time is reached, if you know what I mean. But is that performance-friendly? :/

    What do you have for ideas?
     
  2. Offline

    SmooshCakez

    If you want it to display the time, try messing around with the Calendar class, and you could use a BukkitRunnable to update the time.
     
  3. Offline

    4thegame3

    Store the data in a file and run a task that every minute checks
    Code:java
    1. if(yourdate.before(new Date())
    2. {
    3. //Do something
    4. }


    please search some tutorials that expains how to use SimpleDateFormat()
     
  4. Offline

    jusjus112

  5. Offline

    4thegame3

  6. Offline

    mgbeenieboy


    Ouh.. that's gonna be very hard for a beginner like me.

    I don't need the config for other stuff, so I thought I could use that file to store the date. Am I doing it right, because it's not working:

    Code:java
    1. else if(sign.getLine(0).equals("[Rent]")) {
    2.  
    3. getConfig().addDefault(e.getPlayer().getUniqueId() + ".rent", true);
    4. getConfig().addDefault(e.getPlayer().getUniqueId() + ".start", new Date());
    5. getConfig().addDefault(e.getPlayer().getUniqueId() + ".end", "test");
    6. reloadConfig();
    7.  
    8. }
     
  7. Offline

    4thegame3

    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    getConfig().set(UUID + ".start", format.format(new Date()));

    Date() witout values means NOW
     
  8. Offline

    jusjus112

  9. Offline

    mgbeenieboy


    The bukkit servers were lagging to that moment. That could be the reason why the thread was posted twice. However, sorry that was not my intention.

    I never needed dates in java and just watched some videos. It was hard to understand for me.

    This is what I've done:
    Code:
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
           
    System.out.println(sdf.format(new Date()));
    System.out.println(sdf.format(new Date(System.currentTimeMillis() + 1000000000)));
    As you can see, I had to add the time in milliseconds to the current time. How can I add it in the format 1d, 1h or 1m, so that I could directly take what's on line X on the sign?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  10. You would have to create a parser that converts you text to miliseconds. If you will be using 1 time format per sign you can create a switch which contains cases for time formats and then separate the number and multiply it with a corresponding value.

    For expiration you can also schedule single BukkitRunnable's when the plugin is enabled or a new sign is created (the delay would be the time that is left). I am not sure which solution is better.
     
  11. Offline

    mgbeenieboy


    The problem with BukkitRunnables is that the time wouldn't count when the server is offline. Well, I'll try something with switch.

    I don't find a way to do it with the switch statement :/

    But how about that? Just stupid that it won't be possible to have a time format like "1d 5h", but "29h".

    Code:java
    1. long rentTime;
    2. String rts = sign.getLine(2);
    3.  
    4. if(rts.endsWith("m")) {
    5. Sys
    6. rts.replaceAll("m", "");
    7. rentTime = Long.parseLong(rts);
    8. rentTime *= 60000;
    9. }
    10.  
    11. else if(rts.endsWith("h")) {
    12. rts.replaceAll("h", "");
    13. rentTime = Long.parseLong(rts);
    14. rentTime *= 3600000;
    15. }
    16.  
    17. else if(rts.endsWith("d")) {
    18. rts.replaceAll("d", "");
    19. rentTime = Long.parseLong(rts);
    20. rentTime *= 86400000;
    21. }
    22.  
    23. else if(rts.endsWith("w")) {
    24. rts.replaceAll("w", "");
    25. rentTime = Long.parseLong(rts);
    26. rentTime *= 604800000;
    27. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  12. Nice, if you want multiple values that is possible as well. Create a new method with the quoted code that returns a long (rentTime) and takes a String. For this example let's call that method removeFormat().
    If you split your original string by the " " character it will return an array of Strings. This array can be used in a for loop to convert each time format one by one with the removeFormat() method and add them up to get the total amount of time.
     
  13. Offline

    4thegame3

    if the server doesnt count the time when offline then calculate the timeout date.
    Date incrementedDate = DateUtils.addSeconds(date, seconds);

    let me know if that helped

    EDIT you can also add milliseconds, (addMilliseconds)
     
  14. Offline

    mgbeenieboy

    Code:java
    1. else if(sign.getLine(0).equals("[Rent]")) {
    2.  
    3. long rentTime = 0;
    4. String rts = sign.getLine(2);
    5. String s = new StringBuilder(rts).deleteCharAt(rts.length() - 1).toString();
    6. long amount = Long.parseLong(s);
    7.  
    8. if(rts.endsWith("m")) {
    9. rentTime = (rentTime + 1) * 60000 * amount;
    10. }
    11.  
    12. else if(rts.endsWith("h")) {
    13. rentTime = (rentTime + 1) * 3600000 * amount;
    14. }
    15.  
    16. else if(rts.endsWith("d")) {
    17. rentTime = (rentTime + 1) * 86400000 * amount;
    18. }
    19.  
    20. else if(rts.endsWith("w")) {
    21. rentTime = (rentTime + 1) * 604800000 * amount;
    22. }
    23.  
    24. else {
    25. rentTime = -1;
    26. e.getPlayer().sendMessage("§cDu kannst diese Region nicht mieten, da die angegebene Mietdauer ungültig ist.");
    27. }
    28.  
    29. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    30.  
    31. getConfig().set(e.getPlayer().getUniqueId() + ".rent", true);
    32. getConfig().set(e.getPlayer().getUniqueId() + ".start", sdf.format(new Date()));
    33. getConfig().set(e.getPlayer().getUniqueId() + ".end", sdf.format(new Date(System.currentTimeMillis() + rentTime)));
    34.  
    35. saveConfig();
    36. reloadConfig();
    37. }
    38. }
    39.  
    40. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    41.  
    42. @Override
    43. public void run() {
    44.  
    45. //
    46.  
    47. }
    48. }, 0, 1200L);


    The first part is done!
    This is how the config.yml looks like after I right clicked a sign with "2m" in the third line:

    Code:
    3e75a7a1-6776-40bc-beef-c699810144c0:
      rent: true
      start: 14/09/2014 11:54
      end: 14/09/2014 11:56
    With a Bukkit Scheduler I want to check now every minute if the current time is after getConfig("UUID.end"). And I cannot say "UUID.end", so how to check that? I've no idea.
     
  15. You should have an array of UUID's upon checking the expiry. Once you have that you can use a for loop and use the String object containing the uuid every time.

    Edit: Look into getConfig().getKeys();
     
  16. Offline

    4thegame3

    getConfig.getString(UUID+".end");
     
  17. Offline

    mgbeenieboy


    Thank you. That is what I've done:

    Code:java
    1. ArrayList<UUID> uuids = new ArrayList<UUID>();
    2.  
    3. @Override
    4. public void onEnable() {
    5. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    6.  
    7. @Override
    8. public void run() {
    9.  
    10. for(UUID uuid : uuids) {
    11. if(getConfig().getK // I DON'T KNOW HOW TO CONTINUE.
    12. }
    13.  
    14. }
    15. }, 0, 1200L);
    16. }
    17.  
    18. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    19.  
    20. uuids.add(e.getPlayer().getUniqueId());
    21.  
    22. getConfig().set(e.getPlayer().getUniqueId() + ".rent", true);
    23. getConfig().set(e.getPlayer().getUniqueId() + ".start", sdf.format(new Date()));
    24. getConfig().set(e.getPlayer().getUniqueId() + ".end", sdf.format(new Date(System.currentTimeMillis() + rentTime)));


    Of course this is not how my class really looks like. I skipped unimportant parts to keep it clear.

    I created this arraylist and everytime a player rents a region, his UUID becomes added into this list.
    On enable, a scheduler runs that should check every minute whether an-uuid-of-the-stringlist-uuids.end is < the-current-time. I read the bukkit documentation and I still do not know how Config Keys work. I hope it's clear where the problem is.
     
  18. Offline

    4thegame3

    mgbeenieboy i dont understand what do you need now, the run() method that checks?

    EDIT:
    Code:java
    1. for(String UUID:getConfig().getKeys(false)){
    2. //check
    3. }
     
  19. Here you can find out

    Edit: I haven't seen the 'uuids' object before. Then you don't need to read the config, just go ahead and use that.
     
  20. Offline

    mgbeenieboy

    if(getConfig().getString(uuid + ".end") > sdf.format(new Date())))

    That should work. I just cannot compare dates in the format dd/MM/yyyy HH:mm with each other, but I'll find a way..
     
  21. Offline

    4thegame3

    Code:text
    1. if(date1.before(new Date()){
    2. //do stuff
    3. }
     
  22. Offline

    mgbeenieboy

    Code:java
    1. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    2. Date expireDate = sdf.parse(getConfig().getString(uuid + ".end")); //Unhandled exception type ParseException
    3. if(expireDate.before(new Date())) {
     
  23. Offline

    4thegame3

    use try/catch block
     
  24. Offline

    mgbeenieboy

Thread Status:
Not open for further replies.

Share This Page