mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
Add ability to define cooldowns for actions (#437)
This commit is contained in:
1 parent
fb052340c0
commit
fc8d3b0098
52 files changed
+1140
-619
No files matched your search
@@ -24,24 +24,36 @@ import net.william278.huskhomes.hook.EconomyHook;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* Utility class for resolving and performing economy transactions through an {@link EconomyHook}
|
||||
* Class for validating and performing economy and cooldown transactions involving {@link Action}s
|
||||
*/
|
||||
public interface TransactionResolver {
|
||||
|
||||
/**
|
||||
* Perform an economy check on the {@link OnlineUser}; returning {@code true} if it passes the check
|
||||
* Validates whether an {@link OnlineUser} can perform an {@link Action}, in terms of whether they have sufficient
|
||||
* funds and are not on cooldown. This method will also send the user a message if they cannot perform the action.
|
||||
* <p>
|
||||
* This method will return {@code true} if the user has sufficient funds and is not on cooldown, otherwise
|
||||
* {@code false}.
|
||||
*
|
||||
* @param player the {@link OnlineUser player} to perform the check on
|
||||
* @param action the {@link EconomyHook.Action action} to perform
|
||||
* @return {@code true} if the action passes the check, {@code false} if the user has insufficient funds
|
||||
* @param action the {@link Action action} to perform
|
||||
* @return {@code true} if the action can be performed, {@code false} otherwise
|
||||
*/
|
||||
default boolean canPerformTransaction(@NotNull OnlineUser player, @NotNull EconomyHook.Action action) {
|
||||
return getPlugin().getSettings().getEconomyCost(action)
|
||||
.map(Math::abs)
|
||||
.flatMap(c -> player.hasPermission(EconomyHook.BYPASS_PERMISSION) ? Optional.empty() : Optional.of(c))
|
||||
default boolean validateTransaction(@NotNull OnlineUser player, @NotNull Action action) {
|
||||
return hasFunds(player, action) && isNotOnCooldown(player, action);
|
||||
}
|
||||
|
||||
// Validates if the user has funds to perform an action
|
||||
private boolean hasFunds(@NotNull OnlineUser player, @NotNull Action action) {
|
||||
return getPlugin().getSettings().getEconomyCost(action).map(Math::abs)
|
||||
.flatMap(c -> player.hasPermission(Action.BYPASS_ECONOMY_PERMISSION) ? Optional.empty() : Optional.of(c))
|
||||
.map(c -> getEconomyHook()
|
||||
.map(hook -> {
|
||||
if (hook.getPlayerBalance(player) < c) {
|
||||
@@ -55,20 +67,87 @@ public interface TransactionResolver {
|
||||
.orElse(true);
|
||||
}
|
||||
|
||||
// Validates if the user is on cooldown for an action
|
||||
private boolean isNotOnCooldown(@NotNull OnlineUser player, @NotNull Action action) {
|
||||
final long configCooldown = getPlugin().getSettings().getCooldown(action);
|
||||
if (configCooldown <= 0 || player.hasPermission(Action.BYPASS_COOLDOWNS_PERMISSION)) {
|
||||
return true;
|
||||
}
|
||||
return getPlugin().getDatabase().getCooldown(action, player)
|
||||
.map(cooldownEnds -> {
|
||||
if (cooldownEnds.isAfter(Instant.now())) {
|
||||
getPlugin().getLocales().getLocale("error_on_cooldown",
|
||||
formatDuration(Duration.between(Instant.now(), cooldownEnds).abs()))
|
||||
.ifPresent(player::sendMessage);
|
||||
return false;
|
||||
}
|
||||
getPlugin().getDatabase().removeCooldown(action, player);
|
||||
return true;
|
||||
})
|
||||
.orElse(true);
|
||||
}
|
||||
|
||||
// Formats a Duration object into a human-readable days/hours/minutes/seconds string (e.g. "1d 2h 3m 4s")
|
||||
@NotNull
|
||||
private String formatDuration(@NotNull Duration duration) {
|
||||
final long days = duration.toDays();
|
||||
final long hours = duration.minusDays(days).toHours();
|
||||
final long minutes = duration.minusDays(days).minusHours(hours).toMinutes();
|
||||
final long seconds = duration.minusDays(days).minusHours(hours).minusMinutes(minutes).getSeconds();
|
||||
final StringJoiner formattedDuration = new StringJoiner(" ");
|
||||
if (days > 0) {
|
||||
formattedDuration.add(days + "d");
|
||||
}
|
||||
if (hours > 0) {
|
||||
formattedDuration.add(hours + "h");
|
||||
}
|
||||
if (minutes > 0) {
|
||||
formattedDuration.add(minutes + "m");
|
||||
}
|
||||
if (seconds > 0) {
|
||||
formattedDuration.add(seconds + "s");
|
||||
}
|
||||
return formattedDuration.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an economy transaction if needed, updating the player's balance
|
||||
*
|
||||
* @param player the {@link OnlineUser player} to deduct the cost from if needed
|
||||
* @param action the {@link EconomyHook.Action action} to deduct the cost from if needed
|
||||
* @param action the {@link Action action} to deduct the cost from if needed
|
||||
*/
|
||||
default void performTransaction(@NotNull OnlineUser player, @NotNull EconomyHook.Action action) {
|
||||
getPlugin().getSettings().getEconomyCost(action)
|
||||
.map(Math::abs)
|
||||
.flatMap(c -> player.hasPermission(EconomyHook.BYPASS_PERMISSION) ? Optional.empty() : Optional.of(c))
|
||||
.ifPresent(cost -> getEconomyHook().ifPresent(hook -> {
|
||||
default void performTransaction(@NotNull OnlineUser player, @NotNull Action action) {
|
||||
getEconomyHook().ifPresent(hook -> getPlugin().getSettings()
|
||||
.getEconomyCost(action).map(Math::abs)
|
||||
.flatMap(c -> player.hasPermission(Action.BYPASS_ECONOMY_PERMISSION) ? Optional.empty() : Optional.of(c))
|
||||
.ifPresent(cost -> {
|
||||
hook.changePlayerBalance(player, -cost);
|
||||
hook.notifyDeducted(player, getPlugin(), action);
|
||||
}));
|
||||
|
||||
final long configCooldown = getPlugin().getSettings().getCooldown(action);
|
||||
getPlugin().log(Level.INFO, "Setting cooldown for " + player.getUsername() + " to " + configCooldown + " seconds");
|
||||
if (configCooldown > 0 && !player.hasPermission(Action.BYPASS_COOLDOWNS_PERMISSION)) {
|
||||
getPlugin().getDatabase().setCooldown(action, player, Instant.now().plusSeconds(configCooldown));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #validateTransaction(OnlineUser, Action)} instead
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "4.4", forRemoval = true)
|
||||
default boolean canPerformTransaction(@NotNull OnlineUser player, @NotNull EconomyHook.Action action) {
|
||||
return this.validateTransaction(player, action.getTransactionAction());
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #performTransaction(OnlineUser, Action)} instead
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "4.4", forRemoval = true)
|
||||
default void performTransaction(@NotNull OnlineUser player, @NotNull EconomyHook.Action action) {
|
||||
this.performTransaction(player, action.getTransactionAction());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,4 +163,65 @@ public interface TransactionResolver {
|
||||
@NotNull
|
||||
HuskHomes getPlugin();
|
||||
|
||||
/**
|
||||
* Represents actions that can be the subject of a transaction
|
||||
*/
|
||||
enum Action {
|
||||
|
||||
/*
|
||||
* Home and public home slots
|
||||
*/
|
||||
ADDITIONAL_HOME_SLOT(100d, 0),
|
||||
MAKE_HOME_PUBLIC(50d, 0),
|
||||
BACK_COMMAND,
|
||||
|
||||
/*
|
||||
* Teleportation actions
|
||||
*/
|
||||
RANDOM_TELEPORT(25d, 600),
|
||||
HOME_TELEPORT,
|
||||
PUBLIC_HOME_TELEPORT,
|
||||
WARP_TELEPORT,
|
||||
SPAWN_TELEPORT,
|
||||
|
||||
/*
|
||||
* Teleport request actions
|
||||
*/
|
||||
SEND_TELEPORT_REQUEST,
|
||||
ACCEPT_TELEPORT_REQUEST;
|
||||
|
||||
public static final String BYPASS_ECONOMY_PERMISSION = "huskhomes.bypass_economy_checks";
|
||||
public static final String BYPASS_COOLDOWNS_PERMISSION = "huskhomes.bypass_cooldowns";
|
||||
|
||||
private final double defaultCost;
|
||||
private final int defaultCooldown;
|
||||
|
||||
Action(double defaultCost, int defaultCooldown) {
|
||||
this.defaultCost = defaultCost;
|
||||
this.defaultCooldown = defaultCooldown;
|
||||
}
|
||||
|
||||
Action() {
|
||||
this(0d, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an action with a default cost
|
||||
*
|
||||
* @return the default cost
|
||||
*/
|
||||
public double getDefaultCost() {
|
||||
return defaultCost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default cooldown for this action (in seconds)
|
||||
*
|
||||
* @return the default cooldown in seconds
|
||||
*/
|
||||
public int getDefaultCooldown() {
|
||||
return defaultCooldown;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user