add /rankup cooldown

This commit is contained in:
okx-code
2018-08-28 20:42:26 +01:00
parent 3fca80a79b
commit 923cc8918c
10 changed files with 59 additions and 16 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ plugins {
}
group 'sh.okx'
version '3.0-alpha.12'
version '3.0-alpha.13'
sourceCompatibility = 1.8
+32 -3
View File
@@ -33,6 +33,8 @@ import sh.okx.rankup.ranks.requirements.XpLevelRequirement;
import java.io.File;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.Supplier;
public class Rankup extends JavaPlugin {
@@ -54,6 +56,11 @@ public class Rankup extends JavaPlugin {
@Getter
private Placeholders placeholders;
/**
* Players who cannot rankup for a certain amount of time.
*/
private Map<Player, Long> cooldowns;
@Override
public void onEnable() {
registerRequirements();
@@ -69,7 +76,7 @@ public class Rankup extends JavaPlugin {
}
});
if(config.getBoolean("ranks")) {
if (config.getBoolean("ranks")) {
getCommand("ranks").setExecutor(new RankListCommand(this));
}
getCommand("rankup").setExecutor(new RankupCommand(this));
@@ -85,6 +92,7 @@ public class Rankup extends JavaPlugin {
}
public void reload() {
cooldowns = new WeakHashMap<>();
closeInventories();
loadConfigs();
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
@@ -204,6 +212,11 @@ public class Rankup extends JavaPlugin {
.send(player);
oldRank.runCommands(player, rank);
// apply cooldown last
if(config.getInt("cooldown") > 0) {
cooldowns.put(player, System.currentTimeMillis());
}
}
/**
@@ -239,18 +252,34 @@ public class Rankup extends JavaPlugin {
replaceRequirements(player, builder, rank);
builder.send(player);
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;
}
public void replaceRequirements(Player player, MessageBuilder builder, Rank rank) {
DecimalFormat simpleFormat = placeholders.getSimpleFormat();
DecimalFormat percentFormat = placeholders.getPercentFormat();
for(Requirement requirement : rank.getRequirements()) {
for (Requirement requirement : rank.getRequirements()) {
replaceRequirements(builder, Variable.AMOUNT, requirement, () -> simpleFormat.format(requirement.getAmount()));
replaceRequirements(builder, Variable.AMOUNT_NEEDED, requirement, () -> simpleFormat.format(requirement.getRemaining(player)));
replaceRequirements(builder, Variable.PERCENT_LEFT, requirement, () -> percentFormat.format(Math.max(0, (requirement.getRemaining(player) / requirement.getAmount()) * 100)));
replaceRequirements(builder, Variable.PERCENT_DONE, requirement, () -> percentFormat.format(Math.min(100, (1-(requirement.getRemaining(player) / requirement.getAmount())) * 100)));
replaceRequirements(builder, Variable.PERCENT_DONE, requirement, () -> percentFormat.format(Math.min(100, (1 - (requirement.getRemaining(player) / requirement.getAmount())) * 100)));
}
}
@@ -8,7 +8,6 @@ import org.bukkit.entity.Player;
import sh.okx.rankup.Rankup;
import sh.okx.rankup.gui.Gui;
import sh.okx.rankup.messages.Message;
import sh.okx.rankup.messages.Variable;
import sh.okx.rankup.ranks.Rank;
import sh.okx.rankup.ranks.Rankups;
@@ -16,7 +15,10 @@ import java.util.Map;
import java.util.WeakHashMap;
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 Rankup plugin;
public RankupCommand(Rankup plugin) {
@@ -7,6 +7,9 @@ class EmptyMessageBuilder extends MessageBuilder {
super(null);
}
/**
* what are you doing failing if empty when it has already failed?
*/
@Override
public MessageBuilder failIfEmpty() {
throw new UnsupportedOperationException();
@@ -14,7 +14,9 @@ public enum Message {
RANKS_FOOTER("rankup.ranks.footer"),
RANKS_COMPLETE("rankup.ranks.complete"),
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
private final String name;
@@ -1,16 +1,12 @@
package sh.okx.rankup.messages;
import lombok.AllArgsConstructor;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
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.Pattern;
@@ -65,10 +61,11 @@ public class MessageBuilder {
/**
* Fails the MessageBuilder if the message is empty.
* if this fails, all subsequent calls to that MessageBuilder will do nothing
*
* @return an EmptyMessageBuilder if the message is empty, itself otherwise
*/
public MessageBuilder failIfEmpty() {
if(message.isEmpty()) {
if (message.isEmpty()) {
return new EmptyMessageBuilder();
} else {
return this;
@@ -84,7 +81,7 @@ public class MessageBuilder {
* ie, calls MessageBuilder#send(Player) for all players online, and sends the message in the console.
*/
public void broadcast() {
for(Player player : Bukkit.getOnlinePlayers()) {
for (Player player : Bukkit.getOnlinePlayers()) {
send(player);
}
send(Bukkit.getConsoleSender());
@@ -14,7 +14,8 @@ public enum Variable {
AMOUNT,
AMOUNT_NEEDED,
PERCENT_DONE,
PERCENT_LEFT;
PERCENT_LEFT,
SECONDS;
public String replace(String message, String value, String type) {
Pattern pattern = Pattern.compile("\\{" + type + "_" + this + "}", Pattern.CASE_INSENSITIVE);
+6 -1
View File
@@ -10,6 +10,11 @@ ranks: true
# options are: gui, text or none
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:
rows: 1
rankup:
@@ -34,7 +39,7 @@ gui:
text:
# the time in seconds for a player to
# confirm ranking up by typing the command again
# confirm by typing /rankup again
timeout: 10
placeholders:
+4
View File
@@ -32,5 +32,9 @@ rankup:
# an empty string disables the header/footer
header: ''
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."
+1 -1
View File
@@ -1,5 +1,5 @@
name: Rankup
version: 3.0-alpha.12
version: 3.0-alpha.13
main: sh.okx.rankup.Rankup
author: Okx
depend: [Vault]