add /rankup cooldown
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group 'sh.okx'
|
group 'sh.okx'
|
||||||
version '3.0-alpha.12'
|
version '3.0-alpha.13'
|
||||||
|
|
||||||
sourceCompatibility = 1.8
|
sourceCompatibility = 1.8
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ import sh.okx.rankup.ranks.requirements.XpLevelRequirement;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.WeakHashMap;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class Rankup extends JavaPlugin {
|
public class Rankup extends JavaPlugin {
|
||||||
@@ -54,6 +56,11 @@ public class Rankup extends JavaPlugin {
|
|||||||
@Getter
|
@Getter
|
||||||
private Placeholders placeholders;
|
private Placeholders placeholders;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Players who cannot rankup for a certain amount of time.
|
||||||
|
*/
|
||||||
|
private Map<Player, Long> cooldowns;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerRequirements();
|
registerRequirements();
|
||||||
@@ -85,6 +92,7 @@ public class Rankup extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
public void reload() {
|
||||||
|
cooldowns = new WeakHashMap<>();
|
||||||
closeInventories();
|
closeInventories();
|
||||||
loadConfigs();
|
loadConfigs();
|
||||||
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
||||||
@@ -204,6 +212,11 @@ public class Rankup extends JavaPlugin {
|
|||||||
.send(player);
|
.send(player);
|
||||||
|
|
||||||
oldRank.runCommands(player, rank);
|
oldRank.runCommands(player, rank);
|
||||||
|
|
||||||
|
// apply cooldown last
|
||||||
|
if(config.getInt("cooldown") > 0) {
|
||||||
|
cooldowns.put(player, System.currentTimeMillis());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -239,7 +252,23 @@ public class Rankup extends JavaPlugin {
|
|||||||
replaceRequirements(player, builder, rank);
|
replaceRequirements(player, builder, rank);
|
||||||
builder.send(player);
|
builder.send(player);
|
||||||
return false;
|
return false;
|
||||||
|
} else if (cooldowns.containsKey(player)) {
|
||||||
|
long time = System.currentTimeMillis() - cooldowns.get(player);
|
||||||
|
// if time passed is less than the cooldown
|
||||||
|
long timeLeft = (config.getInt("cooldown") * 1000) - time;
|
||||||
|
if (timeLeft > 0) {
|
||||||
|
long secondsLeft = (long) Math.ceil(timeLeft / 1000f);
|
||||||
|
getMessage(rank, secondsLeft > 1 ? Message.COOLDOWN_PLURAL : Message.COOLDOWN_SINGULAR)
|
||||||
|
.failIfEmpty()
|
||||||
|
.replaceAll(player, rank)
|
||||||
|
.replace(Variable.SECONDS, secondsLeft)
|
||||||
|
.send(player);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
// cooldown has expired so remove it
|
||||||
|
cooldowns.remove(player);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import org.bukkit.entity.Player;
|
|||||||
import sh.okx.rankup.Rankup;
|
import sh.okx.rankup.Rankup;
|
||||||
import sh.okx.rankup.gui.Gui;
|
import sh.okx.rankup.gui.Gui;
|
||||||
import sh.okx.rankup.messages.Message;
|
import sh.okx.rankup.messages.Message;
|
||||||
import sh.okx.rankup.messages.Variable;
|
|
||||||
import sh.okx.rankup.ranks.Rank;
|
import sh.okx.rankup.ranks.Rank;
|
||||||
import sh.okx.rankup.ranks.Rankups;
|
import sh.okx.rankup.ranks.Rankups;
|
||||||
|
|
||||||
@@ -16,7 +15,10 @@ import java.util.Map;
|
|||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
public class RankupCommand implements CommandExecutor {
|
public class RankupCommand implements CommandExecutor {
|
||||||
|
// weak hash maps so players going offline are automatically removed.
|
||||||
|
// otherwise there is a potential (but small) memory leak.
|
||||||
private final Map<Player, Long> confirming = new WeakHashMap<>();
|
private final Map<Player, Long> confirming = new WeakHashMap<>();
|
||||||
|
|
||||||
private final Rankup plugin;
|
private final Rankup plugin;
|
||||||
|
|
||||||
public RankupCommand(Rankup plugin) {
|
public RankupCommand(Rankup plugin) {
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ class EmptyMessageBuilder extends MessageBuilder {
|
|||||||
super(null);
|
super(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* what are you doing failing if empty when it has already failed?
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public MessageBuilder failIfEmpty() {
|
public MessageBuilder failIfEmpty() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ public enum Message {
|
|||||||
RANKS_FOOTER("rankup.ranks.footer"),
|
RANKS_FOOTER("rankup.ranks.footer"),
|
||||||
RANKS_COMPLETE("rankup.ranks.complete"),
|
RANKS_COMPLETE("rankup.ranks.complete"),
|
||||||
RANKS_CURRENT("rankup.ranks.current"),
|
RANKS_CURRENT("rankup.ranks.current"),
|
||||||
RANKS_INCOMPLETE("rankup.ranks.incomplete");
|
RANKS_INCOMPLETE("rankup.ranks.incomplete"),
|
||||||
|
COOLDOWN_SINGULAR("rankup.cooldown.singular"),
|
||||||
|
COOLDOWN_PLURAL("rankup.cooldown.plural");
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
package sh.okx.rankup.messages;
|
package sh.okx.rankup.messages;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import net.milkbowl.vault.economy.Economy;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import sh.okx.rankup.ranks.Rank;
|
import sh.okx.rankup.ranks.Rank;
|
||||||
import sh.okx.rankup.ranks.requirements.Requirement;
|
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@@ -65,6 +61,7 @@ public class MessageBuilder {
|
|||||||
/**
|
/**
|
||||||
* Fails the MessageBuilder if the message is empty.
|
* Fails the MessageBuilder if the message is empty.
|
||||||
* if this fails, all subsequent calls to that MessageBuilder will do nothing
|
* if this fails, all subsequent calls to that MessageBuilder will do nothing
|
||||||
|
*
|
||||||
* @return an EmptyMessageBuilder if the message is empty, itself otherwise
|
* @return an EmptyMessageBuilder if the message is empty, itself otherwise
|
||||||
*/
|
*/
|
||||||
public MessageBuilder failIfEmpty() {
|
public MessageBuilder failIfEmpty() {
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ public enum Variable {
|
|||||||
AMOUNT,
|
AMOUNT,
|
||||||
AMOUNT_NEEDED,
|
AMOUNT_NEEDED,
|
||||||
PERCENT_DONE,
|
PERCENT_DONE,
|
||||||
PERCENT_LEFT;
|
PERCENT_LEFT,
|
||||||
|
SECONDS;
|
||||||
|
|
||||||
public String replace(String message, String value, String type) {
|
public String replace(String message, String value, String type) {
|
||||||
Pattern pattern = Pattern.compile("\\{" + type + "_" + this + "}", Pattern.CASE_INSENSITIVE);
|
Pattern pattern = Pattern.compile("\\{" + type + "_" + this + "}", Pattern.CASE_INSENSITIVE);
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ ranks: true
|
|||||||
# options are: gui, text or none
|
# options are: gui, text or none
|
||||||
confirmation-type: 'gui'
|
confirmation-type: 'gui'
|
||||||
|
|
||||||
|
# how long in seconds people have to wait between a /rankup
|
||||||
|
# only successful rankups start the cooldown.
|
||||||
|
# set to 0 to disable.
|
||||||
|
cooldown: 1
|
||||||
|
|
||||||
gui:
|
gui:
|
||||||
rows: 1
|
rows: 1
|
||||||
rankup:
|
rankup:
|
||||||
@@ -34,7 +39,7 @@ gui:
|
|||||||
|
|
||||||
text:
|
text:
|
||||||
# the time in seconds for a player to
|
# the time in seconds for a player to
|
||||||
# confirm ranking up by typing the command again
|
# confirm by typing /rankup again
|
||||||
timeout: 10
|
timeout: 10
|
||||||
|
|
||||||
placeholders:
|
placeholders:
|
||||||
|
|||||||
@@ -32,5 +32,9 @@ rankup:
|
|||||||
# an empty string disables the header/footer
|
# an empty string disables the header/footer
|
||||||
header: ''
|
header: ''
|
||||||
footer: ''
|
footer: ''
|
||||||
|
# sent when a player tries to rankup when they are on cooldown
|
||||||
|
cooldown:
|
||||||
|
singular: '&cYou must wait {SECONDS} more second to rankup again.'
|
||||||
|
plural: '&cYou must wait {SECONDS} more seconds to rankup again.'
|
||||||
|
|
||||||
not-in-ladder: "&cSorry, but we could not find any rankups for the group(s) you are in."
|
not-in-ladder: "&cSorry, but we could not find any rankups for the group(s) you are in."
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
name: Rankup
|
name: Rankup
|
||||||
version: 3.0-alpha.12
|
version: 3.0-alpha.13
|
||||||
main: sh.okx.rankup.Rankup
|
main: sh.okx.rankup.Rankup
|
||||||
author: Okx
|
author: Okx
|
||||||
depend: [Vault]
|
depend: [Vault]
|
||||||
|
|||||||
Reference in New Issue
Block a user