Solved Cooldown, Change seconds to minutes

Discussion in 'Plugin Development' started by BeastCraft3, Dec 8, 2014.

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

    BeastCraft3

    Hello, I just learned how to use a cooldown class, the only issue with it now is that I don't know how to change the seconds to minutes.
    Now when they try to use a kit while their still in the cooldown it says: you have to wait 2345 seconds.
    It would be better if it stood "x minute(s) and x second(s)"
    this is one of my kits:
    Code:java
    1. if(args[0].equalsIgnoreCase("warrior")) {
    2. int ca = 1800;
    3. String Warrior = "Warrior";
    4. if(p.hasPermission("Kitz.warrior")) {
    5. if(map.containsKey(p.getUniqueId())) {
    6. p.sendMessage(kit);
    7. } else {
    8. if(Cooldown.isInCooldown(p.getUniqueId(), Warrior)){
    9. int timeLeft = Cooldown.getTimeLeft(p.getUniqueId(), Warrior);
    10. p.sendMessage(ChatColor.RED + "Time remaining on kit Warrior is "+ timeLeft +" seconds");
    11. } else {
    12. p.sendMessage(ChatColor.DARK_AQUA + "Recived kit Warrior!");
    13. p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
    14. p.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE, 1));
    15. p.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
    16. p.getInventory().addItem(new ItemStack(Material.IRON_HOE, 1));
    17. p.getInventory().addItem(new ItemStack(Material.COOKED_BEEF, 320));
    18. p.getInventory().addItem(new ItemStack(Material.FLINT, 320));
    19. p.getInventory().addItem(new ItemStack(Material.SEEDS, 128));
    20. p.getInventory().addItem(new ItemStack(Material.CLAY_BALL, 128));
    21. p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    22. pi.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    23. pi.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    24. pi.setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    25. Cooldown c = new Cooldown(p.getUniqueId(), Warrior, ca);
    26. c.start();
    27. map.put(p.getUniqueId(), name);
    28. }
    29. }
    30. } else {
    31. p.sendMessage("Only Warriors can use this kit!");
    32. p.sendMessage("§bPurchase Warrior at: ");
    33. }
    34. }


    this is my cooldown class:
    Code:java
    1. package com.BeastCraft3.SoccerKitZ;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. import java.util.UUID;
    6. public class Cooldown {
    7.  
    8. private static Map<String, Cooldown> cooldowns = new HashMap<String, Cooldown>();
    9. private long start;
    10. private final int timeInSeconds;
    11. private final UUID id;
    12. private final String cooldownName;
    13.  
    14. public Cooldown(UUID id, String cooldownName, int timeInSeconds){
    15. this.id = id;
    16. this.cooldownName = cooldownName;
    17. this.timeInSeconds = timeInSeconds;
    18. }
    19.  
    20. public static boolean isInCooldown(UUID id, String cooldownName){
    21. if(getTimeLeft(id, cooldownName)>=1){
    22. return true;
    23. } else {
    24. stop(id, cooldownName);
    25. return false;
    26. }
    27. }
    28.  
    29. private static void stop(UUID id, String cooldownName){
    30. Cooldown.cooldowns.remove(id+cooldownName);
    31. }
    32.  
    33. private static Cooldown getCooldown(UUID id, String cooldownName){
    34. return cooldowns.get(id.toString()+cooldownName);
    35. }
    36.  
    37. public static int getTimeLeft(UUID id, String cooldownName){
    38. Cooldown cooldown = getCooldown(id, cooldownName);
    39. int f = -1;
    40. if(cooldown!=null){
    41. long now = System.currentTimeMillis();
    42. long cooldownTime = cooldown.start;
    43. int totalTime = cooldown.timeInSeconds;
    44. int r = (int) (now - cooldownTime) / 1000;
    45. f = (r - totalTime) * (-1);
    46. }
    47. return f;
    48. }
    49.  
    50. public void start(){
    51. this.start = System.currentTimeMillis();
    52. cooldowns.put(this.id.toString()+this.cooldownName, this);
    53. }
    54.  
    55.  
    56. }


    Btw, if you wonder why I check if their in a hashmap before the cooldown stuff is simply because their only able to use one kit each time they die :p

    I hope you can help me with this,
    Regards,
    BeastCraft3
     
  2. Offline

    Rocoty

    Think math. How many seconds in a minute? 60 right? So how do you calculate how many minutes 2345 seconds is? Should be a simpe division. As for the seconds, what you want is the remainder of that division. The modulo (%) operator should help you there.
     
    BeastCraft3 likes this.
  3. Offline

    BeastCraft3

    Rocoty
    I understand that xD, just I don't know how to write it ;P
     
  4. Offline

    fireblast709

    BeastCraft3 likes this.
  5. Offline

    BeastCraft3

    fireblast709
    so something like
    int timeLeft / 60 = minutes
    ?? I just don't get it..
     
  6. Offline

    zackpollard

    That would be how time works, yes.
    Seconds would be the same, but with the modulus (%) sign.

    This was literally a 5 second google away rather than asking here.

    - Zack
     
  7. BeastCraft3 You'd still follow basic Java rules though :) If you don't know basic Java (which this is) I'd recommend learning it from either the Oracle tutorials or a Java book before making plugins, otherwise you'll run into problems you can't solve a lot of the time.
     
    BeastCraft3 likes this.
  8. Offline

    Rocoty

    AdamQpzm Oh come on, Adam. Be reasonable. This isn't even basic Java. It's basic math.
     
    BeastCraft3, teej107 and aaomidi like this.
  9. Offline

    aaomidi

    Please don't recommend Java tutorials for people who don't understand basic math and algorithm writing.
     
    BeastCraft3, teej107 and Rocoty like this.
  10. Offline

    JordyPwner

    YOU JUST GOT PWNED BY aaomidi and Rocoty OEEEH!! :p


    ONTOPIC: BeastCraft3 calculate the seconds to minutes "use google" <== just a suggestion :p
     
    BeastCraft3 likes this.
  11. Offline

    BeastCraft3

    JordyPwner aaomidi Rocoty zackpollard fireblast709
    Thanks Everyone :p I think I got it.. And AdamQpzm Belive it or not, but I've read two different java tutorials/ oracle and tutorialspoint :p
    I just started reading Java for dummies xD

    1) Sorry for double posting :p
    2) just going to paste what I did for other people that may wonder how to do this ;)
    Code:java
    1. if(args[0].equalsIgnoreCase("warrior")) {
    2. int ca = 1800;
    3. String Warrior = "Warrior";
    4. if(p.hasPermission("Kitz.warrior")) {
    5. if(map.containsKey(p.getUniqueId())) {
    6. p.sendMessage(kit);
    7. } else {
    8. if(Cooldown.isInCooldown(p.getUniqueId(), Warrior)){
    9. int timeLeft = Cooldown.getTimeLeft(p.getUniqueId(), Warrior);
    10. int minutes = timeLeft / 60;
    11. int seconds = timeLeft % 60;
    12. p.sendMessage("time reminding on kit Warrior: " + minutes + " minute(s) and " + seconds + " second(s)");
    13. } else {
    14. p.sendMessage(ChatColor.DARK_AQUA + "Recived kit Warrior!");
    15. p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
    16. p.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE, 1));
    17. p.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
    18. p.getInventory().addItem(new ItemStack(Material.IRON_HOE, 1));
    19. p.getInventory().addItem(new ItemStack(Material.COOKED_BEEF, 320));
    20. p.getInventory().addItem(new ItemStack(Material.FLINT, 320));
    21. p.getInventory().addItem(new ItemStack(Material.SEEDS, 128));
    22. p.getInventory().addItem(new ItemStack(Material.CLAY_BALL, 128));
    23. p.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    24. pi.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    25. pi.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    26. pi.setHelmet(new ItemStack(Material.DIAMOND_HELMET));
    27. Cooldown c = new Cooldown(p.getUniqueId(), Warrior, ca);
    28. c.start();
    29. map.put(p.getUniqueId(), name);
    30. }
    31. }
    32. } else {
    33. p.sendMessage("Only Warriors can use this kit!");
    34. p.sendMessage("§bPurchase Warrior at: ");
    35. }
    36. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
    Rocoty likes this.
  12. Rocoty aaomidi JordyPwner My "follow the rules of basic Java" was more to do with the way he was writing it :p

    Sans the "int", this could be seen as valid from a maths point of view, but not a Java point of view - it'd need to be int minutes = timeLeft/60
     
    BeastCraft3 and Skionz like this.
  13. Offline

    zackpollard


    I think we are safe under the assumption that most other people can work this out or google it.

    - Zack
     
  14. Offline

    Rocoty

    BeastCraft3 and AdamQpzm like this.
  15. zackpollard Better to provide the solution that to hide it to yourself, no matter how simple it seems ;)
     
    BeastCraft3 likes this.
  16. Offline

    BeastCraft3

    AdamQpzm likes this.
  17. Offline

    Funergy

    BeastCraft3 Use the TimeUnit thing that has been implemented in java
     
  18. Offline

    BrentHogeling

    JordyPwner I'm getting real tired of seeing your face on here, you come onto posts and talk shit, when you have no room to yourself. Do you actually know Java or are you here to bash on people, which are more then likely better then you, seriously just stop.
     
    Totom3 likes this.
  19. Offline

    JordyPwner

    Why do you make this thread offtopic? Sjeez. If u want to talk about me do it in pm not here -.-
     
Thread Status:
Not open for further replies.

Share This Page