Time restriction

How do I add a time restraint for my commands. I want to make it so players have to wait 1-2 minutes before they are able to use a command again to stop spammers. I don’t want to be using config manager for this because I feel like it will just be a massive mess.

get time and add if statement to check if time is X minutes from last time registered?

where would I store times and players though? I am pretty new and have been storing all my variables in the config Edit If I create a linked list, when I create it in my main method will it stay there until restart? I have been iffy on creating these things because I wasn’t sure if it just runs the method once and java just destroys all variables created.

Config ideally shouldn’t be used for storing anything. Local variables created during a method’s execution are removed, but class-level variables aren’t - you could store a Map<UUID, Instant> as a class-level variable.

1 Like

yes if you make a variable that is created with your plugin it will usually unless you make it do other stay there until server goes down, crashes etc

In each CommanExecutor you can add a Map<User, Long> that maps from users to the last time they used that command.
When it’s called next time, it checks the last time in the table, subtracts that from the current time, and executes the actual command code only if the difference is greater than the cooldown.


Ya, I decided to go with a map. use system milis and some math. Thanks guys.