I am working on a plugin for the game Minecraft, and one of the things that I would like to do is add a cooldown to a command sent in game. The only part I am having trouble with is the time part, I am not going to put my code here as most of it wouldn't make much sense due to it using the bukkit API. I would just like to know how to use the System.currentTimeMillis method to add a cooldown that could go as high as hours.
Having trouble getting time.
Page 1 of 14 Replies - 462 Views - Last Post: 13 October 2012 - 07:39 AM
Replies To: Having trouble getting time.
#2
Re: Having trouble getting time.
Posted 13 October 2012 - 04:06 AM
Doesn't minecraft have some kind of update method where you also have a delay time or something like that?
Another way you could do it, is by saving the time where the command should get off cooldown. Then all you have to do is check if the current time > cooldown time (if checking for off cooldown)
But I do not like using System.currentTimeMillis, instead you should use the time supplied by the engine you are using
private long cooldown; //Time left on the cooldown in ms. The command is off cooldown if cooldown is 0
public void update(long delay) {
cooldown = Math.max(0, cooldown - delay);
if (isOffCooldown()) {
//Not on cooldown
} else {
//On cooldown
}
}
private boolean isOffCooldown() {
return cooldown == 0;
}
private void setOnCooldown() {
cooldown = 3600000; //One hour in ms
}
Another way you could do it, is by saving the time where the command should get off cooldown. Then all you have to do is check if the current time > cooldown time (if checking for off cooldown)
But I do not like using System.currentTimeMillis, instead you should use the time supplied by the engine you are using
private long cooldown;
public void update() {
if (isOffCooldown()) {
//Not on cooldown
} else {
//On cooldown
}
}
private boolean isOffCooldown() {
return System.currentTimeMillis() > cooldown;
}
private void setOnCooldown() {
cooldown = System.currentTimeMillis() + 3600000;
}
#3
Re: Having trouble getting time.
Posted 13 October 2012 - 04:58 AM
Thank you so much, the way I Was doing it was way to involved, I applied your second bit of code to my plugin and it worked like a charm. You don't mind if I use it do you?
#4
Re: Having trouble getting time.
Posted 13 October 2012 - 05:08 AM
#5
Re: Having trouble getting time.
Posted 13 October 2012 - 07:39 AM
I was thinking of doing a tutorial on the bukkit api, but I don't know if it would fit here.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|