Cooldown Error

1) It's showing me this, how should I fix it? 2) How can I make it into seconds, rather then having it into miliseconds?
No description
43 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @shazim! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
shazim
shazimOP2mo ago
The miliseconds it is saying isn't even accurate
package me.shazim.shazimsPlugin.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

import java.util.HashMap;
import java.util.UUID;

public class Meow implements CommandExecutor {

// key = uuid of the player
// long = the epoch time of when they ran the command
private final HashMap<UUID, Long> cooldown;

public Meow(){
this.cooldown = new HashMap<>();
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p){

if (!this.cooldown.containsKey(p.getUniqueId()) || System.currentTimeMillis() - cooldown.get(p.getUniqueId()) > 5000){
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());

p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
return true;

}else {
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - System.currentTimeMillis() - cooldown.get(p.getUniqueId())) + " milliseconds");
}
}

return true;
}
}
package me.shazim.shazimsPlugin.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

import java.util.HashMap;
import java.util.UUID;

public class Meow implements CommandExecutor {

// key = uuid of the player
// long = the epoch time of when they ran the command
private final HashMap<UUID, Long> cooldown;

public Meow(){
this.cooldown = new HashMap<>();
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p){

if (!this.cooldown.containsKey(p.getUniqueId()) || System.currentTimeMillis() - cooldown.get(p.getUniqueId()) > 5000){
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());

p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
return true;

}else {
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - System.currentTimeMillis() - cooldown.get(p.getUniqueId())) + " milliseconds");
}
}

return true;
}
}
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1st2mo ago
you would need to put the System.currentTimeMillis() - cooldown.get(p.getUniqueId()) in () also you'd probably want to use variables because if you run System.currentTimeMillis() twice after each other, it's possible that these are giving you a different time in some cases (e.g. automatic time synchronization or daylight saving time), it is even possible that the second time is earlier than the first (which could be avoided using monotonic clocks like System.nanoTime()) for converting to seconds - just divide it by 1000 but think about what should happen if it's less than one second
Defective
Defective2mo ago
The TimeUnit class is also useful for converting between time units For example you can do TimeUnit.MILLISECONDS.toSeconds(timeInMillis) to convert a millisecond value to seconds there are also toHours, toMinutes methods and so on
shazim
shazimOP2mo ago
ohh, thank y’all so much
JavaBot
JavaBot2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
shazim
shazimOP2mo ago
I can’t code rn but if I have any questions I’ll let you know Sorry but in where would I put that? If you are able to guide me, that would be great. I'm not really good at Java, since I'm just beginning.
dan1st
dan1st2mo ago
You wrote
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - System.currentTimeMillis() - cooldown.get(p.getUniqueId())) + " milliseconds");
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - System.currentTimeMillis() - cooldown.get(p.getUniqueId())) + " milliseconds");
I suggested changing it to
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - (System.currentTimeMillis() - cooldown.get(p.getUniqueId()))) + " milliseconds");
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - (System.currentTimeMillis() - cooldown.get(p.getUniqueId()))) + " milliseconds");
Madjosz
Madjosz2mo ago
Or better refactor it to use variables right upfront:
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime) + " milliseconds");
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime) + " milliseconds");
shazim
shazimOP2mo ago
If I wanted it to be into seconds would I do the following?
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime / 1000) + " seconds");
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime / 1000) + " seconds");
dan1st
dan1st2mo ago
that was my second suggestion but I would put the variable before the if: https://discord.com/channels/648956210850299986/1394822979627651193/1394904022787428472
Hosti
Hosti2mo ago
also you'd probably want to use variables because if you run System.currentTimeMillis() twice after each other, it's possible that these are giving you a different time
shazim
shazimOP2mo ago
So like this?
package me.shazim.shazimsPlugin.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

import java.util.HashMap;
import java.util.UUID;

public class Meow implements CommandExecutor {

// key = uuid of the player
// long = the epoch time of when they ran the command
private final HashMap<UUID, Long> cooldown;

public Meow(){
this.cooldown = new HashMap<>();
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p){

if (!this.cooldown.containsKey(p.getUniqueId()) || System.currentTimeMillis() - cooldown.get(p.getUniqueId()) > 5000){
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());

p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
return true;

}else {
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime / 1000) + " seconds");
}
}

return true;
}
}
package me.shazim.shazimsPlugin.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

import java.util.HashMap;
import java.util.UUID;

public class Meow implements CommandExecutor {

// key = uuid of the player
// long = the epoch time of when they ran the command
private final HashMap<UUID, Long> cooldown;

public Meow(){
this.cooldown = new HashMap<>();
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p){

if (!this.cooldown.containsKey(p.getUniqueId()) || System.currentTimeMillis() - cooldown.get(p.getUniqueId()) > 5000){
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());

p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
return true;

}else {
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime / 1000) + " seconds");
}
}

