Untitled

Anonymous
Plain Text
08/22/2025 8:20 PM
5.0 KB
21
Indexable
Never
package fr.lostera.eco.module.impl.commands.handler;

import fr.lostera.eco.module.ModuleCommand;
import fr.lostera.eco.module.ModuleManager;
import fr.lostera.eco.module.impl.commands.CommandsModule;
import fr.lostera.eco.module.impl.core.currency.Currency;
import fr.lostera.eco.module.impl.legacy.LegacyModule;
import fr.lostera.eco.util.ComponentUtil;
import fr.lostera.eco.util.NumberAbbreviator;
import fr.lostera.eco.util.logger.BaseLogger;
import net.kyori.adventure.text.Component;
import org.bukkit.OfflinePlayer;
import studio.mevera.imperat.BukkitSource;
import studio.mevera.imperat.annotations.*;

import java.math.BigDecimal;

@Command({"economy", "eco"})
@Permission("lostera.economy.admin")
@Description("Admin economy commands")
public class EconomyCommand extends ModuleCommand<CommandsModule> {
    public EconomyCommand(final CommandsModule module) {
        super(module);
    }

    @Usage
    @Async
    public void defaultUsage(final BukkitSource source) {
        for(final Component component : module.getConfig().getEconomyCommandUsage()) {
            source.reply(component);
        }
    }

    @SubCommand("set")
    @Permission("lostera.economy.admin.set")
    public static class SetCommands {
        @Dependency
        private ModuleManager moduleManager;

        @Usage
        public void setCurrency(final BukkitSource source, final OfflinePlayer player, @Optional Currency currency, final BigDecimal amount) {
            if (player == null) {
                return;
            }

            final CommandsModule commandsModule = moduleManager.getModule(CommandsModule.class);
            if (currency == null) {
                currency = commandsModule.getCoreModule().getConfig().getDefaultCurrency();
            }

            commandsModule.getCoreModule().getTransactionManager().setBalance(player.getUniqueId(), currency, amount);

            source.reply(ComponentUtil.deserialize(commandsModule.getConfig().getCurrencySetMessage(), player,
                    "%target%", player.getName(),
                    "%currency%", currency.getName(),
                    "%amount%", NumberAbbreviator.format(amount)));
        }
    }

    // TODO: Extract duplicate code
    @SubCommand("add")
    @Permission("lostera.economy.admin.add")
    public static class AddCommand {
        @Dependency
        private ModuleManager moduleManager;

        @Usage
        @Async
        public void addCurrency(final BukkitSource source, final OfflinePlayer player, @Optional Currency currency, final BigDecimal amount, @Switch("boosted") final boolean boosted, @Switch("silent") final boolean silent) {
            if (player == null) {
                return;
            }

            final CommandsModule commandsModule = moduleManager.getModule(CommandsModule.class);
            if (currency == null) {
                BaseLogger.info("Currency is null");
                currency = commandsModule.getCoreModule().getConfig().getDefaultCurrency();
            } else {
                BaseLogger.info("Currency is " + currency.getName());
            }

            BigDecimal boostAmount = null;
            if (boosted) {
                final LegacyModule legacyModule = moduleManager.getModule(LegacyModule.class);
                boostAmount = legacyModule != null ? legacyModule.getBoosterManager().getBoostAmount(currency, player.getUniqueId()) : null;
            }

            commandsModule.getCoreModule().getTransactionManager().addBalance(player.getUniqueId(), currency, amount, boostAmount);

            if(!silent) {
                source.reply(ComponentUtil.deserialize(commandsModule.getConfig().getCurrencyAddMessage(), player,
                        "%target%", player.getName(),
                        "%currency%", currency.getName(),
                        "%amount%", NumberAbbreviator.format(amount)));
            }
        }
    }

    @SubCommand("remove")
    @Permission("lostera.economy.admin.remove")
    public static class RemoveCommand {
        @Dependency
        private ModuleManager moduleManager;

        @Usage
        @Async
        public void removeCurrency(final BukkitSource source, final OfflinePlayer player, @Optional Currency currency, final BigDecimal amount) {
            if (player == null) {
                return;
            }

            final CommandsModule commandsModule = moduleManager.getModule(CommandsModule.class);
            if (currency == null) {
                currency = commandsModule.getCoreModule().getConfig().getDefaultCurrency();
            }

            commandsModule.getCoreModule().getTransactionManager().removeBalance(player.getUniqueId(), currency, amount);

            source.reply(ComponentUtil.deserialize(commandsModule.getConfig().getCurrencyRemoveMessage(), player,
                    "%target%", player.getName(),
                    "%currency%", currency.getName(),
                    "%amount%", NumberAbbreviator.format(amount)));
        }
    }
}
Editor is loading...
Leave a Comment