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' group 'sh.okx'
version '3.0-alpha.12' version '3.0-alpha.13'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
+33 -4
View File
@@ -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();
@@ -69,7 +76,7 @@ public class Rankup extends JavaPlugin {
} }
}); });
if(config.getBoolean("ranks")) { if (config.getBoolean("ranks")) {
getCommand("ranks").setExecutor(new RankListCommand(this)); getCommand("ranks").setExecutor(new RankListCommand(this));
} }
getCommand("rankup").setExecutor(new RankupCommand(this)); getCommand("rankup").setExecutor(new RankupCommand(this));
@@ -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,22 +252,38 @@ 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;
} }
public void replaceRequirements(Player player, MessageBuilder builder, Rank rank) { public void replaceRequirements(Player player, MessageBuilder builder, Rank rank) {
DecimalFormat simpleFormat = placeholders.getSimpleFormat(); DecimalFormat simpleFormat = placeholders.getSimpleFormat();
DecimalFormat percentFormat = placeholders.getPercentFormat(); 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, requirement, () -> simpleFormat.format(requirement.getAmount()));
replaceRequirements(builder, Variable.AMOUNT_NEEDED, requirement, () -> simpleFormat.format(requirement.getRemaining(player))); 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_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)));
} }
} }
private void replaceRequirements(MessageBuilder builder, Variable variable, Requirement requirement, Supplier<Object> value) { private void replaceRequirements(MessageBuilder builder, Variable variable, Requirement requirement, Supplier<Object> value) {
builder.replace(variable + " " + requirement.getName(), value.get()); builder.replace(variable + " " + requirement.getName(), value.get());
} }
} }
@@ -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,10 +61,11 @@ 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() {
if(message.isEmpty()) { if (message.isEmpty()) {
return new EmptyMessageBuilder(); return new EmptyMessageBuilder();
} else { } else {
return this; 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. * ie, calls MessageBuilder#send(Player) for all players online, and sends the message in the console.
*/ */
public void broadcast() { public void broadcast() {
for(Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
send(player); send(player);
} }
send(Bukkit.getConsoleSender()); send(Bukkit.getConsoleSender());
@@ -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);
+6 -1
View File
@@ -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:
+4
View File
@@ -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 -1
View File
@@ -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]