CoolDown Problem

Discussion in 'Plugin Development' started by Divinity Realms, Feb 4, 2015.

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

    Divinity Realms

    Im getting this problem were the cooldown doesnt work at all.. Its constant and also every time you click it i think it adds more to the cooldown. Please help! http://pastebin.com/UrH3jR0C And ive looked up a lot of tutorials dont work
     
  2. Offline

    sirrus86

    Actually it would appear your cooldown is only half a second long.
     
  3. Offline

    BaconStripzMan

    10 = Half a second, 20 = 1 second :p
     
  4. i didn't take a look at your code, but i will tell you a very easy way to check if a cooldown is over or not.
    Code:
    public class SomeClass{
     private long coolDown = 2000; // cooldown = 2 seconds
     private long lastCall = 0;
    
     public void call(){
      final long currentTime = System.currentTimeMillis();
      final long cooldownEnd = lastCall + coolDown;
      if(currentTime<cooldownEnd)
       throw new IllegalStateException("Remaining cooldown: "+(cooldownEnd-currentTime));
      lastCall = currentTime;
      // do your stuff here
     }
    }
    Every time "call()" is called, it runs through your code or it throws an illegalstateexception, if the time of the cooldown (in millieseconds) didn't go over since the last time "call()" was called
     
  5. Offline

    nverdier

  6. @nverdier dude i am working as a java developer xD i was just to lazy to figure out another way. easier and better way would ve been to return a boolean which tells if it worked or not. i know throwing exceptions is nothing you should do in this case :D i wrote it by the way without editor in this stupid little textbox which is used to add code. so i didnt wanted to look up something or whatever, it was the first thing i had in my mind. dont know what to do -> throw exception xD you should be happy that i didn't throw just an Exception instead of an IllegalStateException. I just wanted to show my idea with this. how he/she uses it doesn't matter.
     
  7. Offline

    sirrus86

    Code:
    if (something == null) {
         throw new Throwable(new Throwable(new Throwable(new Throwable("it broke"))));
    }
    I don't see the problem.
     
    leon3001 likes this.
  8. Offline

    leon3001

  9. Offline

    mythbusterma

    @Shmobi

    I work as a developer as well. That sort of code wouldn't fly.

    Exceptions are for exceptional states, unrecoverable errors, etc. NOT for normal program behaviour that should be checked against. That is what special return values are for, in this case simply returning false would've been sufficient.

    Also, you're going to need some sort of structure to store that information associated with the player.
     
    Darkpicasa likes this.
  10. Offline

    1Rogue

    I would've fired you if you did that in production code.
     
    ProMCKingz, teej107 and mythbusterma like this.
  11. @mythbusterma the code i posted is only suppost to show how to realize cooldowns. it is not connected to the actual problem, thats why i didnt include a connection to a player or something. as you maybe read, i said in my post that i havent read his code and i dont know what the problem is. i just read cooldown and that there is a problem with handling it. thats why i showed only how to do something like that. simply add a member of type uuid and you got your connection to the player or whatever

    @1Rogue calm down, i wouldnt

    Just saying to make everybody happe: i used the exception in this case just because i searched for a short way to break the code and get some informations why it quited. i would never use an exception like this in a "real code". this was just an example/pseudocode to show my idea how to deal with a cooldown. it was not meant to be copypasted!
     
  12. Offline

    teej107

    That still doesn't make me happy. You can log/print a message and end the method within the if statement without throwing an exception.

    Here is how to use cooldowns

    1. Store a long with the System's time as the value.
    2. When your ready to check the cooldown, get the difference of the System's now current time and the stored long value and compare it with the preferred cooldown time!

    Code:
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    pi.addItem(flightDust);
    Do you have something against for loops?
     
    Darkpicasa likes this.
  13. Offline

    Darkpicasa

    Then what's the point of posting here?
     
  14. @teej107 dude, read the whole post before complaining -.- what you discribed is exaclty what i am doing in my code. people are just compaining because instread of returning true or false or something to tell the methodcaller that the cooldown was over or not, i throw an exception to do that. i also explained why i did that, but i think you didnt read this either ......

    @Darkpicasa repeating my self... read the whole post and you will know. i already explained it. also givint a hint how to do something must not be in a direct connection to the actual problem and can be usefull, too.
     
  15. Offline

    teej107

    So what's the problem? Can we see your current "real code" then?
     
  16. @teej107 oh my god... read the whole post or be quiet. but stop complaining about single lines ripped out of the whole context -.- in my first post you can see my code, which does exactly what you discribed. it is not the logic which is wrong in my code. there is nothing wrong, it is just fucking ugly that i used an exception to "return" the method caller, that the cooldown isnt over. thats the only thing people were complaining about. and now you come and complain about every line i wrote, but not in once.
     
  17. Offline

    teej107

    @Shmobi Well it's rather annoying that you paste poor code and when people tell you what's wrong with it, you then say its pseudo/example code.

    EDIT: Should have been more clear in my first post but the 2 sentences below the quote was directed toward you. The rest toward the OP.
     
  18. @teej107 well then sorry for my agressive way. i dont blame ppl for finding erros in my code. i just blame ppl for tring to find everything you could imagine as an error. thats annyoing. i posted an example how to do this. this does not mean that the person i helped shall copy paste it. it is only suppost to show him the way of doing it, in this case return something after you are done. understood your sentence wrong then
     
Thread Status:
Not open for further replies.

Share This Page