Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public enum Permission {
ESSENTIALS_MONEY_OTHER,
ESSENTIALS_ECO_SET,
ESSENTIALS_ECO_RESET,
ESSENTIALS_ECO_RESET_ALL,
ESSENTIALS_ECO_SHOW,
ESSENTIALS_PAY,
ESSENTIALS_TP_HERE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ public interface EconomyManager extends Module {
*/
boolean set(UUID uniqueId, Economy economy, BigDecimal amount, String reason);

/**
* Resets every player's balance for the specified economy to zero.
*
* @param economy the economy to reset.
* @param reason the reason associated with the reset operation.
*/
void resetAll(Economy economy, String reason);

/**
* Gets all available economies.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public enum Message {
DESCRIPTION_ECO_GIVE_RANDOM("Gives the specified player a random amount of money"),
DESCRIPTION_ECO_GIVE_ALL("Gives for all players the specified amount of money"),
DESCRIPTION_ECO_RESET("Resets the specified player's balance to the server's starting balance"),
DESCRIPTION_ECO_RESET_ALL("Resets every player's balance for the specified economy"),
DESCRIPTION_ECO_SHOW("Show player money"),
DESCRIPTION_PAY("Pays another player from your balance"),
DESCRIPTION_PAY_TOGGLE("Activate or not the receipt of money"),
Expand Down Expand Up @@ -424,6 +425,8 @@ public enum Message {
// Economy

COMMAND_ECONOMY_NOT_FOUND("<error> Can’t find a economy with the name &f%name%<error>."),
COMMAND_ECONOMY_RESET_ALL_CONFIRM("<error>You are about to reset every balance for &f%economy%<error>. Run the command again within &f%seconds% seconds<error> to confirm."),
COMMAND_ECONOMY_RESET_ALL_SUCCESS("#99E0FFYou reset all balances for &f%economy%#99E0FF."),
COMMAND_ECONOMY_GIVE_ALL_SENDER("#99E0FFYou just gave &f%economyFormat% #99E0FFto the online players."),
COMMAND_ECONOMY_GIVE_SENDER("#99E0FFYou just gave &f%economyFormat% #99E0FFto the player &7%player%#99E0FF."),
COMMAND_ECONOMY_GIVE_RECEIVER("#99E0FFYou have just received &f%economyFormat%."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public interface IStorage {
*/
void updateEconomy(UUID uniqueId, Economy economy, BigDecimal bigDecimal);

/**
* Resets the specified economy for every stored player to the given amount.
*
* @param economy the economy to reset
* @param amount the amount to apply to every player
*/
void resetEconomy(Economy economy, BigDecimal amount);

/**
* Deletes a user's cooldown.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public CommandEconomy(EssentialsPlugin plugin) {
this.addSubCommand(new CommandEconomyTake(plugin));
this.addSubCommand(new CommandEconomySet(plugin));
this.addSubCommand(new CommandEconomyReset(plugin));
this.addSubCommand(new CommandEconomyResetAll(plugin));
this.addSubCommand(new CommandEconomyShow(plugin));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fr.maxlego08.essentials.commands.commands.economy;

import fr.maxlego08.essentials.api.EssentialsPlugin;
import fr.maxlego08.essentials.api.commands.CommandResultType;
import fr.maxlego08.essentials.api.commands.Permission;
import fr.maxlego08.essentials.api.economy.Economy;
import fr.maxlego08.essentials.api.economy.EconomyManager;
import fr.maxlego08.essentials.api.messages.Message;
import fr.maxlego08.essentials.module.modules.economy.EconomyModule;
import fr.maxlego08.essentials.zutils.utils.commands.VCommand;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class CommandEconomyResetAll extends VCommand {

private static final long CONFIRMATION_DURATION = TimeUnit.SECONDS.toMillis(30);
private static final Map<String, Confirmation> CONFIRMATIONS = new HashMap<>();

public CommandEconomyResetAll(EssentialsPlugin plugin) {
super(plugin);
this.setModule(EconomyModule.class);
this.setPermission(Permission.ESSENTIALS_ECO_RESET_ALL);
this.setDescription(Message.DESCRIPTION_ECO_RESET_ALL);
this.addSubCommand("reset-all");
this.addRequireArg("economy", (sender, args) -> plugin.getEconomyManager().getEconomies().stream().map(Economy::getName).toList());
}

@Override
protected CommandResultType perform(EssentialsPlugin plugin) {

String economyName = this.argAsString(0);
EconomyManager economyManager = plugin.getEconomyManager();
Optional<Economy> optional = economyManager.getEconomy(economyName);
if (optional.isEmpty()) {
message(sender, Message.COMMAND_ECONOMY_NOT_FOUND, "%name%", economyName);
return CommandResultType.DEFAULT;
}

Economy economy = optional.get();
long now = System.currentTimeMillis();
String key = getConfirmationKey(this.sender);
Confirmation confirmation = CONFIRMATIONS.get(key);

if (confirmation == null || confirmation.isExpired(now) || !confirmation.isSameEconomy(economy.getName())) {
CONFIRMATIONS.put(key, new Confirmation(economy.getName(), now + CONFIRMATION_DURATION));
message(sender, Message.COMMAND_ECONOMY_RESET_ALL_CONFIRM, "%economy%", economy.getDisplayName(), "%seconds%", (int) TimeUnit.MILLISECONDS.toSeconds(CONFIRMATION_DURATION));
return CommandResultType.DEFAULT;
}

CONFIRMATIONS.remove(key);

String reason = this.getMessage(economyManager.getCommandResetReason(), "%sender%", sender.getName());
economyManager.resetAll(economy, reason);

message(sender, Message.COMMAND_ECONOMY_RESET_ALL_SUCCESS, "%economy%", economy.getDisplayName());

return CommandResultType.SUCCESS;
}

private String getConfirmationKey(CommandSender sender) {
if (sender instanceof Player player) {
return player.getUniqueId().toString();
}
return sender.getName();
}

private record Confirmation(String economyName, long expiresAt) {

private boolean isExpired(long now) {
return now > this.expiresAt;
}

private boolean isSameEconomy(String name) {
return this.economyName.equalsIgnoreCase(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,24 @@ public boolean set(UUID uniqueId, Economy economy, BigDecimal amount, String rea
return true;
}

@Override
public void resetAll(Economy economy, String reason) {
BigDecimal amount = BigDecimal.ZERO;

this.plugin.getServer().getOnlinePlayers().forEach(onlinePlayer -> {
User user = this.plugin.getUser(onlinePlayer.getUniqueId());
if (user != null) {
user.set(economy, amount, reason);
}
});

this.offlinePlayers.values().forEach(offlineEconomy -> offlineEconomy.set(economy.getName(), amount));

this.plugin.getStorageManager().getStorage().resetEconomy(economy, amount);

refreshBaltop(economy);
}

@Override
public Collection<Economy> getEconomies() {
return Collections.unmodifiableCollection(this.economies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public void upsert(UUID uuid, Economy economy, BigDecimal bigDecimal) {
});
}

public void reset(Economy economy, BigDecimal amount) {
update(table -> {
table.decimal("amount", amount);
table.where("economy_name", economy.getName());
});
}

public List<EconomyDTO> select(UUID uuid) {
return select(EconomyDTO.class, table -> table.where("unique_id", uuid));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public void updateEconomy(UUID uniqueId, Economy economy, BigDecimal bigDecimal)
this.saveFileAsync(uniqueId);
}

@Override
public void resetEconomy(Economy economy, BigDecimal amount) {
throw new NotImplementedException("resetEconomy is not implemented, use MYSQL storage");
Comment on lines +149 to +151

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Gracefully handle reset-all when JSON storage lacks bulk reset

EconomyManager.resetAll always invokes IStorage.resetEconomy, but the JSON storage implementation added here just throws NotImplementedException. On servers running the default JSON storage backend, /eco reset-all will therefore blow up after setting online players to zero, leaving offline balances untouched and surfacing a stack trace. Consider implementing the bulk reset for JSON storage or short‑circuiting the command with a user-facing error when JSON storage is active.

Useful? React with 👍 / 👎.

}

@Override
public void deleteCooldown(UUID uniqueId, String key) {
this.saveFileAsync(uniqueId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,17 @@ public void updateEconomy(UUID uniqueId, Economy economy, BigDecimal bigDecimal)
}
}

@Override
public void resetEconomy(Economy economy, BigDecimal amount) {
synchronized (economyUpdateQueue) {
economyUpdateQueue.values().stream()
.filter(pending -> pending.economy().equals(economy))
.forEach(pending -> pending.latestValue().set(amount));
}

async(() -> with(UserEconomyRepository.class).reset(economy, amount));
}

private void launchUpdateTask(String key, PendingEconomyUpdate pending) {
async(() -> {
ensureUserExists(pending.uniqueId());
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages/message_zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ command-seen-first-join:

# 经济
command-economy-not-found: <error> 找不到名为 &f%name%<error> 的经济系统。
command-economy-reset-all-confirm: <error>你即将重置 &f%economy%<error> 的所有余额。请在 &f%seconds% 秒内再次执行该命令以确认。
command-economy-reset-all-success: '#99E0FF你已重置 &f%economy%#99E0FF 的所有余额。'
command-economy-give-all-sender: '#99E0FF您刚刚给了 &f%economyFormat% #99E0FF给在线玩家。'
command-economy-give-sender: '#99E0FF您刚刚给了 &f%economyFormat% #99E0FF给玩家 &7%player%#99E0FF。'
command-economy-give-receiver: '#99E0FF您刚刚收到了 &f%economyFormat%。'
Expand Down Expand Up @@ -301,6 +303,7 @@ description-eco-give: 给予指定玩家指定金额
description-eco-give-all: 给予所有玩家指定金额
description-eco-give-random: 给予指定玩家随机金额的钱
description-eco-reset: 将指定玩家的余额重置为服务器的起始余额
description-eco-reset-all: 将指定经济的所有玩家余额重置
description-eco-show: 显示玩家的钱
description-pay: 从您的余额中支付给另一名玩家
description-fly: 切换飞行模式
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ command-seen-first-join:

# Economy
command-economy-not-found: "<error> Can’t find a economy with the name &f%name%<error>."
command-economy-reset-all-confirm: "<error>You are about to reset every balance for &f%economy%<error>. Run the command again within &f%seconds% seconds<error> to confirm."
command-economy-reset-all-success: "#99E0FFYou reset all balances for &f%economy%#99E0FF."
command-economy-give-all-sender: "#99E0FFYou just gave &f%economyFormat% #99E0FFto the online players."
command-economy-give-sender: "#99E0FFYou just gave &f%economyFormat% #99E0FFto the player &7%player%#99E0FF."
command-economy-give-receiver: "#99E0FFYou have just received &f%economyFormat%."
Expand Down Expand Up @@ -306,6 +308,7 @@ description-eco-give: "Gives the specified player the specified amount of money"
description-eco-give-random: "Gives the specified player a random amount of money"
description-eco-give-all: "Gives for all players the specified amount of money"
description-eco-reset: "Resets the specified player's balance to the server's starting balance"
description-eco-reset-all: "Resets every player's balance for the specified economy"
description-eco-show: "Show player money"
description-pay: "Pays another player from your balance"
description-fly: "Toggle flight"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages/messages_de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ command-seen-first-join:

# Wirtschaftsbefehle
command-economy-not-found: "<error> Es konnte keine Wirtschaft mit dem Namen &f%name%<error> gefunden werden."
command-economy-reset-all-confirm: "<error>Du bist dabei, alle Guthaben für &f%economy%<error> zurückzusetzen. Führe den Befehl innerhalb von &f%seconds% Sekunden<error> erneut aus, um zu bestätigen."
command-economy-reset-all-success: "#99E0FFDu hast alle Guthaben für &f%economy%#99E0FF zurückgesetzt."
command-economy-give-all-sender: "#99E0FFDu hast gerade &f%economyFormat% #99E0FFan die online Spieler verteilt."
command-economy-give-sender: "#99E0FFDu hast gerade &f%economyFormat% #99E0FFan den Spieler &7%player%#99E0FF gegeben."
command-economy-give-receiver: "#99E0FFDu hast gerade &f%economyFormat% erhalten."
Expand Down Expand Up @@ -284,6 +286,7 @@ description-eco-take: "Zieht den angegebenen Geldbetrag vom Konto eines Spielers
description-eco-give: "Gibt einem Spieler den angegebenen Geldbetrag"
description-eco-give-all: "Gibt allen Spielern den angegebenen Geldbetrag"
description-eco-reset: "Setzt das Guthaben eines Spielers auf den Startbetrag des Servers zurück"
description-eco-reset-all: "Setzt das Guthaben aller Spieler für die angegebene Ökonomie zurück"
description-eco-show: "Geld eines Spielers anzeigen"
description-pay: "Einen anderen Spieler von deinem Guthaben bezahlen"
description-fly: "Flugmodus ein- oder ausschalten"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages/messages_es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ command-seen-first-join:
type: WITHOUT_PREFIX
message: "#99E0FFPrimera conexión&8: &f%created_at%"
command-economy-not-found: "<error> No se puede encontrar una economía con el nombre &f%name%<error>."
command-economy-reset-all-confirm: "<error>Estás a punto de restablecer todos los saldos de &f%economy%<error>. Ejecuta el comando de nuevo en &f%seconds% segundos<error> para confirmar."
command-economy-reset-all-success: "#99E0FFHas restablecido todos los saldos de &f%economy%#99E0FF."
command-economy-give-all-sender: "#99E0FFAcabas de dar &f%economyFormat% #99E0FFa todos los jugadores en línea."
command-economy-give-sender: "#99E0FFAcabas de dar &f%economyFormat% #99E0FFal jugador &7%player%#99E0FF."
command-economy-give-receiver: "#99E0FFAcabas de recibir &f%economyFormat%."
Expand Down Expand Up @@ -266,6 +268,7 @@ description-eco-take: "Toma la cantidad especificada de dinero del jugador espec
description-eco-give: "Da la cantidad especificada de dinero al jugador especificado"
description-eco-give-all: "Da la cantidad especificada de dinero a todos los jugadores"
description-eco-reset: "Restablece el saldo del jugador especificado al saldo inicial del servidor"
description-eco-reset-all: "Restablece el saldo de todos los jugadores para la economía especificada"
description-eco-show: "Mostrar el dinero del jugador"
description-pay: "Pagar a otro jugador desde tu saldo"
description-fly: "Activar/Desactivar el vuelo"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages/messages_fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ command-seen-first-join:

# Économie
command-economy-not-found: "<error>Impossible de trouver une économie avec le nom &f%name%<error>."
command-economy-reset-all-confirm: "<error>Vous êtes sur le point de réinitialiser tous les soldes pour &f%economy%<error>. Exécutez à nouveau la commande dans &f%seconds% secondes<error> pour confirmer."
command-economy-reset-all-success: "#99E0FFVous avez réinitialisé tous les soldes pour &f%economy%#99E0FF."
command-economy-give-all-sender: "#99E0FFVous venez de donner &f%economyFormat% #99E0FFaux joueurs en ligne."
command-economy-give-sender: "#99E0FFVous venez de donner &f%economyFormat% #99E0FFau joueur &7%player%#99E0FF."
command-economy-give-receiver: "#99E0FFVous venez de recevoir &f%economyFormat%."
Expand Down Expand Up @@ -301,6 +303,7 @@ description-eco-give: "Donne au joueur spécifié le montant d'argent spécifié
description-eco-give-random: "Donne au joueur spécifié un montant aléatoire d'argent"
description-eco-give-all: "Donne à tous les joueurs le montant d'argent spécifié"
description-eco-reset: "Réinitialise le solde du joueur spécifié au solde de départ du serveur"
description-eco-reset-all: "Réinitialise le solde de tous les joueurs pour l'économie spécifiée"
description-eco-show: "Afficher l'argent du joueur"
description-pay: "Payer un autre joueur depuis votre solde"
description-fly: "Activer/désactiver le vol"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages/messages_it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ command-seen-first-join:

# Economy
command-economy-not-found: "<error> Non riesco a trovare una valuta di nome &f%name%<error>."
command-economy-reset-all-confirm: "<error>Stai per azzerare tutti i saldi per &f%economy%<error>. Esegui di nuovo il comando entro &f%seconds% secondi<error> per confermare."
command-economy-reset-all-success: "#99E0FFHai azzerato tutti i saldi per &f%economy%#99E0FF."
command-economy-give-all-sender: "#99E0FFHai appena dato &f%economyFormat% #99E0FFai giocatori online."
command-economy-give-sender: "#99E0FFHai appena dato &f%economyFormat% #99E0FFa &7%player%#99E0FF."
command-economy-give-receiver: "#99E0FFHai appena ricevuto &f%economyFormat%."
Expand Down Expand Up @@ -262,6 +264,7 @@ description-eco-take: "Prendi dei soldi da un giocatore"
description-eco-give: "Dai a un giocatore dei soldi"
description-eco-give-all: "Dai a tutti i giocatori dei soldi"
description-eco-reset: "Azzera il saldo del giocatore al saldo iniziale del server"
description-eco-reset-all: "Azzera il saldo di tutti i giocatori per l'economia specificata"
description-eco-show: "Mostra il saldo di un giocatore"
description-pay: "Paga un altro giocatore attraverso l'utilizzo del tuo saldo"
description-fly: "Abilita/Disabilita il volo"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages/messages_nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ command-seen-first-join:

# Economie
command-economy-not-found: "<error>Kan geen economie vinden met de naam &f%name%<error>."
command-economy-reset-all-confirm: "<error>Je staat op het punt alle saldi voor &f%economy%<error> te resetten. Voer het commando opnieuw uit binnen &f%seconds% seconden<error> om te bevestigen."
command-economy-reset-all-success: "#99E0FFJe hebt alle saldi voor &f%economy%#99E0FF gereset."
command-economy-give-all-sender: "#99E0FFJe hebt zojuist &f%economyFormat% #99E0FFgegeven aan de online spelers."
command-economy-give-sender: "#99E0FFJe hebt zojuist &f%economyFormat% #99E0FFgegeven aan de speler &7%player%#99E0FF."
command-economy-give-receiver: "#99E0FFJe hebt zojuist &f%economyFormat% ontvangen."
Expand Down Expand Up @@ -306,6 +308,7 @@ description-eco-give: "Geef een opgegeven speler een opgegeven bedrag geld"
description-eco-give-random: "Geef een opgegeven speler een willekeurige hoeveelheid geld"
description-eco-give-all: "Geef alle spelers een opgegeven bedrag geld"
description-eco-reset: "Reset het saldo van een opgegeven speler naar het startbedrag van de server"
description-eco-reset-all: "Reset het saldo van alle spelers voor de opgegeven economie"
description-eco-show: "Toon het geld van een speler"
description-pay: "Betaal een andere speler vanuit je saldo"
description-fly: "Schakel vliegen in of uit"
Expand Down