return true;
}
}
Madjosz
Madjosz2mo ago
Overlooked it indeed. Problem is here that you want to handle nonexistence in the same if and Java has no if let or something like this. But one could just default to Instant.EPOCH.
dan1st
dan1st2mo ago
order of operation matters
shazim
shazimOP2mo ago
oops 💀 So like what should I exactly do? Could you futher explain?
Madjosz
Madjosz2mo ago
if (sender instanceof Player p){
long elapsedTime = System.currentTimeMillis() - cooldown.getOrDefault(p.getUniqueId(), 0L);
if (elapsedTime > 5000) {
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());
p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
} else {
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime) / 1000 + " seconds");
}
return true;
}
if (sender instanceof Player p){
long elapsedTime = System.currentTimeMillis() - cooldown.getOrDefault(p.getUniqueId(), 0L);
if (elapsedTime > 5000) {
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());
p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
} else {
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime) / 1000 + " seconds");
}
return true;
}
shazim
shazimOP2mo ago
What did you change in this example?
dan1st
dan1st2mo ago
look at the differences ;)
Madjosz
Madjosz2mo ago
I replaced this.cooldown.containsKey(p.getUniqueId()) and cooldown.get(p.getUniqueId()) by cooldown.getOrDefault(p.getUniqueId(), 0L). (Hint: Only works until 17.08.+292278994 07:12:55 UTC)
dan1st
dan1st2mo ago
that makes using System.nanoTime() harder
Madjosz
Madjosz2mo ago
No description
dan1st
dan1st2mo ago
Yeah your program shouldn't run that long you can ignore that issue like it's an issue only if you run it for that long without restarting
dan1st
dan1st2mo ago
You should update your JDK which includes restarting your Java applications at least every two years
Madjosz
Madjosz2mo ago
You cannot update your Java on your time capsule orbiting some proximate black hole.
dan1st
dan1st2mo ago
That's not how monotonic clocks work even orbiting a black hole doesn't break it with System.nanoTime()
Madjosz
Madjosz2mo ago
It can break 292 years without restart though.
dan1st
dan1st2mo ago
And how do you achieve that? If you are orbiting a black hole, it may be 292 years from the outside but monotonic clocks don't synchronize or similar. They are measing the time difference on the system itself, not outside
Madjosz
Madjosz2mo ago
I can still wait more than 292 years before I call the function the next time.
dan1st
dan1st2mo ago
That requires keeping the JVM running for that time which is unreasonable.
Madjosz
Madjosz2mo ago
So was my setting.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
shazim
shazimOP2mo ago
This just gave me alot more errors
shazim
shazimOP2mo ago
No description
shazim
shazimOP2mo ago
package me.shazim.shazimsPlugin.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

import java.util.HashMap;
import java.util.UUID;

public class Meow implements CommandExecutor {

// key = uuid of the player
// long = the epoch time of when they ran the command
private final HashMap<UUID, Long> cooldown;

public Meow(){
this.cooldown = new HashMap<>();
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p){

long elapsedTime = System.currentTimeMillis() - cooldown.getOrDefault(p.getUniqueId(), 0L);
if (elapsedTime > 5000) {
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());
p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
} else {
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime) / 1000 + " seconds");
}
return true;
}

}else {
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime / 1000) + " seconds");
}
}

return true;
}
}
package me.shazim.shazimsPlugin.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

import java.util.HashMap;
import java.util.UUID;

public class Meow implements CommandExecutor {

// key = uuid of the player
// long = the epoch time of when they ran the command
private final HashMap<UUID, Long> cooldown;

public Meow(){
this.cooldown = new HashMap<>();
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


if (sender instanceof Player p){

long elapsedTime = System.currentTimeMillis() - cooldown.getOrDefault(p.getUniqueId(), 0L);
if (elapsedTime > 5000) {
this.cooldown.put(p.getUniqueId(), System.currentTimeMillis());
p.sendMessage(ChatColor.LIGHT_PURPLE + "Meow :3");
} else {
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime) / 1000 + " seconds");
}
return true;
}

}else {
long elapsedTime = System.currentTimeMillis() - cooldown.get(p.getUniqueId());
p.sendMessage(ChatColor.RED + "Unfortunately, you cannot meow for another " + (5000 - elapsedTime / 1000) + " seconds");
}
}

return true;
}
}
Nvm I did it wrong How'd we get from a cooldown error to blackholes 💀
ayylmao123xdd
ayylmao123xdd2mo ago
welcome to the java server :GnuTrolling:
shazim
shazimOP2mo ago
:thonk:
panda
panda2mo ago
:spring:
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
shazim
shazimOP2mo ago
. Ty
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@810632160418988053>.

Did you find this page helpful?