4 Replies - 1480 Views - Last Post: 13 October 2012 - 07:39 AM Rate Topic: -----

#1 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Having trouble getting time.

Posted 13 October 2012 - 12:56 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble getting time.

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

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?

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;
}


Was This Post Helpful? 1
  • +
  • -

#3 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

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?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: Having trouble getting time.

Posted 13 October 2012 - 05:08 AM

View PostYtry, on 13 October 2012 - 11:58 AM, said:

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?


You're welcome

No I do not mind you using it lol
Was This Post Helpful? 0
  • +
  • -

#5 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1