Skip to content

Commit

Permalink
Cooldown (#22)
Browse files Browse the repository at this point in the history
* 🚧 Add cooldown

* 🐛 Fix message cooldown

* add updated cooldown impl

---------

Co-authored-by: Ryder Belserion <[email protected]>
  • Loading branch information
Maxlego08 and ryderbelserion authored Oct 21, 2024
1 parent 38a88a9 commit f21601e
Show file tree
Hide file tree
Showing 9 changed files with 1,138 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public enum Messages {
hit_voucher_limit(MessageKeys.hit_voucher_limit),
two_step_authentication(MessageKeys.two_step_authentication),
has_blacklist_permission(MessageKeys.has_blacklist_permission),
cooldown_active(MessageKeys.cooldown_active),
not_in_whitelisted_world(MessageKeys.not_in_whitelist_world),
unstack_item(MessageKeys.unstack_item),
cannot_put_items_in_crafting_table(MessageKeys.cannot_put_items_in_crafting_table),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import com.badbones69.crazyvouchers.config.ConfigManager;
import com.badbones69.crazyvouchers.config.types.ConfigKeys;
import java.util.ArrayList;
Expand All @@ -35,6 +35,10 @@ public class Voucher {
private final ItemBuilder itemBuilder;

private final String name;

private final boolean hasCooldown;
private final int cooldownInterval;

private boolean usesArgs;
private final boolean glowing;
private final String usedMessage;
Expand Down Expand Up @@ -68,6 +72,8 @@ public class Voucher {

private final List<ItemFlag> itemFlags = new ArrayList<>();

private final Map<UUID, Long> cooldowns = new HashMap<>();

private String requiredPlaceholdersMessage;

public Voucher(int number) {
Expand All @@ -88,17 +94,23 @@ public Voucher(int number) {
this.fireworkToggle = false;
this.isEdible = false;
this.glowing = false;

this.hasCooldown = false;
this.cooldownInterval = 0;
}

public Voucher(FileConfiguration fileConfiguration, String name) {
this.name = name;
this.usesArgs = false;

@NotNull CrazyVouchers plugin = CrazyVouchers.get();
final CrazyVouchers plugin = CrazyVouchers.get();
final boolean loadOldWay = ConfigManager.getConfig().getProperty(ConfigKeys.mono_file);

String path = loadOldWay ? "vouchers." + name + "." : "voucher.";

this.hasCooldown = fileConfiguration.getBoolean(path + "cooldown.toggle", false);
this.cooldownInterval = fileConfiguration.getInt(path + "cooldown.interval", 5);

this.itemBuilder = new ItemBuilder()
.setMaterial(fileConfiguration.getString(path + "item", "Stone"))
.setName(fileConfiguration.getString(path + "name", ""))
Expand Down Expand Up @@ -442,6 +454,26 @@ public boolean isEdible() {
return this.isEdible;
}

public boolean hasCooldown() {
return this.hasCooldown;
}

public int getCooldown() {
return this.cooldownInterval;
}

public boolean isCooldown(final Player player) {
return this.cooldowns.getOrDefault(player.getUniqueId(), 0L) >= System.currentTimeMillis();
}

public void addCooldown(final Player player) {
this.cooldowns.put(player.getUniqueId(), System.currentTimeMillis() + (1000L * getCooldown()));
}

public void removeCooldown(final Player player) {
this.cooldowns.remove(player.getUniqueId());
}

private String getMessage(String path, FileConfiguration file) {
String messageString;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public void registerComments(CommentsConfiguration conf) {
""")
public static final Property<Boolean> mono_file = newProperty("settings.use-old-file-system", false);

@Comment("Should players have a voucher cooldown?")
public static final Property<Boolean> cooldown_toggle = newProperty("settings.cooldown.toggle", false);

@Comment("How many seconds should a player have to wait, before opening a new voucher?")
public static final Property<Integer> cooldown_wait = newProperty("settings.cooldown.interval", 5);

@Comment({
"Vouchers will no longer be able to stack, as each one has a unique identifier",
"Once the voucher is used, the uuid attached is thrown in a file.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void registerComments(CommentsConfiguration conf) {
public static final Property<String> not_a_number = newProperty("voucher.requirements.not-a-number", "{prefix}&c{arg} is not a number.");

public static final Property<String> not_a_voucher = newProperty("voucher.requirements.not-a-voucher", "{prefix}&cThat is not a Voucher Type.");

public static final Property<String> has_blacklist_permission = newProperty("voucher.requirements.un-stack-item", "{prefix}&cSorry but you can not use this voucher because you have a black-listed permission.");

public static final Property<String> not_in_whitelist_world = newProperty("voucher.requirements.not-in-world", "{prefix}&cYou can not use that voucher here as you are not in a whitelisted world for this voucher.");
Expand All @@ -83,6 +84,9 @@ public void registerComments(CommentsConfiguration conf) {

public static final Property<String> sent_voucher = newProperty("voucher.sent-voucher", "{prefix}&3You have just given &6{player} &3a &6{voucher} &3voucher.");

@Comment("A list of available placeholders: {time}")
public static final Property<String> cooldown_active = newProperty("voucher.cooldown", "{prefix}Please wait {time}, before using the voucher again");

public static final Property<String> sent_everyone_voucher = newProperty("voucher.sent-everyone-voucher", "{prefix}&3You have just given all players a &6{voucher} &3voucher.");

public static final Property<String> config_reload = newProperty("misc.config-reload", "{prefix}&7You have just reloaded the configurations.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ private void useVoucher(Player player, Voucher voucher, ItemStack item) {

return;
}

if (voucher.hasCooldown() && voucher.isCooldown(player)){
player.sendMessage(Messages.cooldown_active.getMessage(player, "{time}", String.valueOf(voucher.getCooldown())));

return;
} else {
voucher.removeCooldown(player); // remove cooldown, to avoid the gc not cleaning it up just in case.
}
}

if (Support.placeholder_api.isEnabled()) {
Expand Down Expand Up @@ -358,6 +366,10 @@ private void populate(Player player, String argument) {
private void voucherClick(Player player, ItemStack item, Voucher voucher, String argument) {
Methods.removeItem(item, player);

if (voucher.hasCooldown()){
voucher.addCooldown(player);
}

populate(player, argument);

for (String command : voucher.getCommands()) {
Expand Down
214 changes: 214 additions & 0 deletions src/main/java/com/badbones69/crazyvouchers/api/enums/Messages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package com.badbones69.crazyvouchers.api.enums;

import com.badbones69.crazyvouchers.Methods;
import com.badbones69.crazyvouchers.CrazyVouchers;
import com.badbones69.crazyvouchers.api.FileManager.Files;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public enum Messages {

RELOAD("Config-Reload", "&7You have just reloaded the Config.yml"),
INVENTORY_FULL("Inventory-Full", "&cYour inventory is to full. Please open up some space to buy that."),
PLAYERS_ONLY("Reload", "&cOnly players can use this command."),
NO_PERMISSION("No-Permission", "&cYou do not have permission to use that command!"),
NO_PERMISSION_TO_VOUCHER("No-Permission-To-Voucher", "&cYou do not have permission to use that voucher."),
NOT_ONLINE("Not-Online", "&cThat player is not online at this time."),
NOT_A_NUMBER("Not-A-Number", "&c%Arg% is not a number."),
NOT_A_VOUCHER("Not-A-Voucher", "&cThat is not a Voucher Type."),
NOT_A_PLAYER("Not-A-Player", "&cYou must be a player to use this command."),
CODE_UNAVAILABLE("Code-UnAvailable", "&cThe Voucher code &6%Arg% &cis incorrect or unavailable at this time."),
CODE_USED("Code-Used", "&cThe voucher code &6%code% &chas already been used."),
GIVEN_A_VOUCHER("Given-A-Voucher", "&3You have just given &6%Player% &3a &6%Voucher% &3voucher."),
GIVEN_ALL_PLAYERS_VOUCHER("Given-All-Players-Voucher", "&3You have just given all players a &6%Voucher% &3voucher."),
HIT_LIMIT("Hit-Limit", "&cYou have hit your limit for using this voucher."),

COOLDOWN("Cooldown", "&c.Please wait a few seconds before using the voucher again."),
TWO_STEP_AUTHENTICATION("Two-Step-Authentication", "&7Right click again to confirm that you want to use this voucher."),
HAS_BLACKLIST_PERMISSION("Has-Blacklist-Permission", "&cSorry but you can not use this voucher because you have a black-listed permission."),
NOT_IN_WHITELISTED_WORLD("Not-In-Whitelisted-World", "&cYou can not use that voucher here as you are not in a whitelisted world for this voucher."),
UNSTACK_ITEM("Unstack-Item", "&cYou need to unstack that item before you can use it."),
NO_PERMISSION_TO_USE_VOUCHER_IN_OFFHAND("No-Permission-To-Use-Voucher-In-OffHand", "&cYou do not have permission to use vouchers in your off hand."),
CANNOT_PUT_ITEMS_IN_CRAFTING_TABLE("Cannot-Put-Items-In-Crafting-Table", "&cYou cannot put vouchers in the crafting table."),
HELP("Help",
Arrays.asList(
"&8- &6/Voucher Help &3Lists all the commands for vouchers.",
"&8- &6/Voucher Types &3Lists all types of vouchers and codes.",
"&8- &6/Voucher Redeem <Code> &3Allows player to redeem a voucher code.",
"&8- &6/Voucher Give <Type> [Amount] [Player] [Arguments] &3Gives a player a voucher.",
"&8- &6/Voucher GiveAll <Type> [Amount] [Arguments] &3Gives all players a voucher.",
"&8- &6/Voucher Open [Page] &3Opens a GUI so you can get vouchers easy.",
"&8- &6/Voucher Reload &3Reloaded the configuration files."));

private final String path;
private String defaultMessage;
private List<String> defaultListMessage;

Messages(String path, String defaultMessage) {
this.path = path;
this.defaultMessage = defaultMessage;
}

Messages(String path, List<String> defaultListMessage) {
this.path = path;
this.defaultListMessage = defaultListMessage;
}

private static CrazyVouchers plugin = CrazyVouchers.getPlugin();

private static Methods methods = plugin.getMethods();

public static String convertList(List<String> list) {
StringBuilder message = new StringBuilder();

for (String line : list) {
message.append(methods.color(line)).append("\n");
}

return message.toString();
}

public static void addMissingMessages() {
FileConfiguration messages = Files.MESSAGES.getFile();
boolean saveFile = false;

for (Messages message : values()) {
if (!messages.contains("Messages." + message.getPath())) {
saveFile = true;

if (message.getDefaultMessage() != null) {
messages.set("Messages." + message.getPath(), message.getDefaultMessage());
} else {
messages.set("Messages." + message.getPath(), message.getDefaultListMessage());
}
}
}

if (saveFile) {
Files.MESSAGES.saveFile();
}
}

public String getMessage() {
return getMessage(true);
}

public String getMessage(String placeholder, String replacement) {
HashMap<String, String> placeholders = new HashMap<>();
placeholders.put(placeholder, replacement);
return getMessage(placeholders, true);
}

public String getMessage(HashMap<String, String> placeholders) {
return getMessage(placeholders, true);
}

public String getMessageNoPrefix() {
return getMessage(false);
}

public String getMessageNoPrefix(String placeholder, String replacement) {
HashMap<String, String> placeholders = new HashMap<>();
placeholders.put(placeholder, replacement);
return getMessage(placeholders, false);
}

public String getMessageNoPrefix(HashMap<String, String> placeholders) {
return getMessage(placeholders, false);
}

public static String replacePlaceholders(HashMap<String, String> placeholders, String message) {
for (String placeholder : placeholders.keySet()) {
message = message.replaceAll(placeholder, placeholders.get(placeholder))
.replaceAll(placeholder.toLowerCase(), placeholders.get(placeholder));
}

return message;
}

public static List<String> replacePlaceholders(HashMap<String, String> placeholders, List<String> messageList) {
List<String> newMessageList = new ArrayList<>();

for (String message : messageList) {
for (String placeholder : placeholders.keySet()) {
newMessageList.add(message.replaceAll(placeholder, placeholders.get(placeholder))
.replaceAll(placeholder.toLowerCase(), placeholders.get(placeholder)));
}
}

return newMessageList;
}

// check if message is blank
// if message is blank return true
// if message is not blank return false
public boolean isBlank() {
return getMessage(false).equals("");
}

private String getMessage(boolean prefix) {
return getMessage(new HashMap<>(), prefix);
}

private String getMessage(HashMap<String, String> placeholders, boolean prefix) {
String message;
boolean isList = isList();
boolean exists = exists();

if (isList) {
if (exists) {
message = methods.color(convertList(Files.MESSAGES.getFile().getStringList("Messages." + path)));
} else {
message = methods.color(convertList(getDefaultListMessage()));
}
} else {
if (exists) {
message = methods.color(Files.MESSAGES.getFile().getString("Messages." + path));
} else {
message = methods.color(getDefaultMessage());
}
}

for (String placeholder : placeholders.keySet()) {
message = message.replaceAll(placeholder, placeholders.get(placeholder))
.replaceAll(placeholder.toLowerCase(), placeholders.get(placeholder));
}

if (isList) { // Don't want to add a prefix to a list of messages.
return methods.color(message);
} else { // If the message isn't a list.
if (prefix) { // If the message needs a prefix.
return methods.getPrefix(message);
} else { // If the message doesn't need a prefix.
return methods.color(message);
}
}
}

private boolean exists() {
return Files.MESSAGES.getFile().contains("Messages." + path);
}

private boolean isList() {
if (Files.MESSAGES.getFile().contains("Messages." + path)) {
return !Files.MESSAGES.getFile().getStringList("Messages." + path).isEmpty();
} else {
return defaultMessage == null;
}
}

private String getPath() {
return path;
}

private String getDefaultMessage() {
return defaultMessage;
}

private List<String> getDefaultListMessage() {
return defaultListMessage;
}
}
Loading

0 comments on commit f21601e

Please sign in to comment.