From b5d1c210f1ff8f21f510201a46abfd0bddaaf713 Mon Sep 17 00:00:00 2001 From: Qbiter Date: Sat, 11 Oct 2025 19:44:34 +0200 Subject: [PATCH 01/17] Implement basic of paycheck management system with admin commands and configuration --- eternaleconomy-core/build.gradle.kts | 1 + .../economy/EconomyBukkitPlugin.java | 14 ++- .../economy/EconomyPermissionConstant.java | 1 + .../command/impl/admin/AdminItemCommand.java | 24 +++++ .../economy/config/ConfigService.java | 3 +- .../config/implementation/PluginConfig.java | 15 +++ .../messages/MessageConfig.java | 1 + .../messages/MessagePaycheckSubSection.java | 18 ++++ .../economy/paycheck/PaycheckCommand.java | 47 ++++++++++ .../economy/paycheck/PaycheckManager.java | 93 +++++++++++++++++++ .../economy/paycheck/PaycheckTagger.java | 28 ++++++ 11 files changed, 239 insertions(+), 6 deletions(-) create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java diff --git a/eternaleconomy-core/build.gradle.kts b/eternaleconomy-core/build.gradle.kts index 09f83224..7e503dc7 100644 --- a/eternaleconomy-core/build.gradle.kts +++ b/eternaleconomy-core/build.gradle.kts @@ -49,6 +49,7 @@ dependencies { // okaeri configs implementation("eu.okaeri:okaeri-configs-yaml-snakeyaml:${Versions.OKAERI_CONFIGS}") implementation("eu.okaeri:okaeri-configs-serdes-commons:${Versions.OKAERI_CONFIGS}") + implementation("eu.okaeri:okaeri-configs-serdes-bukkit:${Versions.OKAERI_CONFIGS}") compileOnly("me.clip:placeholderapi:${Versions.PLACEHOLDER_API}") diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 705ee90e..72b7358b 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -11,11 +11,7 @@ import com.eternalcode.economy.account.database.AccountRepository; import com.eternalcode.economy.account.database.AccountRepositoryImpl; import com.eternalcode.economy.bridge.BridgeManager; -import com.eternalcode.economy.command.impl.admin.AdminAddCommand; -import com.eternalcode.economy.command.impl.admin.AdminBalanceCommand; -import com.eternalcode.economy.command.impl.admin.AdminRemoveCommand; -import com.eternalcode.economy.command.impl.admin.AdminResetCommand; -import com.eternalcode.economy.command.impl.admin.AdminSetCommand; +import com.eternalcode.economy.command.impl.admin.*; import com.eternalcode.economy.command.argument.AccountArgument; import com.eternalcode.economy.command.context.AccountContext; import com.eternalcode.economy.command.cooldown.CommandCooldownEditor; @@ -38,6 +34,9 @@ import com.eternalcode.economy.multification.NoticeBroadcastHandler; import com.eternalcode.economy.multification.NoticeHandler; import com.eternalcode.economy.multification.NoticeService; +import com.eternalcode.economy.paycheck.PaycheckCommand; +import com.eternalcode.economy.paycheck.PaycheckManager; +import com.eternalcode.economy.paycheck.PaycheckTagger; import com.eternalcode.economy.vault.VaultEconomyProvider; import com.eternalcode.multification.notice.Notice; import com.eternalcode.multification.notice.NoticeBroadcast; @@ -101,6 +100,9 @@ public void onEnable() { DecimalFormatter decimalFormatter = new DecimalFormatterImpl(pluginConfig); AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); + PaycheckTagger paycheckTagger = new PaycheckTagger(this); + PaycheckManager paycheckManager = new PaycheckManager(noticeService, pluginConfig, paycheckTagger); + VaultEconomyProvider vaultEconomyProvider = new VaultEconomyProvider(this, decimalFormatter, accountPaymentService, accountManager); server.getServicesManager().register(Economy.class, vaultEconomyProvider, this, ServicePriority.Highest); @@ -131,6 +133,8 @@ public void onEnable() { new AdminSetCommand(accountPaymentService, decimalFormatter, noticeService), new AdminResetCommand(accountPaymentService, noticeService), new AdminBalanceCommand(noticeService, decimalFormatter), + new AdminItemCommand(paycheckManager), + new PaycheckCommand(paycheckManager, noticeService), new MoneyBalanceCommand(noticeService, decimalFormatter), new MoneyTransferCommand(accountPaymentService, decimalFormatter, noticeService, pluginConfig), new EconomyReloadCommand(configService, noticeService), diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java index f8e3b747..7857def0 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java @@ -8,6 +8,7 @@ public class EconomyPermissionConstant { public static final String ADMIN_REMOVE_PERMISSION = "eternaleconomy.admin.remove"; public static final String ADMIN_SET_PERMISSION = "eternaleconomy.admin.set"; public static final String ADMIN_RELOAD_PERMISSION = "eternaleconomy.admin.reload"; + public static final String ADMIN_ITEM_PERMISSION = "eternaleconomy.admin.item"; public static final String PLAYER_BALANCE_PERMISSION = "eternaleconomy.player.balance"; public static final String PLAYER_BALANCE_OTHER_PERMISSION = "eternaleconomy.player.balance.other"; diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java new file mode 100644 index 00000000..5c42757b --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java @@ -0,0 +1,24 @@ +package com.eternalcode.economy.command.impl.admin; + +import com.eternalcode.economy.EconomyPermissionConstant; +import com.eternalcode.economy.paycheck.PaycheckManager; +import dev.rollczi.litecommands.annotations.command.Command; +import dev.rollczi.litecommands.annotations.context.Context; +import dev.rollczi.litecommands.annotations.execute.Execute; +import dev.rollczi.litecommands.annotations.permission.Permission; +import org.bukkit.entity.Player; + +@Command(name = "economy item") +@Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) +public class AdminItemCommand { + private final PaycheckManager paycheckManager; + + public AdminItemCommand(PaycheckManager paycheckManager) { + this.paycheckManager = paycheckManager; + } + + @Execute + void execute(@Context Player player) { + paycheckManager.setItem(player); + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/ConfigService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/ConfigService.java index 640df63c..3efd7f7e 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/ConfigService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/ConfigService.java @@ -7,6 +7,7 @@ import eu.okaeri.configs.ConfigManager; import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.serdes.commons.SerdesCommons; +import eu.okaeri.configs.yaml.bukkit.serdes.SerdesBukkit; import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer; import java.io.File; import java.util.HashSet; @@ -30,7 +31,7 @@ public T create(Class config, File file) { NoticeResolverRegistry noticeRegistry = NoticeResolverDefaults.createRegistry() .registerResolver(new SoundBukkitResolver()); configFile - .withConfigurer(yamlConfigurer, new SerdesCommons()) + .withConfigurer(yamlConfigurer, new SerdesCommons(), new SerdesBukkit()) .withConfigurer(yamlConfigurer, new MultificationSerdesPack(noticeRegistry)) .withBindFile(file) .withRemoveOrphans(true) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java index 18afcf4c..af7d9e61 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java @@ -4,6 +4,9 @@ import com.eternalcode.economy.format.DecimalUnit; import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + import java.math.BigDecimal; import java.util.Arrays; import java.util.List; @@ -29,6 +32,9 @@ public class PluginConfig extends OkaeriConfig { @Comment("Should leaderboard command show player's position in the leaderboard") public boolean showLeaderboardPosition = true; + @Comment("Currency item settings") + public CurrencyItem currencyItem = new CurrencyItem(); + public static class Units extends OkaeriConfig { public List format = Arrays.asList( @@ -40,4 +46,13 @@ public static class Units extends OkaeriConfig { new DecimalUnit(1_000_000_000_000_000_000L, 'e') ); } + + public static class CurrencyItem extends OkaeriConfig { + @Comment({"Name of the item", + "{VALUE} - value of the check",}) + public String name = "Check worth {VALUE}$"; + + @Comment("Item") + public ItemStack item = new ItemStack(Material.PAPER); + } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java index b44fdd85..99d2f009 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java @@ -23,4 +23,5 @@ public class MessageConfig extends OkaeriConfig { public MessageAdminSubSection admin = new MessageAdminSubSection(); public MessagesPlayerSubSection player = new MessagesPlayerSubSection(); + public MessagePaycheckSubSection paycheck = new MessagePaycheckSubSection(); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java new file mode 100644 index 00000000..eed1cf96 --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java @@ -0,0 +1,18 @@ +package com.eternalcode.economy.config.implementation.messages; + +import com.eternalcode.multification.notice.Notice; +import eu.okaeri.configs.OkaeriConfig; + +public class MessagePaycheckSubSection extends OkaeriConfig { + public Notice noItem = Notice.chat( + "You must hold item to create paycheck!" + ); + + public Notice setItem = Notice.chat( + "You have set paycheck item to {ITEM}!" + ); + + public Notice withdraw = Notice.chat( + "You have withdrawn your paycheck of {VALUE}!" + ); +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java new file mode 100644 index 00000000..ea29107e --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java @@ -0,0 +1,47 @@ +package com.eternalcode.economy.paycheck; + +import com.eternalcode.economy.EconomyPermissionConstant; +import com.eternalcode.economy.account.Account; +import com.eternalcode.economy.multification.NoticeService; +import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.command.Command; +import dev.rollczi.litecommands.annotations.context.Context; +import dev.rollczi.litecommands.annotations.execute.Execute; +import dev.rollczi.litecommands.annotations.permission.Permission; +import jakarta.validation.constraints.Positive; +import org.bukkit.entity.Player; + +import java.math.BigDecimal; + +@Command(name = "withdraw", aliases = {"paycheck", "check"}) +@Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) +public class PaycheckCommand { + private final PaycheckManager paycheckManager; + private final NoticeService noticeService; + + public PaycheckCommand( + PaycheckManager paycheckManager, + NoticeService noticeService + ) { + this.paycheckManager = paycheckManager; + this.noticeService = noticeService; + } + + @Execute + void execute(@Context Account account, @Arg @Positive BigDecimal value) { + BigDecimal balance = account.balance(); + BigDecimal subtract = balance.subtract(value); + + if(subtract.compareTo(BigDecimal.ZERO) < 0) { + noticeService.create() + .notice(messageConfig -> messageConfig.player.insufficientBalance) + .placeholder("{MISSING_BALANCE}", subtract.toString()) + .player(account.uuid()) + .send(); + + return; + } + + paycheckManager.givePaycheck(account.uuid(), value); + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java new file mode 100644 index 00000000..16d60d15 --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java @@ -0,0 +1,93 @@ +package com.eternalcode.economy.paycheck; + +import com.eternalcode.economy.config.implementation.PluginConfig; +import com.eternalcode.economy.multification.NoticeService; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.math.BigDecimal; +import java.util.Objects; +import java.util.UUID; + +public class PaycheckManager { + private final NoticeService noticeService; + private final PluginConfig config; + private final PaycheckTagger paycheckTagger; + private final MiniMessage mm; + + public PaycheckManager( + NoticeService noticeService, + PluginConfig pluginConfig, + PaycheckTagger paycheckTagger + ) { + this.noticeService = noticeService; + this.config = pluginConfig; + this.paycheckTagger = paycheckTagger; + this.mm = MiniMessage.miniMessage(); + } + + public void setItem(Player player) { + ItemStack item = player.getInventory().getItemInMainHand(); + + if(item.getType() == Material.AIR) { + noticeService.create() + .notice(messageConfig -> messageConfig.paycheck.noItem) + .player(player.getUniqueId()) + .send(); + + return; + } + + this.config.currencyItem.item = item; + + + String displayName = item.getItemMeta().getDisplayName(); + if(displayName.isEmpty()) { + this.config.currencyItem.name = item.getType().name(); + } else { + this.config.currencyItem.name = displayName; + } + + this.config.save(); + + noticeService.create() + .notice(messageConfig -> messageConfig.paycheck.setItem) + .placeholder("{ITEM}", this.config.currencyItem.name) + .player(player.getUniqueId()) + .send(); + } + + public void givePaycheck(UUID uuid, BigDecimal value) { + Player player = Bukkit.getPlayer(uuid); + if(player == null) { + return; + } + + ItemStack item = this.config.currencyItem.item; + ItemMeta meta = Objects.requireNonNull(item.getItemMeta()); + + String displayName = this.config.currencyItem.name + .replace("{VALUE}", value.toString()); + + Component component = mm.deserialize(displayName); + String legacyName = LegacyComponentSerializer.legacySection().serialize(component); + + meta.setDisplayName(legacyName); + item.setItemMeta(meta); + + item = paycheckTagger.tagItem(item, value); + player.getInventory().addItem(item); + + noticeService.create() + .notice(messageConfig -> messageConfig.paycheck.withdraw) + .placeholder("{VALUE}", value.toString()) + .player(player.getUniqueId()) + .send(); + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java new file mode 100644 index 00000000..0bf4a5fe --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java @@ -0,0 +1,28 @@ +package com.eternalcode.economy.paycheck; + +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.plugin.Plugin; + +import java.math.BigDecimal; + +public class PaycheckTagger { + private final NamespacedKey amountKey; + + public PaycheckTagger(Plugin plugin) { + this.amountKey = new NamespacedKey(plugin, "paycheck"); + } + + public ItemStack tagItem(ItemStack item, BigDecimal amount) { + ItemStack taggedItem = item.clone(); + ItemMeta meta = taggedItem.getItemMeta(); + + if(meta != null) { + meta.getPersistentDataContainer().set(amountKey, PersistentDataType.DOUBLE, amount.doubleValue()); + } + + return taggedItem; + } +} From 2ec394518de4bfbc9c77415b741f2c971bcb04e0 Mon Sep 17 00:00:00 2001 From: Qbiter Date: Sun, 12 Oct 2025 18:05:20 +0200 Subject: [PATCH 02/17] Enhance paycheck system: add price argument resolver, improve paycheck command, and implement paycheck listener --- .../economy/EconomyBukkitPlugin.java | 28 +++--- .../argument/PriceArgumentResolver.java | 73 +++++++++++++++ .../messages/MessageConfig.java | 4 + .../messages/MessagePaycheckSubSection.java | 14 ++- .../messages/MessagesPlayerSubSection.java | 2 +- .../economy/paycheck/PaycheckCommand.java | 15 ++-- .../economy/paycheck/PaycheckListener.java | 51 +++++++++++ .../economy/paycheck/PaycheckManager.java | 89 +++++++++++++++---- .../economy/paycheck/PaycheckTagger.java | 19 +++- 9 files changed, 259 insertions(+), 36 deletions(-) create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckListener.java diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 72b7358b..1bcd9089 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -11,30 +11,32 @@ import com.eternalcode.economy.account.database.AccountRepository; import com.eternalcode.economy.account.database.AccountRepositoryImpl; import com.eternalcode.economy.bridge.BridgeManager; -import com.eternalcode.economy.command.impl.admin.*; import com.eternalcode.economy.command.argument.AccountArgument; +import com.eternalcode.economy.command.argument.PriceArgumentResolver; import com.eternalcode.economy.command.context.AccountContext; import com.eternalcode.economy.command.cooldown.CommandCooldownEditor; import com.eternalcode.economy.command.cooldown.CommandCooldownMessage; import com.eternalcode.economy.command.handler.InvalidUsageHandlerImpl; import com.eternalcode.economy.command.handler.MissingPermissionHandlerImpl; -import com.eternalcode.economy.command.message.InvalidBigDecimalMessage; import com.eternalcode.economy.command.impl.MoneyBalanceCommand; import com.eternalcode.economy.command.impl.MoneyTransferCommand; -import com.eternalcode.economy.database.DatabaseManager; -import com.eternalcode.economy.leaderboard.LeaderboardCommand; +import com.eternalcode.economy.command.impl.admin.*; +import com.eternalcode.economy.command.message.InvalidBigDecimalMessage; import com.eternalcode.economy.command.validator.notsender.NotSender; import com.eternalcode.economy.command.validator.notsender.NotSenderValidator; import com.eternalcode.economy.config.ConfigService; import com.eternalcode.economy.config.implementation.CommandsConfig; import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.config.implementation.messages.MessageConfig; +import com.eternalcode.economy.database.DatabaseManager; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.format.DecimalFormatterImpl; +import com.eternalcode.economy.leaderboard.LeaderboardCommand; import com.eternalcode.economy.multification.NoticeBroadcastHandler; import com.eternalcode.economy.multification.NoticeHandler; import com.eternalcode.economy.multification.NoticeService; import com.eternalcode.economy.paycheck.PaycheckCommand; +import com.eternalcode.economy.paycheck.PaycheckListener; import com.eternalcode.economy.paycheck.PaycheckManager; import com.eternalcode.economy.paycheck.PaycheckTagger; import com.eternalcode.economy.vault.VaultEconomyProvider; @@ -42,13 +44,11 @@ import com.eternalcode.multification.notice.NoticeBroadcast; import com.google.common.base.Stopwatch; import dev.rollczi.litecommands.LiteCommands; +import dev.rollczi.litecommands.argument.ArgumentKey; import dev.rollczi.litecommands.bukkit.LiteBukkitFactory; import dev.rollczi.litecommands.jakarta.LiteJakartaExtension; import dev.rollczi.litecommands.message.LiteMessages; import jakarta.validation.constraints.Positive; -import java.io.File; -import java.math.BigDecimal; -import java.time.Duration; import net.kyori.adventure.platform.AudienceProvider; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -58,6 +58,10 @@ import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; +import java.math.BigDecimal; +import java.time.Duration; + @SuppressWarnings("unused") public class EconomyBukkitPlugin extends JavaPlugin { @@ -101,7 +105,7 @@ public void onEnable() { AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); PaycheckTagger paycheckTagger = new PaycheckTagger(this); - PaycheckManager paycheckManager = new PaycheckManager(noticeService, pluginConfig, paycheckTagger); + PaycheckManager paycheckManager = new PaycheckManager(noticeService, pluginConfig, decimalFormatter, paycheckTagger, accountPaymentService, accountManager); VaultEconomyProvider vaultEconomyProvider = new VaultEconomyProvider(this, decimalFormatter, accountPaymentService, accountManager); @@ -121,7 +125,7 @@ public void onEnable() { .invalidUsage(new InvalidUsageHandlerImpl(noticeService)) .message(LiteMessages.COMMAND_COOLDOWN, new CommandCooldownMessage(noticeService, commandsConfig)) - .message(LiteMessages.INVALID_NUMBER, (invocation, amount) -> noticeService.create() + .message(LiteMessages.INVALID_NUMBER, (invocation, amount) -> noticeService.create() .notice(messageConfig.positiveNumberRequired) .placeholder("{AMOUNT}", amount) .viewer(invocation.sender())) @@ -134,7 +138,7 @@ public void onEnable() { new AdminResetCommand(accountPaymentService, noticeService), new AdminBalanceCommand(noticeService, decimalFormatter), new AdminItemCommand(paycheckManager), - new PaycheckCommand(paycheckManager, noticeService), + new PaycheckCommand(paycheckManager, noticeService, decimalFormatter), new MoneyBalanceCommand(noticeService, decimalFormatter), new MoneyTransferCommand(accountPaymentService, decimalFormatter, noticeService, pluginConfig), new EconomyReloadCommand(configService, noticeService), @@ -144,6 +148,8 @@ public void onEnable() { .context(Account.class, new AccountContext(accountManager, messageConfig)) .argument(Account.class, new AccountArgument(accountManager, noticeService, server)) + .argument(BigDecimal.class, ArgumentKey.of(PriceArgumentResolver.KEY), new PriceArgumentResolver(pluginConfig, messageConfig)) + .result(Notice.class, new NoticeHandler(noticeService)) .result(NoticeBroadcast.class, new NoticeBroadcastHandler()) @@ -151,6 +157,8 @@ public void onEnable() { server.getPluginManager().registerEvents(new AccountController(accountManager), this); + server.getPluginManager().registerEvents(new PaycheckListener(paycheckManager, paycheckTagger), this); + BridgeManager bridgeManager = new BridgeManager( this.getDescription(), accountManager, diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java new file mode 100644 index 00000000..dee7e0b1 --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java @@ -0,0 +1,73 @@ +package com.eternalcode.economy.command.argument; + +import com.eternalcode.economy.config.implementation.PluginConfig; +import com.eternalcode.economy.config.implementation.messages.MessageConfig; +import dev.rollczi.litecommands.argument.Argument; +import dev.rollczi.litecommands.argument.parser.ParseResult; +import dev.rollczi.litecommands.argument.resolver.ArgumentResolver; +import dev.rollczi.litecommands.invocation.Invocation; +import org.bukkit.command.CommandSender; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class PriceArgumentResolver extends ArgumentResolver { + public static final String KEY = "price"; + private static final Pattern PRICE_PATTERN = + Pattern.compile("^(\\d+(?:[.,]\\d+)?)([kmb])?$", Pattern.CASE_INSENSITIVE); + + private final PluginConfig config; + private final MessageConfig messages; + + private final Map multipliers; + + public PriceArgumentResolver(PluginConfig config, MessageConfig messages) { + this.config = config; + this.messages = messages; + this.multipliers = new HashMap<>(); + + config.units.format.forEach(unit -> { + this.multipliers.put(unit.getSuffix(), BigDecimal.valueOf(unit.getFactor())); + }); + } + + @Override + protected ParseResult parse( + Invocation invocation, Argument argument, String raw) { + Matcher matcher = PRICE_PATTERN.matcher(raw.toLowerCase()); + + if (!matcher.matches()) { + return ParseResult.failure(this.messages.invalidPrice); + } + + String numberPart = matcher.group(1).replace(',', '.'); + String suffix = matcher.group(2); + + try { + BigDecimal value = new BigDecimal(numberPart); + + if (suffix != null) { + BigDecimal multiplier = multipliers.get(Character.toLowerCase(suffix.charAt(0))); + + if (multiplier == null) { + return ParseResult.failure(this.messages.invalidPrice); + } + + value = value.multiply(multiplier); + } + + if (value.compareTo(BigDecimal.ZERO) <= 0) { + return ParseResult.failure(this.messages.priceNeedToBeGreaterThanZero); + } + + return ParseResult.success(value.setScale(2, RoundingMode.DOWN)); + + } catch (NumberFormatException e) { + return ParseResult.failure(this.messages.invalidPrice); + } + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java index 99d2f009..a0cc2fa3 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java @@ -21,6 +21,10 @@ public class MessageConfig extends OkaeriConfig { public Notice missingPermission = Notice.chat( "ECONOMY Missing permission: {PERMISSION}."); + public Notice invalidPrice = + Notice.chat("ECONOMY Nieprawidłowy format ceny! Użyj: 1000, 1k, 1.5k, 1m, etc."); + public Notice priceNeedToBeGreaterThanZero = Notice.chat("ECONOMY Cena musi być większa od 0!"); + public MessageAdminSubSection admin = new MessageAdminSubSection(); public MessagesPlayerSubSection player = new MessagesPlayerSubSection(); public MessagePaycheckSubSection paycheck = new MessagePaycheckSubSection(); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java index eed1cf96..7fa1ee3e 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java @@ -5,14 +5,22 @@ public class MessagePaycheckSubSection extends OkaeriConfig { public Notice noItem = Notice.chat( - "You must hold item to create paycheck!" + "ECONOMY You must hold item to create paycheck!" + ); + + public Notice noCheck = Notice.chat( + "ECONOMY You must hold an paycheck to redeem it!" ); public Notice setItem = Notice.chat( - "You have set paycheck item to {ITEM}!" + "ECONOMY You have set paycheck item to {ITEM}!" ); public Notice withdraw = Notice.chat( - "You have withdrawn your paycheck of {VALUE}!" + "ECONOMY You have withdrawn a paycheck of {VALUE}!" + ); + + public Notice redeem = Notice.chat( + "ECONOMY You have redeemed your paycheck of {VALUE}!" ); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java index 42654ec9..b1677062 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java @@ -21,7 +21,7 @@ public class MessagesPlayerSubSection extends OkaeriConfig { + " {PLAYER}'s balance is {BALANCE}."); public Notice insufficientBalance = Notice.chat("ECONOMY " + " Insufficient funds," - + " you are missing {MISSING_BALANCE}."); + + " you are missing {MISSING_BALANCE}."); public Notice transferSuccess = Notice.chat("ECONOMY Successfully transferred {AMOUNT} to " + "{PLAYER}."); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java index ea29107e..2cfd7315 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java @@ -2,14 +2,16 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; +import com.eternalcode.economy.command.argument.PriceArgumentResolver; +import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.argument.Key; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; import jakarta.validation.constraints.Positive; -import org.bukkit.entity.Player; import java.math.BigDecimal; @@ -18,24 +20,27 @@ public class PaycheckCommand { private final PaycheckManager paycheckManager; private final NoticeService noticeService; + private final DecimalFormatter decimalFormatter; public PaycheckCommand( PaycheckManager paycheckManager, - NoticeService noticeService + NoticeService noticeService, + DecimalFormatter decimalFormatter ) { this.paycheckManager = paycheckManager; this.noticeService = noticeService; + this.decimalFormatter = decimalFormatter; } @Execute - void execute(@Context Account account, @Arg @Positive BigDecimal value) { + void execute(@Context Account account, @Arg @Positive @Key(PriceArgumentResolver.KEY) BigDecimal value) { BigDecimal balance = account.balance(); BigDecimal subtract = balance.subtract(value); - if(subtract.compareTo(BigDecimal.ZERO) < 0) { + if (subtract.compareTo(BigDecimal.ZERO) < 0) { noticeService.create() .notice(messageConfig -> messageConfig.player.insufficientBalance) - .placeholder("{MISSING_BALANCE}", subtract.toString()) + .placeholder("{MISSING_BALANCE}", decimalFormatter.format(subtract.abs())) .player(account.uuid()) .send(); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckListener.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckListener.java new file mode 100644 index 00000000..0df333a7 --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckListener.java @@ -0,0 +1,51 @@ +package com.eternalcode.economy.paycheck; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import java.math.BigDecimal; +import java.util.Objects; + + +public class PaycheckListener implements Listener { + private final PaycheckManager paycheckManager; + private final PaycheckTagger paycheckTagger; + + public PaycheckListener(PaycheckManager paycheckManager, PaycheckTagger paycheckTagger) { + this.paycheckManager = paycheckManager; + this.paycheckTagger = paycheckTagger; + } + + + @EventHandler + public void onItemUse(PlayerInteractEvent event) { + ItemStack item = event.getItem(); + if (item == null) { + return; + } + + item = item.clone(); + + BigDecimal value = paycheckTagger.getValue(item); + + if (Objects.equals(value, BigDecimal.ZERO)) { + return; + } + + event.setCancelled(true); + Player player = event.getPlayer(); + + if (event.getAction() == Action.RIGHT_CLICK_AIR) { + if (player.isSneaking()) { + paycheckManager.redeem(player, item, value); + } else { + item.setAmount(1); + paycheckManager.redeem(player, item, value); + } + } + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java index 16d60d15..8bdaec69 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java @@ -1,8 +1,13 @@ package com.eternalcode.economy.paycheck; +import com.eternalcode.economy.account.Account; +import com.eternalcode.economy.account.AccountManager; +import com.eternalcode.economy.account.AccountPaymentService; import com.eternalcode.economy.config.implementation.PluginConfig; +import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; @@ -19,23 +24,36 @@ public class PaycheckManager { private final NoticeService noticeService; private final PluginConfig config; private final PaycheckTagger paycheckTagger; + private final DecimalFormatter decimalFormatter; + + private final AccountPaymentService accountPaymentService; + private final AccountManager accountManager; + private final MiniMessage mm; public PaycheckManager( NoticeService noticeService, PluginConfig pluginConfig, - PaycheckTagger paycheckTagger + DecimalFormatter decimalFormatter, + PaycheckTagger paycheckTagger, + AccountPaymentService accountPaymentService, + AccountManager accountManager ) { this.noticeService = noticeService; this.config = pluginConfig; + this.decimalFormatter = decimalFormatter; this.paycheckTagger = paycheckTagger; + + this.accountPaymentService = accountPaymentService; + this.accountManager = accountManager; + this.mm = MiniMessage.miniMessage(); } public void setItem(Player player) { ItemStack item = player.getInventory().getItemInMainHand(); - if(item.getType() == Material.AIR) { + if (item.getType() == Material.AIR) { noticeService.create() .notice(messageConfig -> messageConfig.paycheck.noItem) .player(player.getUniqueId()) @@ -46,9 +64,8 @@ public void setItem(Player player) { this.config.currencyItem.item = item; - String displayName = item.getItemMeta().getDisplayName(); - if(displayName.isEmpty()) { + if (displayName.isEmpty()) { this.config.currencyItem.name = item.getType().name(); } else { this.config.currencyItem.name = displayName; @@ -65,29 +82,71 @@ public void setItem(Player player) { public void givePaycheck(UUID uuid, BigDecimal value) { Player player = Bukkit.getPlayer(uuid); - if(player == null) { + if (player == null) { + return; + } + + ItemStack item = paycheckTagger.tagItem(setUpItem(value), value); + player.getInventory().addItem(item); + player.updateInventory(); + + Account account = accountManager.getAccount(player.getUniqueId()); + accountPaymentService.removeBalance(account, value); + + noticeService.create() + .notice(messageConfig -> messageConfig.paycheck.withdraw) + .placeholder("{VALUE}", decimalFormatter.format(value)) + .player(player.getUniqueId()) + .send(); + } + + public void redeem(Player player, ItemStack item, BigDecimal value) { + ItemStack activeItem; + ItemStack itemInHand = player.getInventory().getItemInMainHand(); + ItemStack itemInOffHand = player.getInventory().getItemInOffHand(); + + if (itemInHand.isSimilar(item)) { + activeItem = itemInHand; + } else if (itemInOffHand.isSimilar(item)) { + activeItem = itemInOffHand; + } else { + noticeService.create() + .notice(messageConfig -> messageConfig.paycheck.noCheck) + .player(player.getUniqueId()) + .send(); return; } + player.getInventory().removeItem(activeItem); + activeItem.setAmount(activeItem.getAmount() - item.getAmount()); + player.getInventory().addItem(activeItem); + player.updateInventory(); + + BigDecimal finalValue = value.multiply(BigDecimal.valueOf(item.getAmount())); + + Account account = accountManager.getAccount(player.getUniqueId()); + accountPaymentService.addBalance(account, finalValue); + + noticeService.create() + .notice(messageConfig -> messageConfig.paycheck.redeem) + .placeholder("{VALUE}", decimalFormatter.format(finalValue)) + .player(player.getUniqueId()) + .send(); + } + + private ItemStack setUpItem(BigDecimal value) { ItemStack item = this.config.currencyItem.item; ItemMeta meta = Objects.requireNonNull(item.getItemMeta()); String displayName = this.config.currencyItem.name - .replace("{VALUE}", value.toString()); + .replace("{VALUE}", decimalFormatter.format(value)); - Component component = mm.deserialize(displayName); + Component component = mm.deserialize(displayName).decoration(TextDecoration.ITALIC, false); String legacyName = LegacyComponentSerializer.legacySection().serialize(component); meta.setDisplayName(legacyName); item.setItemMeta(meta); - item = paycheckTagger.tagItem(item, value); - player.getInventory().addItem(item); - - noticeService.create() - .notice(messageConfig -> messageConfig.paycheck.withdraw) - .placeholder("{VALUE}", value.toString()) - .player(player.getUniqueId()) - .send(); + return item; } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java index 0bf4a5fe..21af820b 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java @@ -12,17 +12,32 @@ public class PaycheckTagger { private final NamespacedKey amountKey; public PaycheckTagger(Plugin plugin) { - this.amountKey = new NamespacedKey(plugin, "paycheck"); + this.amountKey = new NamespacedKey(plugin, "value"); } public ItemStack tagItem(ItemStack item, BigDecimal amount) { ItemStack taggedItem = item.clone(); ItemMeta meta = taggedItem.getItemMeta(); - if(meta != null) { + if (meta != null) { meta.getPersistentDataContainer().set(amountKey, PersistentDataType.DOUBLE, amount.doubleValue()); + taggedItem.setItemMeta(meta); } return taggedItem; } + + public BigDecimal getValue(ItemStack item) { + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return BigDecimal.ZERO; + } + + Double amount = meta.getPersistentDataContainer().get(amountKey, PersistentDataType.DOUBLE); + if (amount == null) { + return BigDecimal.ZERO; + } + + return BigDecimal.valueOf(amount); + } } From 57297af5cd46ef18d92b706f4d18d3176348ab5e Mon Sep 17 00:00:00 2001 From: Qbiter Date: Sun, 12 Oct 2025 18:06:39 +0200 Subject: [PATCH 03/17] Refactor paycheck system to withdraw system: rename classes and update dependencies for improved clarity and functionality --- .../economy/EconomyBukkitPlugin.java | 18 ++++++++--------- .../command/impl/admin/AdminItemCommand.java | 10 +++++----- .../WithdrawCommand.java} | 14 ++++++------- .../WithdrawListener.java} | 20 +++++++++---------- .../WithdrawManager.java} | 14 ++++++------- .../WithdrawTagger.java} | 6 +++--- 6 files changed, 41 insertions(+), 41 deletions(-) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/{paycheck/PaycheckCommand.java => withdraw/WithdrawCommand.java} (85%) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/{paycheck/PaycheckListener.java => withdraw/WithdrawListener.java} (60%) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/{paycheck/PaycheckManager.java => withdraw/WithdrawManager.java} (94%) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/{paycheck/PaycheckTagger.java => withdraw/WithdrawTagger.java} (90%) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 1bcd9089..5447a41b 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -35,10 +35,10 @@ import com.eternalcode.economy.multification.NoticeBroadcastHandler; import com.eternalcode.economy.multification.NoticeHandler; import com.eternalcode.economy.multification.NoticeService; -import com.eternalcode.economy.paycheck.PaycheckCommand; -import com.eternalcode.economy.paycheck.PaycheckListener; -import com.eternalcode.economy.paycheck.PaycheckManager; -import com.eternalcode.economy.paycheck.PaycheckTagger; +import com.eternalcode.economy.withdraw.WithdrawCommand; +import com.eternalcode.economy.withdraw.WithdrawListener; +import com.eternalcode.economy.withdraw.WithdrawManager; +import com.eternalcode.economy.withdraw.WithdrawTagger; import com.eternalcode.economy.vault.VaultEconomyProvider; import com.eternalcode.multification.notice.Notice; import com.eternalcode.multification.notice.NoticeBroadcast; @@ -104,8 +104,8 @@ public void onEnable() { DecimalFormatter decimalFormatter = new DecimalFormatterImpl(pluginConfig); AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); - PaycheckTagger paycheckTagger = new PaycheckTagger(this); - PaycheckManager paycheckManager = new PaycheckManager(noticeService, pluginConfig, decimalFormatter, paycheckTagger, accountPaymentService, accountManager); + WithdrawTagger withdrawTagger = new WithdrawTagger(this); + WithdrawManager withdrawManager = new WithdrawManager(noticeService, pluginConfig, decimalFormatter, withdrawTagger, accountPaymentService, accountManager); VaultEconomyProvider vaultEconomyProvider = new VaultEconomyProvider(this, decimalFormatter, accountPaymentService, accountManager); @@ -137,8 +137,8 @@ public void onEnable() { new AdminSetCommand(accountPaymentService, decimalFormatter, noticeService), new AdminResetCommand(accountPaymentService, noticeService), new AdminBalanceCommand(noticeService, decimalFormatter), - new AdminItemCommand(paycheckManager), - new PaycheckCommand(paycheckManager, noticeService, decimalFormatter), + new AdminItemCommand(withdrawManager), + new WithdrawCommand(withdrawManager, noticeService, decimalFormatter), new MoneyBalanceCommand(noticeService, decimalFormatter), new MoneyTransferCommand(accountPaymentService, decimalFormatter, noticeService, pluginConfig), new EconomyReloadCommand(configService, noticeService), @@ -157,7 +157,7 @@ public void onEnable() { server.getPluginManager().registerEvents(new AccountController(accountManager), this); - server.getPluginManager().registerEvents(new PaycheckListener(paycheckManager, paycheckTagger), this); + server.getPluginManager().registerEvents(new WithdrawListener(withdrawManager, withdrawTagger), this); BridgeManager bridgeManager = new BridgeManager( this.getDescription(), diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java index 5c42757b..381af21f 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java @@ -1,7 +1,7 @@ package com.eternalcode.economy.command.impl.admin; import com.eternalcode.economy.EconomyPermissionConstant; -import com.eternalcode.economy.paycheck.PaycheckManager; +import com.eternalcode.economy.withdraw.WithdrawManager; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; @@ -11,14 +11,14 @@ @Command(name = "economy item") @Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) public class AdminItemCommand { - private final PaycheckManager paycheckManager; + private final WithdrawManager withdrawManager; - public AdminItemCommand(PaycheckManager paycheckManager) { - this.paycheckManager = paycheckManager; + public AdminItemCommand(WithdrawManager withdrawManager) { + this.withdrawManager = withdrawManager; } @Execute void execute(@Context Player player) { - paycheckManager.setItem(player); + withdrawManager.setItem(player); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java similarity index 85% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java index 2cfd7315..2d625e99 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java @@ -1,4 +1,4 @@ -package com.eternalcode.economy.paycheck; +package com.eternalcode.economy.withdraw; import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; @@ -17,17 +17,17 @@ @Command(name = "withdraw", aliases = {"paycheck", "check"}) @Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) -public class PaycheckCommand { - private final PaycheckManager paycheckManager; +public class WithdrawCommand { + private final WithdrawManager withdrawManager; private final NoticeService noticeService; private final DecimalFormatter decimalFormatter; - public PaycheckCommand( - PaycheckManager paycheckManager, + public WithdrawCommand( + WithdrawManager withdrawManager, NoticeService noticeService, DecimalFormatter decimalFormatter ) { - this.paycheckManager = paycheckManager; + this.withdrawManager = withdrawManager; this.noticeService = noticeService; this.decimalFormatter = decimalFormatter; } @@ -47,6 +47,6 @@ void execute(@Context Account account, @Arg @Positive @Key(PriceArgumentResolver return; } - paycheckManager.givePaycheck(account.uuid(), value); + withdrawManager.givePaycheck(account.uuid(), value); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckListener.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawListener.java similarity index 60% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckListener.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawListener.java index 0df333a7..d07cb85e 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckListener.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawListener.java @@ -1,4 +1,4 @@ -package com.eternalcode.economy.paycheck; +package com.eternalcode.economy.withdraw; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -11,13 +11,13 @@ import java.util.Objects; -public class PaycheckListener implements Listener { - private final PaycheckManager paycheckManager; - private final PaycheckTagger paycheckTagger; +public class WithdrawListener implements Listener { + private final WithdrawManager withdrawManager; + private final WithdrawTagger withdrawTagger; - public PaycheckListener(PaycheckManager paycheckManager, PaycheckTagger paycheckTagger) { - this.paycheckManager = paycheckManager; - this.paycheckTagger = paycheckTagger; + public WithdrawListener(WithdrawManager withdrawManager, WithdrawTagger withdrawTagger) { + this.withdrawManager = withdrawManager; + this.withdrawTagger = withdrawTagger; } @@ -30,7 +30,7 @@ public void onItemUse(PlayerInteractEvent event) { item = item.clone(); - BigDecimal value = paycheckTagger.getValue(item); + BigDecimal value = withdrawTagger.getValue(item); if (Objects.equals(value, BigDecimal.ZERO)) { return; @@ -41,10 +41,10 @@ public void onItemUse(PlayerInteractEvent event) { if (event.getAction() == Action.RIGHT_CLICK_AIR) { if (player.isSneaking()) { - paycheckManager.redeem(player, item, value); + withdrawManager.redeem(player, item, value); } else { item.setAmount(1); - paycheckManager.redeem(player, item, value); + withdrawManager.redeem(player, item, value); } } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawManager.java similarity index 94% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawManager.java index 8bdaec69..ce7f4c94 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckManager.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawManager.java @@ -1,4 +1,4 @@ -package com.eternalcode.economy.paycheck; +package com.eternalcode.economy.withdraw; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountManager; @@ -20,10 +20,10 @@ import java.util.Objects; import java.util.UUID; -public class PaycheckManager { +public class WithdrawManager { private final NoticeService noticeService; private final PluginConfig config; - private final PaycheckTagger paycheckTagger; + private final WithdrawTagger withdrawTagger; private final DecimalFormatter decimalFormatter; private final AccountPaymentService accountPaymentService; @@ -31,18 +31,18 @@ public class PaycheckManager { private final MiniMessage mm; - public PaycheckManager( + public WithdrawManager( NoticeService noticeService, PluginConfig pluginConfig, DecimalFormatter decimalFormatter, - PaycheckTagger paycheckTagger, + WithdrawTagger withdrawTagger, AccountPaymentService accountPaymentService, AccountManager accountManager ) { this.noticeService = noticeService; this.config = pluginConfig; this.decimalFormatter = decimalFormatter; - this.paycheckTagger = paycheckTagger; + this.withdrawTagger = withdrawTagger; this.accountPaymentService = accountPaymentService; this.accountManager = accountManager; @@ -86,7 +86,7 @@ public void givePaycheck(UUID uuid, BigDecimal value) { return; } - ItemStack item = paycheckTagger.tagItem(setUpItem(value), value); + ItemStack item = withdrawTagger.tagItem(setUpItem(value), value); player.getInventory().addItem(item); player.updateInventory(); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawTagger.java similarity index 90% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawTagger.java index 21af820b..ee7ae873 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/paycheck/PaycheckTagger.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawTagger.java @@ -1,4 +1,4 @@ -package com.eternalcode.economy.paycheck; +package com.eternalcode.economy.withdraw; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; @@ -8,10 +8,10 @@ import java.math.BigDecimal; -public class PaycheckTagger { +public class WithdrawTagger { private final NamespacedKey amountKey; - public PaycheckTagger(Plugin plugin) { + public WithdrawTagger(Plugin plugin) { this.amountKey = new NamespacedKey(plugin, "value"); } From a6c213450da2d010297cebb41e105d10eb79f70c Mon Sep 17 00:00:00 2001 From: Qbiter Date: Mon, 13 Oct 2025 02:49:05 +0200 Subject: [PATCH 04/17] Refactor withdraw system: rename classes, update dependencies, and implement new withdraw functionality + Support blocking anvil interactions --- eternaleconomy-core/build.gradle.kts | 3 + .../economy/EconomyBukkitPlugin.java | 20 +++-- .../economy/EconomyPermissionConstant.java | 1 + .../messages/MessageConfig.java | 2 +- ...on.java => MessageWithdrawSubSection.java} | 19 +++-- .../economy/withdraw/WithdrawCommand.java | 14 ++-- ...awTagger.java => WithdrawItemService.java} | 26 ++++--- .../economy/withdraw/WithdrawListener.java | 51 ------------- ...hdrawManager.java => WithdrawService.java} | 73 +++++++++++-------- .../WithdrawSetItemCommand.java} | 15 ++-- .../controller/WithdrawAnvilController.java | 52 +++++++++++++ .../controller/WithdrawController.java | 53 ++++++++++++++ 12 files changed, 206 insertions(+), 123 deletions(-) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/{MessagePaycheckSubSection.java => MessageWithdrawSubSection.java} (51%) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/{WithdrawTagger.java => WithdrawItemService.java} (55%) delete mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawListener.java rename eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/{WithdrawManager.java => WithdrawService.java} (70%) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/{command/impl/admin/AdminItemCommand.java => withdraw/WithdrawSetItemCommand.java} (55%) create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java diff --git a/eternaleconomy-core/build.gradle.kts b/eternaleconomy-core/build.gradle.kts index 7e503dc7..f1ff9312 100644 --- a/eternaleconomy-core/build.gradle.kts +++ b/eternaleconomy-core/build.gradle.kts @@ -88,6 +88,9 @@ bukkit { tasks.runServer { minecraftVersion("1.21.8") + downloadPlugins { + url("https://github.com/MilkBowl/Vault/releases/download/1.7.3/Vault.jar") + } } tasks.shadowJar { diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 5447a41b..93dde398 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -35,10 +35,12 @@ import com.eternalcode.economy.multification.NoticeBroadcastHandler; import com.eternalcode.economy.multification.NoticeHandler; import com.eternalcode.economy.multification.NoticeService; +import com.eternalcode.economy.withdraw.WithdrawSetItemCommand; import com.eternalcode.economy.withdraw.WithdrawCommand; -import com.eternalcode.economy.withdraw.WithdrawListener; -import com.eternalcode.economy.withdraw.WithdrawManager; -import com.eternalcode.economy.withdraw.WithdrawTagger; +import com.eternalcode.economy.withdraw.controller.WithdrawAnvilController; +import com.eternalcode.economy.withdraw.controller.WithdrawController; +import com.eternalcode.economy.withdraw.WithdrawService; +import com.eternalcode.economy.withdraw.WithdrawItemService; import com.eternalcode.economy.vault.VaultEconomyProvider; import com.eternalcode.multification.notice.Notice; import com.eternalcode.multification.notice.NoticeBroadcast; @@ -104,8 +106,9 @@ public void onEnable() { DecimalFormatter decimalFormatter = new DecimalFormatterImpl(pluginConfig); AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); - WithdrawTagger withdrawTagger = new WithdrawTagger(this); - WithdrawManager withdrawManager = new WithdrawManager(noticeService, pluginConfig, decimalFormatter, withdrawTagger, accountPaymentService, accountManager); + WithdrawItemService withdrawItemService = new WithdrawItemService(this); + WithdrawService withdrawService = new WithdrawService(server, noticeService, pluginConfig, decimalFormatter, + withdrawItemService, accountPaymentService, accountManager, miniMessage); VaultEconomyProvider vaultEconomyProvider = new VaultEconomyProvider(this, decimalFormatter, accountPaymentService, accountManager); @@ -137,8 +140,8 @@ public void onEnable() { new AdminSetCommand(accountPaymentService, decimalFormatter, noticeService), new AdminResetCommand(accountPaymentService, noticeService), new AdminBalanceCommand(noticeService, decimalFormatter), - new AdminItemCommand(withdrawManager), - new WithdrawCommand(withdrawManager, noticeService, decimalFormatter), + new WithdrawSetItemCommand(withdrawService), + new WithdrawCommand(withdrawService, noticeService, decimalFormatter), new MoneyBalanceCommand(noticeService, decimalFormatter), new MoneyTransferCommand(accountPaymentService, decimalFormatter, noticeService, pluginConfig), new EconomyReloadCommand(configService, noticeService), @@ -157,7 +160,8 @@ public void onEnable() { server.getPluginManager().registerEvents(new AccountController(accountManager), this); - server.getPluginManager().registerEvents(new WithdrawListener(withdrawManager, withdrawTagger), this); + server.getPluginManager().registerEvents(new WithdrawController(withdrawService, withdrawItemService), this); + server.getPluginManager().registerEvents(new WithdrawAnvilController(withdrawItemService, noticeService), this); BridgeManager bridgeManager = new BridgeManager( this.getDescription(), diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java index 7857def0..96330d2f 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyPermissionConstant.java @@ -14,4 +14,5 @@ public class EconomyPermissionConstant { public static final String PLAYER_BALANCE_OTHER_PERMISSION = "eternaleconomy.player.balance.other"; public static final String PLAYER_PAY_PERMISSION = "eternaleconomy.player.pay"; public static final String PLAYER_BALANCE_TOP_PERMISSION = "eternaleconomy.player.balance.top"; + public static final String PLAYER_WITHDRAW_PERMISSION = "eternaleconomy.player.withdraw"; } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java index a0cc2fa3..c82a0619 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java @@ -27,5 +27,5 @@ public class MessageConfig extends OkaeriConfig { public MessageAdminSubSection admin = new MessageAdminSubSection(); public MessagesPlayerSubSection player = new MessagesPlayerSubSection(); - public MessagePaycheckSubSection paycheck = new MessagePaycheckSubSection(); + public MessageWithdrawSubSection withdraw = new MessageWithdrawSubSection(); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageWithdrawSubSection.java similarity index 51% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageWithdrawSubSection.java index 7fa1ee3e..5a11ff10 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagePaycheckSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageWithdrawSubSection.java @@ -3,7 +3,7 @@ import com.eternalcode.multification.notice.Notice; import eu.okaeri.configs.OkaeriConfig; -public class MessagePaycheckSubSection extends OkaeriConfig { +public class MessageWithdrawSubSection extends OkaeriConfig { public Notice noItem = Notice.chat( "ECONOMY You must hold item to create paycheck!" ); @@ -12,15 +12,20 @@ public class MessagePaycheckSubSection extends OkaeriConfig { "ECONOMY You must hold an paycheck to redeem it!" ); - public Notice setItem = Notice.chat( - "ECONOMY You have set paycheck item to {ITEM}!" + public Notice itemSet = Notice.chat( + "ECONOMY You have set the banknote item to {ITEM}!" ); - public Notice withdraw = Notice.chat( - "ECONOMY You have withdrawn a paycheck of {VALUE}!" + public Notice banknoteWithdrawn = Notice.chat( + "ECONOMY You have withdrawn a banknote worth {VALUE}!" ); - public Notice redeem = Notice.chat( - "ECONOMY You have redeemed your paycheck of {VALUE}!" + public Notice banknoteRedeemed = Notice.chat( + "ECONOMY You have redeemed your banknote worth {VALUE}!" + ); + + public Notice anvilInteract = Notice.chat( + "ECONOMY You cannot rename the " + + "banknote item in an anvil!" ); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java index 2d625e99..947722e3 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java @@ -11,29 +11,27 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; -import jakarta.validation.constraints.Positive; - import java.math.BigDecimal; @Command(name = "withdraw", aliases = {"paycheck", "check"}) -@Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) +@Permission(EconomyPermissionConstant.PLAYER_WITHDRAW_PERMISSION) public class WithdrawCommand { - private final WithdrawManager withdrawManager; + private final WithdrawService withdrawService; private final NoticeService noticeService; private final DecimalFormatter decimalFormatter; public WithdrawCommand( - WithdrawManager withdrawManager, + WithdrawService withdrawService, NoticeService noticeService, DecimalFormatter decimalFormatter ) { - this.withdrawManager = withdrawManager; + this.withdrawService = withdrawService; this.noticeService = noticeService; this.decimalFormatter = decimalFormatter; } @Execute - void execute(@Context Account account, @Arg @Positive @Key(PriceArgumentResolver.KEY) BigDecimal value) { + void execute(@Context Account account, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal value) { BigDecimal balance = account.balance(); BigDecimal subtract = balance.subtract(value); @@ -47,6 +45,6 @@ void execute(@Context Account account, @Arg @Positive @Key(PriceArgumentResolver return; } - withdrawManager.givePaycheck(account.uuid(), value); + withdrawService.addBanknote(account.uuid(), value); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawTagger.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java similarity index 55% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawTagger.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java index ee7ae873..25ab145d 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawTagger.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java @@ -1,26 +1,25 @@ package com.eternalcode.economy.withdraw; +import java.math.BigDecimal; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataType; import org.bukkit.plugin.Plugin; -import java.math.BigDecimal; - -public class WithdrawTagger { +public class WithdrawItemService { private final NamespacedKey amountKey; - public WithdrawTagger(Plugin plugin) { - this.amountKey = new NamespacedKey(plugin, "value"); + public WithdrawItemService(Plugin plugin) { + this.amountKey = new NamespacedKey(plugin, "economy_withdraw_value"); } - public ItemStack tagItem(ItemStack item, BigDecimal amount) { + public ItemStack markAsBanknote(ItemStack item, BigDecimal amount) { ItemStack taggedItem = item.clone(); ItemMeta meta = taggedItem.getItemMeta(); if (meta != null) { - meta.getPersistentDataContainer().set(amountKey, PersistentDataType.DOUBLE, amount.doubleValue()); + meta.getPersistentDataContainer().set(amountKey, PersistentDataType.STRING, amount.toPlainString()); taggedItem.setItemMeta(meta); } @@ -33,11 +32,20 @@ public BigDecimal getValue(ItemStack item) { return BigDecimal.ZERO; } - Double amount = meta.getPersistentDataContainer().get(amountKey, PersistentDataType.DOUBLE); + String amount = meta.getPersistentDataContainer().get(amountKey, PersistentDataType.STRING); if (amount == null) { return BigDecimal.ZERO; } - return BigDecimal.valueOf(amount); + return new BigDecimal(amount); + } + + public boolean isBanknote(ItemStack item) { + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return false; + } + + return meta.getPersistentDataContainer().has(amountKey, PersistentDataType.STRING); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawListener.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawListener.java deleted file mode 100644 index d07cb85e..00000000 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawListener.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.eternalcode.economy.withdraw; - -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; - -import java.math.BigDecimal; -import java.util.Objects; - - -public class WithdrawListener implements Listener { - private final WithdrawManager withdrawManager; - private final WithdrawTagger withdrawTagger; - - public WithdrawListener(WithdrawManager withdrawManager, WithdrawTagger withdrawTagger) { - this.withdrawManager = withdrawManager; - this.withdrawTagger = withdrawTagger; - } - - - @EventHandler - public void onItemUse(PlayerInteractEvent event) { - ItemStack item = event.getItem(); - if (item == null) { - return; - } - - item = item.clone(); - - BigDecimal value = withdrawTagger.getValue(item); - - if (Objects.equals(value, BigDecimal.ZERO)) { - return; - } - - event.setCancelled(true); - Player player = event.getPlayer(); - - if (event.getAction() == Action.RIGHT_CLICK_AIR) { - if (player.isSneaking()) { - withdrawManager.redeem(player, item, value); - } else { - item.setAmount(1); - withdrawManager.redeem(player, item, value); - } - } - } -} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawManager.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java similarity index 70% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawManager.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java index ce7f4c94..a4a0270f 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawManager.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java @@ -6,48 +6,52 @@ import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; +import java.math.BigDecimal; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import java.math.BigDecimal; -import java.util.Objects; -import java.util.UUID; - -public class WithdrawManager { +public class WithdrawService { + private final Server server; private final NoticeService noticeService; private final PluginConfig config; - private final WithdrawTagger withdrawTagger; + private final WithdrawItemService withdrawItemService; private final DecimalFormatter decimalFormatter; private final AccountPaymentService accountPaymentService; private final AccountManager accountManager; - private final MiniMessage mm; + private final MiniMessage miniMessage; - public WithdrawManager( + public WithdrawService( + Server server, NoticeService noticeService, PluginConfig pluginConfig, DecimalFormatter decimalFormatter, - WithdrawTagger withdrawTagger, + WithdrawItemService withdrawItemService, AccountPaymentService accountPaymentService, - AccountManager accountManager + AccountManager accountManager, + MiniMessage miniMessage ) { + this.server = server; this.noticeService = noticeService; this.config = pluginConfig; this.decimalFormatter = decimalFormatter; - this.withdrawTagger = withdrawTagger; + this.withdrawItemService = withdrawItemService; this.accountPaymentService = accountPaymentService; this.accountManager = accountManager; - this.mm = MiniMessage.miniMessage(); + this.miniMessage = miniMessage; } public void setItem(Player player) { @@ -55,7 +59,7 @@ public void setItem(Player player) { if (item.getType() == Material.AIR) { noticeService.create() - .notice(messageConfig -> messageConfig.paycheck.noItem) + .notice(messageConfig -> messageConfig.withdraw.noItem) .player(player.getUniqueId()) .send(); @@ -64,37 +68,42 @@ public void setItem(Player player) { this.config.currencyItem.item = item; - String displayName = item.getItemMeta().getDisplayName(); - if (displayName.isEmpty()) { - this.config.currencyItem.name = item.getType().name(); - } else { - this.config.currencyItem.name = displayName; + ItemMeta meta = item.getItemMeta(); + + if (meta != null) { + String displayName = meta.getDisplayName(); + + if (displayName.isEmpty()) { + this.config.currencyItem.name = item.getType().name(); + } + else { + this.config.currencyItem.name = displayName; + } } - this.config.save(); + CompletableFuture.runAsync(this.config::save); noticeService.create() - .notice(messageConfig -> messageConfig.paycheck.setItem) + .notice(messageConfig -> messageConfig.withdraw.itemSet) .placeholder("{ITEM}", this.config.currencyItem.name) .player(player.getUniqueId()) .send(); } - public void givePaycheck(UUID uuid, BigDecimal value) { - Player player = Bukkit.getPlayer(uuid); + public void addBanknote(UUID uuid, BigDecimal value) { + Player player = server.getPlayer(uuid); if (player == null) { return; } - ItemStack item = withdrawTagger.tagItem(setUpItem(value), value); + ItemStack item = withdrawItemService.markAsBanknote(setUpItem(value), value); player.getInventory().addItem(item); - player.updateInventory(); Account account = accountManager.getAccount(player.getUniqueId()); accountPaymentService.removeBalance(account, value); noticeService.create() - .notice(messageConfig -> messageConfig.paycheck.withdraw) + .notice(messageConfig -> messageConfig.withdraw.banknoteWithdrawn) .placeholder("{VALUE}", decimalFormatter.format(value)) .player(player.getUniqueId()) .send(); @@ -107,11 +116,13 @@ public void redeem(Player player, ItemStack item, BigDecimal value) { if (itemInHand.isSimilar(item)) { activeItem = itemInHand; - } else if (itemInOffHand.isSimilar(item)) { + } + else if (itemInOffHand.isSimilar(item)) { activeItem = itemInOffHand; - } else { + } + else { noticeService.create() - .notice(messageConfig -> messageConfig.paycheck.noCheck) + .notice(messageConfig -> messageConfig.withdraw.noCheck) .player(player.getUniqueId()) .send(); return; @@ -128,7 +139,7 @@ public void redeem(Player player, ItemStack item, BigDecimal value) { accountPaymentService.addBalance(account, finalValue); noticeService.create() - .notice(messageConfig -> messageConfig.paycheck.redeem) + .notice(messageConfig -> messageConfig.withdraw.banknoteRedeemed) .placeholder("{VALUE}", decimalFormatter.format(finalValue)) .player(player.getUniqueId()) .send(); @@ -141,7 +152,7 @@ private ItemStack setUpItem(BigDecimal value) { String displayName = this.config.currencyItem.name .replace("{VALUE}", decimalFormatter.format(value)); - Component component = mm.deserialize(displayName).decoration(TextDecoration.ITALIC, false); + Component component = miniMessage.deserialize(displayName).decoration(TextDecoration.ITALIC, false); String legacyName = LegacyComponentSerializer.legacySection().serialize(component); meta.setDisplayName(legacyName); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java similarity index 55% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java index 381af21f..38f18c75 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminItemCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java @@ -1,24 +1,23 @@ -package com.eternalcode.economy.command.impl.admin; +package com.eternalcode.economy.withdraw; import com.eternalcode.economy.EconomyPermissionConstant; -import com.eternalcode.economy.withdraw.WithdrawManager; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; import org.bukkit.entity.Player; -@Command(name = "economy item") +@Command(name = "economy withdraw setitem") @Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) -public class AdminItemCommand { - private final WithdrawManager withdrawManager; +public class WithdrawSetItemCommand { + private final WithdrawService withdrawService; - public AdminItemCommand(WithdrawManager withdrawManager) { - this.withdrawManager = withdrawManager; + public WithdrawSetItemCommand(WithdrawService withdrawService) { + this.withdrawService = withdrawService; } @Execute void execute(@Context Player player) { - withdrawManager.setItem(player); + withdrawService.setItem(player); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java new file mode 100644 index 00000000..2c68a00e --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java @@ -0,0 +1,52 @@ +package com.eternalcode.economy.withdraw.controller; + +import com.eternalcode.economy.multification.NoticeService; +import com.eternalcode.economy.withdraw.WithdrawItemService; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.AnvilInventory; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class WithdrawAnvilController implements Listener { + + private final WithdrawItemService withdrawItemService; + private final NoticeService noticeService; + + public WithdrawAnvilController(WithdrawItemService withdrawItemService, NoticeService noticeService) { + this.withdrawItemService = withdrawItemService; + this.noticeService = noticeService; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onAnvilUse(InventoryClickEvent event) { + Inventory topInventory = event.getInventory(); + Inventory clickedInventory = event.getClickedInventory(); + + if (!(event.getView().getTopInventory() instanceof AnvilInventory anvilInventory)) { + return; + } + + ItemStack item = event.getCurrentItem(); + + if (item == null) { + return; + } + + if (event.getAction() != InventoryAction.MOVE_TO_OTHER_INVENTORY) { + return; + } + + if (this.withdrawItemService.isBanknote(item)) { + event.getView().close(); + + this.noticeService.create() + .notice(messageConfig -> messageConfig.withdraw.anvilInteract) + .viewer(event.getWhoClicked()) + .send(); + } + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java new file mode 100644 index 00000000..205043fe --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java @@ -0,0 +1,53 @@ +package com.eternalcode.economy.withdraw.controller; + +import com.eternalcode.economy.withdraw.WithdrawItemService; +import com.eternalcode.economy.withdraw.WithdrawService; +import java.math.BigDecimal; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +public class WithdrawController implements Listener { + private final WithdrawService withdrawService; + private final WithdrawItemService withdrawItemService; + + public WithdrawController(WithdrawService withdrawService, WithdrawItemService withdrawItemService) { + this.withdrawService = withdrawService; + this.withdrawItemService = withdrawItemService; + } + + @EventHandler + public void onItemUse(PlayerInteractEvent event) { + ItemStack item = event.getItem(); + + if (item == null) { + return; + } + + BigDecimal value = withdrawItemService.getValue(item); + + if (value == null || value.compareTo(BigDecimal.ZERO) == 0) { + return; + } + + Player player = event.getPlayer(); + Action action = event.getAction(); + + if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) { + return; + } + + event.setCancelled(true); + + ItemStack itemToRedeem = item.clone(); + + if (!player.isSneaking()) { + itemToRedeem.setAmount(1); + } + + this.withdrawService.redeem(player, itemToRedeem, value); + } +} From 632c26baf0ca3c9e1229e9e4da675837de5895ba Mon Sep 17 00:00:00 2001 From: Qbiter Date: Mon, 13 Oct 2025 03:06:08 +0200 Subject: [PATCH 05/17] Refactor withdraw system: update message handling, improve price argument parsing, and enhance admin commands --- .../economy/EconomyBukkitPlugin.java | 46 +++++++++++-------- .../argument/PriceArgumentResolver.java | 15 ++++-- .../messages/MessageConfig.java | 8 ++-- .../WithdrawMessageConfig.java} | 12 ++--- .../economy/withdraw/WithdrawService.java | 7 +-- 5 files changed, 54 insertions(+), 34 deletions(-) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/{config/implementation/messages/MessageWithdrawSubSection.java => withdraw/WithdrawMessageConfig.java} (75%) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 93dde398..84eae7c6 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -20,7 +20,11 @@ import com.eternalcode.economy.command.handler.MissingPermissionHandlerImpl; import com.eternalcode.economy.command.impl.MoneyBalanceCommand; import com.eternalcode.economy.command.impl.MoneyTransferCommand; -import com.eternalcode.economy.command.impl.admin.*; +import com.eternalcode.economy.command.impl.admin.AdminAddCommand; +import com.eternalcode.economy.command.impl.admin.AdminBalanceCommand; +import com.eternalcode.economy.command.impl.admin.AdminRemoveCommand; +import com.eternalcode.economy.command.impl.admin.AdminResetCommand; +import com.eternalcode.economy.command.impl.admin.AdminSetCommand; import com.eternalcode.economy.command.message.InvalidBigDecimalMessage; import com.eternalcode.economy.command.validator.notsender.NotSender; import com.eternalcode.economy.command.validator.notsender.NotSenderValidator; @@ -35,13 +39,13 @@ import com.eternalcode.economy.multification.NoticeBroadcastHandler; import com.eternalcode.economy.multification.NoticeHandler; import com.eternalcode.economy.multification.NoticeService; -import com.eternalcode.economy.withdraw.WithdrawSetItemCommand; +import com.eternalcode.economy.vault.VaultEconomyProvider; import com.eternalcode.economy.withdraw.WithdrawCommand; +import com.eternalcode.economy.withdraw.WithdrawItemService; +import com.eternalcode.economy.withdraw.WithdrawService; +import com.eternalcode.economy.withdraw.WithdrawSetItemCommand; import com.eternalcode.economy.withdraw.controller.WithdrawAnvilController; import com.eternalcode.economy.withdraw.controller.WithdrawController; -import com.eternalcode.economy.withdraw.WithdrawService; -import com.eternalcode.economy.withdraw.WithdrawItemService; -import com.eternalcode.economy.vault.VaultEconomyProvider; import com.eternalcode.multification.notice.Notice; import com.eternalcode.multification.notice.NoticeBroadcast; import com.google.common.base.Stopwatch; @@ -51,6 +55,9 @@ import dev.rollczi.litecommands.jakarta.LiteJakartaExtension; import dev.rollczi.litecommands.message.LiteMessages; import jakarta.validation.constraints.Positive; +import java.io.File; +import java.math.BigDecimal; +import java.time.Duration; import net.kyori.adventure.platform.AudienceProvider; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -60,10 +67,6 @@ import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; -import java.math.BigDecimal; -import java.time.Duration; - @SuppressWarnings("unused") public class EconomyBukkitPlugin extends JavaPlugin { @@ -91,7 +94,8 @@ public void onEnable() { ConfigService configService = new ConfigService(); MessageConfig messageConfig = configService.create(MessageConfig.class, new File(dataFolder, "messages.yml")); PluginConfig pluginConfig = configService.create(PluginConfig.class, new File(dataFolder, "config.yml")); - CommandsConfig commandsConfig = configService.create(CommandsConfig.class, new File(dataFolder, "commands.yml")); + CommandsConfig commandsConfig = + configService.create(CommandsConfig.class, new File(dataFolder, "commands.yml")); NoticeService noticeService = new NoticeService(messageConfig, this.audienceProvider, miniMessage); @@ -107,7 +111,8 @@ public void onEnable() { AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); WithdrawItemService withdrawItemService = new WithdrawItemService(this); - WithdrawService withdrawService = new WithdrawService(server, noticeService, pluginConfig, decimalFormatter, + WithdrawService withdrawService = new WithdrawService( + server, noticeService, pluginConfig, decimalFormatter, withdrawItemService, accountPaymentService, accountManager, miniMessage); VaultEconomyProvider vaultEconomyProvider = @@ -115,8 +120,9 @@ public void onEnable() { server.getServicesManager().register(Economy.class, vaultEconomyProvider, this, ServicePriority.Highest); this.liteCommands = LiteBukkitFactory.builder("eternaleconomy", this, server) - .extension(new LiteJakartaExtension<>(), settings -> settings - .violationMessage(Positive.class, BigDecimal.class, new InvalidBigDecimalMessage<>(noticeService)) + .extension( + new LiteJakartaExtension<>(), settings -> settings + .violationMessage(Positive.class, BigDecimal.class, new InvalidBigDecimalMessage<>(noticeService)) ) .annotations(extension -> extension.validator( @@ -128,10 +134,11 @@ public void onEnable() { .invalidUsage(new InvalidUsageHandlerImpl(noticeService)) .message(LiteMessages.COMMAND_COOLDOWN, new CommandCooldownMessage(noticeService, commandsConfig)) - .message(LiteMessages.INVALID_NUMBER, (invocation, amount) -> noticeService.create() - .notice(messageConfig.positiveNumberRequired) - .placeholder("{AMOUNT}", amount) - .viewer(invocation.sender())) + .message( + LiteMessages.INVALID_NUMBER, (invocation, amount) -> noticeService.create() + .notice(messageConfig.positiveNumberRequired) + .placeholder("{AMOUNT}", amount) + .viewer(invocation.sender())) .editorGlobal(new CommandCooldownEditor(commandsConfig)) .commands( @@ -151,7 +158,10 @@ public void onEnable() { .context(Account.class, new AccountContext(accountManager, messageConfig)) .argument(Account.class, new AccountArgument(accountManager, noticeService, server)) - .argument(BigDecimal.class, ArgumentKey.of(PriceArgumentResolver.KEY), new PriceArgumentResolver(pluginConfig, messageConfig)) + .argument( + BigDecimal.class, + ArgumentKey.of(PriceArgumentResolver.KEY), + new PriceArgumentResolver(pluginConfig, messageConfig)) .result(Notice.class, new NoticeHandler(noticeService)) .result(NoticeBroadcast.class, new NoticeBroadcastHandler()) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java index dee7e0b1..65db2e24 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java @@ -17,8 +17,7 @@ public class PriceArgumentResolver extends ArgumentResolver { public static final String KEY = "price"; - private static final Pattern PRICE_PATTERN = - Pattern.compile("^(\\d+(?:[.,]\\d+)?)([kmb])?$", Pattern.CASE_INSENSITIVE); + private final Pattern pricePattern; private final PluginConfig config; private final MessageConfig messages; @@ -30,6 +29,14 @@ public PriceArgumentResolver(PluginConfig config, MessageConfig messages) { this.messages = messages; this.multipliers = new HashMap<>(); + StringBuilder suffixes = new StringBuilder(); + this.config.units.format.forEach(unit -> { + this.multipliers.put(unit.getSuffix(), BigDecimal.valueOf(unit.getFactor())); + suffixes.append(unit.getSuffix()); + }); + + this.pricePattern = Pattern.compile("^(\\d+(?:[.,]\\d+)?)([" + suffixes + "])?$", Pattern.CASE_INSENSITIVE); + config.units.format.forEach(unit -> { this.multipliers.put(unit.getSuffix(), BigDecimal.valueOf(unit.getFactor())); }); @@ -38,7 +45,7 @@ public PriceArgumentResolver(PluginConfig config, MessageConfig messages) { @Override protected ParseResult parse( Invocation invocation, Argument argument, String raw) { - Matcher matcher = PRICE_PATTERN.matcher(raw.toLowerCase()); + Matcher matcher = this.pricePattern.matcher(raw.toLowerCase()); if (!matcher.matches()) { return ParseResult.failure(this.messages.invalidPrice); @@ -66,7 +73,7 @@ protected ParseResult parse( return ParseResult.success(value.setScale(2, RoundingMode.DOWN)); - } catch (NumberFormatException e) { + } catch (NumberFormatException exception) { return ParseResult.failure(this.messages.invalidPrice); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java index c82a0619..3ac910b6 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java @@ -1,5 +1,6 @@ package com.eternalcode.economy.config.implementation.messages; +import com.eternalcode.economy.withdraw.WithdrawMessageConfig; import com.eternalcode.multification.notice.Notice; import eu.okaeri.configs.OkaeriConfig; @@ -22,10 +23,11 @@ public class MessageConfig extends OkaeriConfig { "ECONOMY Missing permission: {PERMISSION}."); public Notice invalidPrice = - Notice.chat("ECONOMY Nieprawidłowy format ceny! Użyj: 1000, 1k, 1.5k, 1m, etc."); - public Notice priceNeedToBeGreaterThanZero = Notice.chat("ECONOMY Cena musi być większa od 0!"); + Notice.chat("ECONOMY Invalid price format! Use: 1000, 1k, 1.5k, 1m, etc."); + public Notice priceNeedToBeGreaterThanZero = Notice.chat("ECONOMY " + + " Price must be greater than 0!"); public MessageAdminSubSection admin = new MessageAdminSubSection(); public MessagesPlayerSubSection player = new MessagesPlayerSubSection(); - public MessageWithdrawSubSection withdraw = new MessageWithdrawSubSection(); + public WithdrawMessageConfig withdraw = new WithdrawMessageConfig(); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageWithdrawSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java similarity index 75% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageWithdrawSubSection.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java index 5a11ff10..2ed78e9c 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageWithdrawSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java @@ -1,15 +1,15 @@ -package com.eternalcode.economy.config.implementation.messages; +package com.eternalcode.economy.withdraw; import com.eternalcode.multification.notice.Notice; import eu.okaeri.configs.OkaeriConfig; -public class MessageWithdrawSubSection extends OkaeriConfig { - public Notice noItem = Notice.chat( - "ECONOMY You must hold item to create paycheck!" +public class WithdrawMessageConfig extends OkaeriConfig { + public Notice noItemHeld = Notice.chat( + "ECONOMY You must hold an item to create a banknote!" ); - public Notice noCheck = Notice.chat( - "ECONOMY You must hold an paycheck to redeem it!" + public Notice noBanknoteInHand = Notice.chat( + "ECONOMY You must hold a banknote in your hand to redeem it!" ); public Notice itemSet = Notice.chat( diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java index a4a0270f..db2c284e 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java @@ -59,7 +59,7 @@ public void setItem(Player player) { if (item.getType() == Material.AIR) { noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.noItem) + .notice(messageConfig -> messageConfig.withdraw.noItemHeld) .player(player.getUniqueId()) .send(); @@ -92,6 +92,7 @@ public void setItem(Player player) { public void addBanknote(UUID uuid, BigDecimal value) { Player player = server.getPlayer(uuid); + if (player == null) { return; } @@ -122,7 +123,7 @@ else if (itemInOffHand.isSimilar(item)) { } else { noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.noCheck) + .notice(messageConfig -> messageConfig.withdraw.noBanknoteInHand) .player(player.getUniqueId()) .send(); return; @@ -146,7 +147,7 @@ else if (itemInOffHand.isSimilar(item)) { } private ItemStack setUpItem(BigDecimal value) { - ItemStack item = this.config.currencyItem.item; + ItemStack item = this.config.currencyItem.item.clone(); ItemMeta meta = Objects.requireNonNull(item.getItemMeta()); String displayName = this.config.currencyItem.name From dc22d8b88b03a306198b75b19baeb4bc78dd744e Mon Sep 17 00:00:00 2001 From: Qbiter Date: Mon, 13 Oct 2025 16:19:35 +0200 Subject: [PATCH 06/17] Refactor withdraw system: update plugin metadata handling, adjust item setup logic, and improve inventory interaction messages --- buildSrc/src/main/kotlin/Versions.kt | 2 +- eternaleconomy-api/build.gradle.kts | 2 +- eternaleconomy-core/build.gradle.kts | 18 ++++-- .../economy/EconomyBukkitPlugin.java | 18 +++--- .../economy/bridge/BridgeManager.java | 9 +-- .../PlaceholderEconomyExpansion.java | 14 ++--- .../argument/PriceArgumentResolver.java | 2 +- .../cooldown/CommandCooldownMessage.java | 2 +- .../economy/multification/NoticeService.java | 17 ++--- .../economy/withdraw/WithdrawItemService.java | 39 ++++++++++-- .../withdraw/WithdrawMessageConfig.java | 10 ++- .../economy/withdraw/WithdrawService.java | 63 ++++++------------- .../controller/WithdrawAnvilController.java | 23 ++++++- .../controller/WithdrawController.java | 10 ++- 14 files changed, 128 insertions(+), 101 deletions(-) diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index a3fd9288..d2744035 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -1,6 +1,6 @@ object Versions { - const val SPIGOT_API = "1.19.4-R0.1-SNAPSHOT" + const val PAPER_API = "1.19.4-R0.1-SNAPSHOT" const val OKAERI_CONFIGS = "5.0.5" const val LITE_COMMANDS = "3.10.5" diff --git a/eternaleconomy-api/build.gradle.kts b/eternaleconomy-api/build.gradle.kts index 73f32897..c82efc96 100644 --- a/eternaleconomy-api/build.gradle.kts +++ b/eternaleconomy-api/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } dependencies { - compileOnly("org.spigotmc:spigot-api:${Versions.SPIGOT_API}") + compileOnly("org.spigotmc:spigot-api:${Versions.PAPER_API}") api("org.jetbrains:annotations:${Versions.JETBRAINS_ANNOTATIONS}") } diff --git a/eternaleconomy-core/build.gradle.kts b/eternaleconomy-core/build.gradle.kts index f1ff9312..b55efb74 100644 --- a/eternaleconomy-core/build.gradle.kts +++ b/eternaleconomy-core/build.gradle.kts @@ -11,12 +11,20 @@ plugins { id("me.champeau.jmh") } + +repositories { + maven { + name = "papermc" + url = uri("https://repo.papermc.io/repository/maven-public/") + } +} + dependencies { // api module implementation(project(":eternaleconomy-api")) - // spigot-api - compileOnly("org.spigotmc:spigot-api:${Versions.SPIGOT_API}") + // paper-api + compileOnly("io.papermc.paper:paper-api:${Versions.PAPER_API}") // eternalcode commons implementation("com.eternalcode:eternalcode-commons-adventure:${Versions.ETERNALCODE_COMMONS}") @@ -40,8 +48,8 @@ dependencies { implementation("com.eternalcode:multification-okaeri:${Versions.MULTIFICATION}") // kyori - implementation("net.kyori:adventure-platform-bukkit:${Versions.ADVENTURE_PLATFORM_BUKKIT}") - implementation("net.kyori:adventure-text-minimessage:${Versions.ADVENTURE_API}") +// implementation("net.kyori:adventure-platform-bukkit:${Versions.ADVENTURE_PLATFORM_BUKKIT}") +// implementation("net.kyori:adventure-text-minimessage:${Versions.ADVENTURE_API}") // vault compileOnly("com.github.MilkBowl:VaultAPI:${Versions.VAULT_API}") @@ -107,7 +115,7 @@ tasks.shadowJar { "eu.okaeri", "panda", "org.yaml", - "net.kyori", +// "net.kyori", "com.eternalcode.commons", "net.jodah", ).forEach { relocate(it, prefix) } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 84eae7c6..7c283ce4 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -58,8 +58,6 @@ import java.io.File; import java.math.BigDecimal; import java.time.Duration; -import net.kyori.adventure.platform.AudienceProvider; -import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.minimessage.MiniMessage; import net.milkbowl.vault.economy.Economy; import org.bukkit.Server; @@ -72,7 +70,7 @@ public class EconomyBukkitPlugin extends JavaPlugin { private static final String PLUGIN_STARTED = "EternalEconomy has been enabled in %dms."; - private AudienceProvider audienceProvider; + // private AudienceProvider audienceProvider; private DatabaseManager databaseManager; private LiteCommands liteCommands; @@ -82,7 +80,7 @@ public void onEnable() { Stopwatch started = Stopwatch.createStarted(); Server server = this.getServer(); - this.audienceProvider = BukkitAudiences.create(this); + // this.audienceProvider = BukkitAudiences.create(this); MiniMessage miniMessage = MiniMessage.builder() .postProcessor(new AdventureUrlPostProcessor()) .postProcessor(new AdventureLegacyColorPostProcessor()) @@ -97,7 +95,7 @@ public void onEnable() { CommandsConfig commandsConfig = configService.create(CommandsConfig.class, new File(dataFolder, "commands.yml")); - NoticeService noticeService = new NoticeService(messageConfig, this.audienceProvider, miniMessage); + NoticeService noticeService = new NoticeService(messageConfig, miniMessage); Scheduler scheduler = EconomySchedulerAdapter.getAdaptiveScheduler(this); @@ -110,7 +108,7 @@ public void onEnable() { DecimalFormatter decimalFormatter = new DecimalFormatterImpl(pluginConfig); AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); - WithdrawItemService withdrawItemService = new WithdrawItemService(this); + WithdrawItemService withdrawItemService = new WithdrawItemService(this, pluginConfig, decimalFormatter, miniMessage); WithdrawService withdrawService = new WithdrawService( server, noticeService, pluginConfig, decimalFormatter, withdrawItemService, accountPaymentService, accountManager, miniMessage); @@ -174,7 +172,7 @@ public void onEnable() { server.getPluginManager().registerEvents(new WithdrawAnvilController(withdrawItemService, noticeService), this); BridgeManager bridgeManager = new BridgeManager( - this.getDescription(), + this.getPluginMeta(), accountManager, decimalFormatter, server, @@ -189,9 +187,9 @@ public void onEnable() { @Override public void onDisable() { - if (this.audienceProvider != null) { - this.audienceProvider.close(); - } + // if (this.audienceProvider != null) { + // this.audienceProvider.close(); + // } if (this.liteCommands != null) { this.liteCommands.unregister(); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/BridgeManager.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/BridgeManager.java index b424583f..9e1d5f54 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/BridgeManager.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/BridgeManager.java @@ -3,6 +3,7 @@ import com.eternalcode.economy.account.AccountManager; import com.eternalcode.economy.bridge.placeholderapi.PlaceholderEconomyExpansion; import com.eternalcode.economy.format.DecimalFormatter; +import io.papermc.paper.plugin.configuration.PluginMeta; import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.Server; @@ -12,7 +13,7 @@ public class BridgeManager { - private final PluginDescriptionFile pluginDescriptionFile; + private final PluginMeta pluginMeta; private final AccountManager accountManager; private final DecimalFormatter decimalFormatter; @@ -22,14 +23,14 @@ public class BridgeManager { private final Logger logger; public BridgeManager( - PluginDescriptionFile pluginDescriptionFile, + PluginMeta pluginMeta, AccountManager accountManager, DecimalFormatter decimalFormatter, Server server, Plugin plugin, Logger logger ) { - this.pluginDescriptionFile = pluginDescriptionFile; + this.pluginMeta = pluginMeta; this.accountManager = accountManager; this.decimalFormatter = decimalFormatter; this.server = server; @@ -44,7 +45,7 @@ public void init() { Bukkit.getScheduler().runTask(this.plugin, () -> { this.setupBridge("PlaceholderAPI", () -> { PlaceholderEconomyExpansion placeholderEconomyExpansion = new PlaceholderEconomyExpansion( - this.pluginDescriptionFile, + this.pluginMeta, this.accountManager, this.decimalFormatter ); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/placeholderapi/PlaceholderEconomyExpansion.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/placeholderapi/PlaceholderEconomyExpansion.java index 31eac73e..b74e4817 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/placeholderapi/PlaceholderEconomyExpansion.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/bridge/placeholderapi/PlaceholderEconomyExpansion.java @@ -4,42 +4,42 @@ import com.eternalcode.economy.account.AccountManager; import com.eternalcode.economy.bridge.BridgeInitializer; import com.eternalcode.economy.format.DecimalFormatter; +import io.papermc.paper.plugin.configuration.PluginMeta; import java.util.UUID; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.OfflinePlayer; -import org.bukkit.plugin.PluginDescriptionFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class PlaceholderEconomyExpansion extends PlaceholderExpansion implements BridgeInitializer { - private final PluginDescriptionFile pluginDescriptionFile; + private final PluginMeta pluginMeta; private final AccountManager accountManager; private final DecimalFormatter decimalFormatter; public PlaceholderEconomyExpansion( - PluginDescriptionFile pluginDescriptionFile, + PluginMeta pluginMeta, AccountManager accountManager, DecimalFormatter decimalFormatter ) { - this.pluginDescriptionFile = pluginDescriptionFile; + this.pluginMeta = pluginMeta; this.accountManager = accountManager; this.decimalFormatter = decimalFormatter; } @Override public @NotNull String getIdentifier() { - return this.pluginDescriptionFile.getName().toLowerCase(); + return this.pluginMeta.getName().toLowerCase(); } @Override public @NotNull String getAuthor() { - return this.pluginDescriptionFile.getAuthors().get(0); + return this.pluginMeta.getAuthors().get(0); } @Override public @NotNull String getVersion() { - return this.pluginDescriptionFile.getVersion(); + return this.pluginMeta.getVersion(); } @Override diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java index 65db2e24..f3245aec 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java @@ -67,7 +67,7 @@ protected ParseResult parse( value = value.multiply(multiplier); } - if (value.compareTo(BigDecimal.ZERO) <= 0) { + if (value.compareTo(BigDecimal.ZERO) <= 0.01) { return ParseResult.failure(this.messages.priceNeedToBeGreaterThanZero); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java index 9dcd914c..6a55f010 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/cooldown/CommandCooldownMessage.java @@ -23,7 +23,7 @@ public CommandCooldownMessage(NoticeService noticeService, CommandsConfig comman @Override public Object get(Invocation invocation, CooldownState cooldownState) { - CommandCooldownConfig cooldown = commandsConfig.cooldowns.get(cooldownState.getCooldownContext().getKey()); + CommandCooldownConfig cooldown = commandsConfig.cooldowns.get(cooldownState.getKey()); if (cooldown == null) { return LiteMessages.COMMAND_COOLDOWN.getDefaultMessage(cooldownState); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/multification/NoticeService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/multification/NoticeService.java index 8d62a952..b9a855d5 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/multification/NoticeService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/multification/NoticeService.java @@ -4,23 +4,20 @@ import com.eternalcode.multification.adventure.AudienceConverter; import com.eternalcode.multification.bukkit.BukkitMultification; import com.eternalcode.multification.translation.TranslationProvider; -import net.kyori.adventure.platform.AudienceProvider; +import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.ComponentSerializer; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; public class NoticeService extends BukkitMultification { private final MessageConfig messageConfig; - private final AudienceProvider audienceProvider; private final MiniMessage miniMessage; - public NoticeService(MessageConfig messageConfig, AudienceProvider audienceProvider, MiniMessage miniMessage) { + public NoticeService(MessageConfig messageConfig, MiniMessage miniMessage) { this.messageConfig = messageConfig; - this.audienceProvider = audienceProvider; this.miniMessage = miniMessage; } @@ -36,12 +33,6 @@ public NoticeService(MessageConfig messageConfig, AudienceProvider audienceProvi @Override protected @NotNull AudienceConverter audienceConverter() { - return commandSender -> { - if (commandSender instanceof Player player) { - return this.audienceProvider.player(player.getUniqueId()); - } - - return this.audienceProvider.console(); - }; + return Audience::audience; } -} \ No newline at end of file +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java index 25ab145d..07f0501d 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java @@ -1,6 +1,12 @@ package com.eternalcode.economy.withdraw; +import com.eternalcode.economy.config.implementation.PluginConfig; +import com.eternalcode.economy.format.DecimalFormatter; import java.math.BigDecimal; +import java.util.Objects; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -8,20 +14,28 @@ import org.bukkit.plugin.Plugin; public class WithdrawItemService { + private final PluginConfig config; + private final DecimalFormatter decimalFormatter; + private final MiniMessage miniMessage; + private final NamespacedKey amountKey; - public WithdrawItemService(Plugin plugin) { + public WithdrawItemService( + Plugin plugin, PluginConfig config, DecimalFormatter decimalFormatter, + MiniMessage miniMessage) { this.amountKey = new NamespacedKey(plugin, "economy_withdraw_value"); + + this.config = config; + this.decimalFormatter = decimalFormatter; + this.miniMessage = miniMessage; } public ItemStack markAsBanknote(ItemStack item, BigDecimal amount) { ItemStack taggedItem = item.clone(); - ItemMeta meta = taggedItem.getItemMeta(); - if (meta != null) { + taggedItem.editMeta(meta -> { meta.getPersistentDataContainer().set(amountKey, PersistentDataType.STRING, amount.toPlainString()); - taggedItem.setItemMeta(meta); - } + }); return taggedItem; } @@ -48,4 +62,19 @@ public boolean isBanknote(ItemStack item) { return meta.getPersistentDataContainer().has(amountKey, PersistentDataType.STRING); } + + public ItemStack setUpItem(BigDecimal value) { + ItemStack item = this.config.currencyItem.item.clone(); + + String displayName = this.config.currencyItem.name + .replace("{VALUE}", decimalFormatter.format(value)); + + Component component = miniMessage.deserialize(displayName).decoration(TextDecoration.ITALIC, false); + + item.editMeta(meta -> { + meta.displayName(component); + }); + + return item; + } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java index 2ed78e9c..15cd4bed 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java @@ -24,8 +24,12 @@ public class WithdrawMessageConfig extends OkaeriConfig { "ECONOMY You have redeemed your banknote worth {VALUE}!" ); - public Notice anvilInteract = Notice.chat( - "ECONOMY You cannot rename the " - + "banknote item in an anvil!" + public Notice inventoryInteract = Notice.chat( + "ECONOMY You cannot use the " + + "banknote item in an anvil or crafting!" + ); + + public Notice noSpace = Notice.chat( + "ECONOMY You do not have enough space in your inventory to withdraw a banknote!" ); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java index db2c284e..f3746f11 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java @@ -14,6 +14,7 @@ import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -68,17 +69,12 @@ public void setItem(Player player) { this.config.currencyItem.item = item; - ItemMeta meta = item.getItemMeta(); + Component displayName = Objects.requireNonNull(item.getItemMeta()).displayName(); - if (meta != null) { - String displayName = meta.getDisplayName(); - - if (displayName.isEmpty()) { - this.config.currencyItem.name = item.getType().name(); - } - else { - this.config.currencyItem.name = displayName; - } + if (displayName != null) { + this.config.currencyItem.name = PlainTextComponentSerializer.plainText().serialize(displayName); + } else { + this.config.currencyItem.name = item.getType().name(); } CompletableFuture.runAsync(this.config::save); @@ -97,7 +93,15 @@ public void addBanknote(UUID uuid, BigDecimal value) { return; } - ItemStack item = withdrawItemService.markAsBanknote(setUpItem(value), value); + if(player.getInventory().firstEmpty() == -1) { + noticeService.create() + .notice(messageConfig -> messageConfig.withdraw.noSpace) + .player(player.getUniqueId()) + .send(); + return; + } + + ItemStack item = withdrawItemService.markAsBanknote(withdrawItemService.setUpItem(value), value); player.getInventory().addItem(item); Account account = accountManager.getAccount(player.getUniqueId()); @@ -110,18 +114,8 @@ public void addBanknote(UUID uuid, BigDecimal value) { .send(); } - public void redeem(Player player, ItemStack item, BigDecimal value) { - ItemStack activeItem; - ItemStack itemInHand = player.getInventory().getItemInMainHand(); - ItemStack itemInOffHand = player.getInventory().getItemInOffHand(); - - if (itemInHand.isSimilar(item)) { - activeItem = itemInHand; - } - else if (itemInOffHand.isSimilar(item)) { - activeItem = itemInOffHand; - } - else { + public void redeem(Player player, ItemStack item, BigDecimal value, int amount) { + if(item.getType() == Material.AIR) { noticeService.create() .notice(messageConfig -> messageConfig.withdraw.noBanknoteInHand) .player(player.getUniqueId()) @@ -129,12 +123,9 @@ else if (itemInOffHand.isSimilar(item)) { return; } - player.getInventory().removeItem(activeItem); - activeItem.setAmount(activeItem.getAmount() - item.getAmount()); - player.getInventory().addItem(activeItem); - player.updateInventory(); + item.setAmount(item.getAmount() - amount); - BigDecimal finalValue = value.multiply(BigDecimal.valueOf(item.getAmount())); + BigDecimal finalValue = value.multiply(BigDecimal.valueOf(amount)); Account account = accountManager.getAccount(player.getUniqueId()); accountPaymentService.addBalance(account, finalValue); @@ -145,20 +136,4 @@ else if (itemInOffHand.isSimilar(item)) { .player(player.getUniqueId()) .send(); } - - private ItemStack setUpItem(BigDecimal value) { - ItemStack item = this.config.currencyItem.item.clone(); - ItemMeta meta = Objects.requireNonNull(item.getItemMeta()); - - String displayName = this.config.currencyItem.name - .replace("{VALUE}", decimalFormatter.format(value)); - - Component component = miniMessage.deserialize(displayName).decoration(TextDecoration.ITALIC, false); - String legacyName = LegacyComponentSerializer.legacySection().serialize(component); - - meta.setDisplayName(legacyName); - item.setItemMeta(meta); - - return item; - } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java index 2c68a00e..71922f42 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java @@ -7,7 +7,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.inventory.AnvilInventory; +import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -24,9 +26,8 @@ public WithdrawAnvilController(WithdrawItemService withdrawItemService, NoticeSe @EventHandler(priority = EventPriority.MONITOR) public void onAnvilUse(InventoryClickEvent event) { Inventory topInventory = event.getInventory(); - Inventory clickedInventory = event.getClickedInventory(); - if (!(event.getView().getTopInventory() instanceof AnvilInventory anvilInventory)) { + if (!(topInventory instanceof AnvilInventory) && !(topInventory instanceof CraftingInventory)) { return; } @@ -44,9 +45,25 @@ public void onAnvilUse(InventoryClickEvent event) { event.getView().close(); this.noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.anvilInteract) + .notice(messageConfig -> messageConfig.withdraw.inventoryInteract) .viewer(event.getWhoClicked()) .send(); } } + + // @EventHandler + // public void onPlayerDrag(InventoryDragEvent event) { + // Inventory topInventory = event.getView().getTopInventory(); + // + // if (!(topInventory instanceof AnvilInventory) && !(topInventory instanceof CraftingInventory)) { + // return; + // } + // event.get + // int topSize = topInventory.getSize(); + // for (int rawSlot : event.getRawSlots()) { + // if (rawSlot < topSize) { + // event.setCancelled(true); + // } + // } + // } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java index 205043fe..d69e75b3 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java @@ -7,7 +7,11 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.AnvilInventory; +import org.bukkit.inventory.CraftingInventory; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; public class WithdrawController implements Listener { @@ -42,12 +46,12 @@ public void onItemUse(PlayerInteractEvent event) { event.setCancelled(true); - ItemStack itemToRedeem = item.clone(); + int itemAmount = item.getAmount(); if (!player.isSneaking()) { - itemToRedeem.setAmount(1); + itemAmount = 1; } - this.withdrawService.redeem(player, itemToRedeem, value); + this.withdrawService.redeem(player, item, value, itemAmount); } } From de005b7707ffaef53fdef93448c2d76676dbf24b Mon Sep 17 00:00:00 2001 From: Qbiter Date: Mon, 13 Oct 2025 16:27:00 +0200 Subject: [PATCH 07/17] Fix price validation logic in PriceArgumentResolver: update threshold to ensure prices are greater than 0.09 --- .../economy/command/argument/PriceArgumentResolver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java index f3245aec..b50fc009 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java @@ -67,7 +67,7 @@ protected ParseResult parse( value = value.multiply(multiplier); } - if (value.compareTo(BigDecimal.ZERO) <= 0.01) { + if (value.compareTo(BigDecimal.valueOf(0.09)) <= 0.09) { return ParseResult.failure(this.messages.priceNeedToBeGreaterThanZero); } From e84550cebf1af304f9f8ea47488993c32f11ebe6 Mon Sep 17 00:00:00 2001 From: Qbiter Date: Mon, 13 Oct 2025 16:29:25 +0200 Subject: [PATCH 08/17] Refactor command argument handling: update amount argument to use PriceArgumentResolver for consistency across commands --- .../economy/command/impl/MoneyTransferCommand.java | 4 +++- .../economy/command/impl/admin/AdminAddCommand.java | 4 +++- .../economy/command/impl/admin/AdminRemoveCommand.java | 4 +++- .../economy/command/impl/admin/AdminSetCommand.java | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java index a1175b83..08e68471 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java @@ -3,11 +3,13 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; +import com.eternalcode.economy.command.argument.PriceArgumentResolver; import com.eternalcode.economy.command.validator.notsender.NotSender; import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.argument.Key; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; @@ -37,7 +39,7 @@ public MoneyTransferCommand( } @Execute - void execute(@Context Account payer, @Arg @NotSender Account receiver, @Arg @Positive BigDecimal amount) { + void execute(@Context Account payer, @Arg @NotSender Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { if (payer.balance().compareTo(amount) < 1) { BigDecimal subtract = amount.subtract(payer.balance()); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java index 0cd2b242..214d854b 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java @@ -3,9 +3,11 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; +import com.eternalcode.economy.command.argument.PriceArgumentResolver; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.argument.Key; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; @@ -33,7 +35,7 @@ public AdminAddCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Positive BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { this.accountPaymentService.addBalance(receiver, amount); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java index bfb314c1..545e2d6a 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java @@ -3,9 +3,11 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; +import com.eternalcode.economy.command.argument.PriceArgumentResolver; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.argument.Key; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; @@ -33,7 +35,7 @@ public AdminRemoveCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Positive BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { if (receiver.balance().compareTo(amount) < 0) { BigDecimal subtract = amount.subtract(receiver.balance()); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java index bd85ab91..95db40f1 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java @@ -3,9 +3,11 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; +import com.eternalcode.economy.command.argument.PriceArgumentResolver; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.argument.Key; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; @@ -33,7 +35,7 @@ public AdminSetCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Positive BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { this.accountPaymentService.setBalance(receiver, amount); this.noticeService.create() From e5580539622f0b9edd61d0c2f7114093d8eb3ae7 Mon Sep 17 00:00:00 2001 From: Qbiter Date: Mon, 13 Oct 2025 16:32:49 +0200 Subject: [PATCH 09/17] Refactor AdminSetCommand: improve method parameter formatting for better readability --- .../economy/command/impl/admin/AdminSetCommand.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java index 95db40f1..487e4acb 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java @@ -12,7 +12,6 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; -import jakarta.validation.constraints.Positive; import java.math.BigDecimal; import org.bukkit.command.CommandSender; @@ -35,7 +34,10 @@ public AdminSetCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { + void execute( + @Context CommandSender sender, + @Arg Account receiver, + @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { this.accountPaymentService.setBalance(receiver, amount); this.noticeService.create() From 292a84774da7ad442b74c67a549015f0cff34540 Mon Sep 17 00:00:00 2001 From: Qbiter Date: Mon, 13 Oct 2025 22:34:31 +0200 Subject: [PATCH 10/17] Refactor WithdrawItemService: ensure default item type is set to PAPER if currency item is AIR --- .../eternalcode/economy/withdraw/WithdrawItemService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java index 07f0501d..741d3dd0 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java @@ -3,10 +3,10 @@ import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.format.DecimalFormatter; import java.math.BigDecimal; -import java.util.Objects; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -66,6 +66,10 @@ public boolean isBanknote(ItemStack item) { public ItemStack setUpItem(BigDecimal value) { ItemStack item = this.config.currencyItem.item.clone(); + if (item.getType() == Material.AIR) { + item.setType(Material.PAPER); + } + String displayName = this.config.currencyItem.name .replace("{VALUE}", decimalFormatter.format(value)); From 2aba885fb9192563822b4214f865f6e9e1b98f3d Mon Sep 17 00:00:00 2001 From: Qbiter Date: Thu, 16 Oct 2025 00:14:20 +0200 Subject: [PATCH 11/17] Implement WithdrawItemService interface and create WithdrawItemServiceImpl class for banknote handling. Add ConfigItem object to handle withdraw item properly --- .../economy/EconomyBukkitPlugin.java | 11 ++- .../argument/PriceArgumentResolver.java | 9 +- .../messages/MessageAdminSubSection.java | 4 +- .../messages/MessageConfig.java | 15 ++- .../messages/MessagesPlayerSubSection.java | 28 +++--- .../economy/config/item/ConfigItem.java | 90 ++++++++++++++++++ .../economy/withdraw/WithdrawItemService.java | 82 +---------------- .../withdraw/WithdrawItemServiceImpl.java | 92 +++++++++++++++++++ .../withdraw/WithdrawMessageConfig.java | 30 +++--- .../economy/withdraw/WithdrawService.java | 36 +++++--- .../controller/WithdrawAnvilController.java | 20 +--- .../controller/WithdrawController.java | 4 - 12 files changed, 262 insertions(+), 159 deletions(-) create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 7c283ce4..c432bd40 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -41,7 +41,7 @@ import com.eternalcode.economy.multification.NoticeService; import com.eternalcode.economy.vault.VaultEconomyProvider; import com.eternalcode.economy.withdraw.WithdrawCommand; -import com.eternalcode.economy.withdraw.WithdrawItemService; +import com.eternalcode.economy.withdraw.WithdrawItemServiceImpl; import com.eternalcode.economy.withdraw.WithdrawService; import com.eternalcode.economy.withdraw.WithdrawSetItemCommand; import com.eternalcode.economy.withdraw.controller.WithdrawAnvilController; @@ -108,10 +108,11 @@ public void onEnable() { DecimalFormatter decimalFormatter = new DecimalFormatterImpl(pluginConfig); AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); - WithdrawItemService withdrawItemService = new WithdrawItemService(this, pluginConfig, decimalFormatter, miniMessage); + WithdrawItemServiceImpl + withdrawItemServiceImpl = new WithdrawItemServiceImpl(this, pluginConfig, decimalFormatter, miniMessage); WithdrawService withdrawService = new WithdrawService( server, noticeService, pluginConfig, decimalFormatter, - withdrawItemService, accountPaymentService, accountManager, miniMessage); + withdrawItemServiceImpl, accountPaymentService, accountManager, miniMessage); VaultEconomyProvider vaultEconomyProvider = new VaultEconomyProvider(this, decimalFormatter, accountPaymentService, accountManager); @@ -168,8 +169,8 @@ public void onEnable() { server.getPluginManager().registerEvents(new AccountController(accountManager), this); - server.getPluginManager().registerEvents(new WithdrawController(withdrawService, withdrawItemService), this); - server.getPluginManager().registerEvents(new WithdrawAnvilController(withdrawItemService, noticeService), this); + server.getPluginManager().registerEvents(new WithdrawController(withdrawService, withdrawItemServiceImpl), this); + server.getPluginManager().registerEvents(new WithdrawAnvilController(withdrawItemServiceImpl, noticeService), this); BridgeManager bridgeManager = new BridgeManager( this.getPluginMeta(), diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java index b50fc009..4f8c28d5 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java @@ -6,17 +6,18 @@ import dev.rollczi.litecommands.argument.parser.ParseResult; import dev.rollczi.litecommands.argument.resolver.ArgumentResolver; import dev.rollczi.litecommands.invocation.Invocation; -import org.bukkit.command.CommandSender; - import java.math.BigDecimal; import java.math.RoundingMode; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.bukkit.command.CommandSender; public class PriceArgumentResolver extends ArgumentResolver { + public static final String KEY = "price"; + private final Pattern pricePattern; private final PluginConfig config; @@ -72,8 +73,8 @@ protected ParseResult parse( } return ParseResult.success(value.setScale(2, RoundingMode.DOWN)); - - } catch (NumberFormatException exception) { + } + catch (NumberFormatException exception) { return ParseResult.failure(this.messages.invalidPrice); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java index f7c581dd..61adcb04 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java @@ -14,7 +14,7 @@ public class MessageAdminSubSection extends OkaeriConfig { public Notice added = Notice.chat("ECONOMY " + "Added {AMOUNT} to " + "{PLAYER}."); - public Notice removed = Notice.chat("ECONOMY " + public Notice removed = Notice.chat("ECONOMY " + " Removed {AMOUNT} from " + "{PLAYER}."); public Notice set = Notice.chat("ECONOMY " @@ -22,7 +22,7 @@ public class MessageAdminSubSection extends OkaeriConfig { + "{AMOUNT}."); public Notice reset = Notice.chat("ECONOMY " + "Reset {PLAYER}'s balance."); - public Notice balance = Notice.chat("ECONOMY " + public Notice balance = Notice.chat("ECONOMY " + " {PLAYER}'s balance is " + "{BALANCE}."); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java index 3ac910b6..45a5ca51 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java @@ -8,24 +8,23 @@ public class MessageConfig extends OkaeriConfig { public Notice positiveNumberRequired = Notice.chat( - "ECONOMY Invalid '{AMOUNT}' value, positive number required!"); + "ECONOMY " + "Invalid '{AMOUNT}' value, positive number required!"); public Notice invalidPlayer = Notice.chat( - "ECONOMY Invalid player, please provide a valid player."); + "ECONOMY " + "Invalid player, please provide a valid player."); public Notice notSender = Notice.chat( - "ECONOMY You cannot perform this action on yourself."); + "ECONOMY " + "You cannot perform this action on yourself."); public Notice correctUsage = - Notice.chat("ECONOMY Correct usage:"); + Notice.chat("ECONOMY " + "Correct usage:"); public Notice correctUsageHeader = Notice.chat(" &fCorrect usage:"); public Notice correctUsageEntry = Notice.chat(" &f{USAGE}"); public Notice missingPermission = Notice.chat( - "ECONOMY Missing permission: {PERMISSION}."); + "ECONOMY " + "Missing permission: {PERMISSION}."); public Notice invalidPrice = - Notice.chat("ECONOMY Invalid price format! Use: 1000, 1k, 1.5k, 1m, etc."); - public Notice priceNeedToBeGreaterThanZero = Notice.chat("ECONOMY " - + " Price must be greater than 0!"); + Notice.chat("ECONOMY " + "Invalid price format! Use: 1000, 1k, 1.5k, 1m, etc."); + public Notice priceNeedToBeGreaterThanZero = Notice.chat("ECONOMY " + "Price must be greater than 0!"); public MessageAdminSubSection admin = new MessageAdminSubSection(); public MessagesPlayerSubSection player = new MessagesPlayerSubSection(); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java index b1677062..1c48554f 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java @@ -14,22 +14,22 @@ public class MessagesPlayerSubSection extends OkaeriConfig { + "Set your balance to {AMOUNT}."); public Notice reset = Notice.chat("ECONOMY " + "Resetted your balance."); - public Notice balance = Notice.chat("ECONOMY " - + " Your balance is {BALANCE}."); + public Notice balance = Notice.chat("ECONOMY " + + "Your balance is {BALANCE}."); public Notice balanceOther = - Notice.chat("ECONOMY " - + " {PLAYER}'s balance is {BALANCE}."); - public Notice insufficientBalance = Notice.chat("ECONOMY " - + " Insufficient funds," + Notice.chat("ECONOMY " + + "{PLAYER}'s balance is {BALANCE}."); + public Notice insufficientBalance = Notice.chat("ECONOMY " + + "Insufficient funds," + " you are missing {MISSING_BALANCE}."); - public Notice transferSuccess = Notice.chat("ECONOMY Successfully transferred {AMOUNT} to " + public Notice transferSuccess = Notice.chat("ECONOMY " + + "Successfully transferred {AMOUNT} to " + "{PLAYER}."); - public Notice transferReceived = Notice.chat("ECONOMY " - + " Received {AMOUNT} from " + public Notice transferReceived = Notice.chat("ECONOMY " + + "Received {AMOUNT} from " + "{PLAYER}."); - public Notice transferLimit = Notice.chat("ECONOMY " - + " Transaction limit is {LIMIT}."); + public Notice transferLimit = Notice.chat("ECONOMY " + + "Transaction limit is {LIMIT}."); @Comment({ "Use {PAGE} placeholder to show the current page number", @@ -65,6 +65,6 @@ public class MessagesPlayerSubSection extends OkaeriConfig { .build(); @Comment("Leaderboard is empty notice") - public Notice leaderboardEmpty = Notice.chat("ECONOMY " - + " Leaderboard is empty :("); + public Notice leaderboardEmpty = Notice.chat("ECONOMY " + + "Leaderboard is empty :("); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java new file mode 100644 index 00000000..d9813936 --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java @@ -0,0 +1,90 @@ +package com.eternalcode.economy.config.item; + +import eu.okaeri.configs.OkaeriConfig; +import java.util.Collections; +import java.util.List; +import net.kyori.adventure.text.Component; +import org.bukkit.Material; + +public class ConfigItem extends OkaeriConfig { + + public String name = "&6Item"; + public List lore = Collections.singletonList("&7This is an item"); + public Material material = Material.PLAYER_HEAD; + public Integer texture = null; + public boolean glow = false; + + public ConfigItem(String name, List lore, Material material, Integer texture, boolean glow) { + this.name = name; + this.lore = lore; + this.material = material; + this.texture = texture; + this.glow = glow; + } + + public ConfigItem() { + + } + + public static Builder builder() { + return new Builder(); + } + + public String name() { + return this.name; + } + + public List lore() { + return this.lore; + } + + public Material material() { + return this.material; + } + + public Integer texture() { + return this.texture; + } + + public boolean glow() { + return this.glow; + } + + public static class Builder { + private final ConfigItem configItem = new ConfigItem(); + + public Builder withName(String name) { + this.configItem.name = name; + + return this; + } + + public Builder withLore(List lore) { + this.configItem.lore = lore; + + return this; + } + + public Builder withMaterial(Material material) { + this.configItem.material = material; + + return this; + } + + public Builder withTexture(Integer texture) { + this.configItem.texture = texture; + + return this; + } + + public Builder withGlow(boolean glow) { + this.configItem.glow = glow; + + return this; + } + + public ConfigItem build() { + return this.configItem; + } + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java index 741d3dd0..7d8b226e 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java @@ -1,84 +1,10 @@ package com.eternalcode.economy.withdraw; -import com.eternalcode.economy.config.implementation.PluginConfig; -import com.eternalcode.economy.format.DecimalFormatter; import java.math.BigDecimal; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.TextDecoration; -import net.kyori.adventure.text.minimessage.MiniMessage; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.persistence.PersistentDataType; -import org.bukkit.plugin.Plugin; -public class WithdrawItemService { - private final PluginConfig config; - private final DecimalFormatter decimalFormatter; - private final MiniMessage miniMessage; - - private final NamespacedKey amountKey; - - public WithdrawItemService( - Plugin plugin, PluginConfig config, DecimalFormatter decimalFormatter, - MiniMessage miniMessage) { - this.amountKey = new NamespacedKey(plugin, "economy_withdraw_value"); - - this.config = config; - this.decimalFormatter = decimalFormatter; - this.miniMessage = miniMessage; - } - - public ItemStack markAsBanknote(ItemStack item, BigDecimal amount) { - ItemStack taggedItem = item.clone(); - - taggedItem.editMeta(meta -> { - meta.getPersistentDataContainer().set(amountKey, PersistentDataType.STRING, amount.toPlainString()); - }); - - return taggedItem; - } - - public BigDecimal getValue(ItemStack item) { - ItemMeta meta = item.getItemMeta(); - if (meta == null) { - return BigDecimal.ZERO; - } - - String amount = meta.getPersistentDataContainer().get(amountKey, PersistentDataType.STRING); - if (amount == null) { - return BigDecimal.ZERO; - } - - return new BigDecimal(amount); - } - - public boolean isBanknote(ItemStack item) { - ItemMeta meta = item.getItemMeta(); - if (meta == null) { - return false; - } - - return meta.getPersistentDataContainer().has(amountKey, PersistentDataType.STRING); - } - - public ItemStack setUpItem(BigDecimal value) { - ItemStack item = this.config.currencyItem.item.clone(); - - if (item.getType() == Material.AIR) { - item.setType(Material.PAPER); - } - - String displayName = this.config.currencyItem.name - .replace("{VALUE}", decimalFormatter.format(value)); - - Component component = miniMessage.deserialize(displayName).decoration(TextDecoration.ITALIC, false); - - item.editMeta(meta -> { - meta.displayName(component); - }); - - return item; - } +public interface WithdrawItemService { + BigDecimal getValue(ItemStack itemStack); + boolean isBanknote(ItemStack itemStack); + ItemStack createBanknote(BigDecimal value); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java new file mode 100644 index 00000000..15a238ed --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java @@ -0,0 +1,92 @@ +package com.eternalcode.economy.withdraw; + +import com.eternalcode.economy.config.implementation.PluginConfig; +import com.eternalcode.economy.config.item.ConfigItem; +import com.eternalcode.economy.format.DecimalFormatter; +import java.math.BigDecimal; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.plugin.Plugin; + +public class WithdrawItemServiceImpl implements WithdrawItemService { + + private final PluginConfig pluginConfig; + private final DecimalFormatter moneyFormatter; + private final MiniMessage miniMessage; + private final NamespacedKey banknoteValueKey; + + public WithdrawItemServiceImpl( + Plugin plugin, + PluginConfig pluginConfig, + DecimalFormatter moneyFormatter, + MiniMessage miniMessage + ) { + this.banknoteValueKey = new NamespacedKey(plugin, "eternaleconomy_withdraw_value"); + this.pluginConfig = pluginConfig; + this.moneyFormatter = moneyFormatter; + this.miniMessage = miniMessage; + } + + @Override + public ItemStack createBanknote(BigDecimal value) { + ItemStack banknoteItem = createBaseBanknoteItem(value); + return attachBanknoteValue(banknoteItem, value); + } + + @Override + public boolean isBanknote(ItemStack itemStack) { + if (itemStack == null || !itemStack.hasItemMeta()) { + return false; + } + ItemMeta itemMeta = itemStack.getItemMeta(); + return itemMeta.getPersistentDataContainer().has(banknoteValueKey, PersistentDataType.STRING); + } + + @Override + public BigDecimal getValue(ItemStack itemStack) { + if (itemStack == null || !itemStack.hasItemMeta()) { + return BigDecimal.ZERO; + } + ItemMeta itemMeta = itemStack.getItemMeta(); + String storedValue = itemMeta.getPersistentDataContainer() + .get(banknoteValueKey, PersistentDataType.STRING); + return storedValue != null ? new BigDecimal(storedValue) : BigDecimal.ZERO; + } + + private ItemStack createBaseBanknoteItem(BigDecimal value) { + ConfigItem configItem = this.pluginConfig.currencyItem.item; + + ItemStack banknoteItem = new ItemStack(configItem.material()); + + banknoteItem.editMeta(meta -> { + meta.setCustomModelData(configItem.texture); + meta.displayName(miniMessage.deserialize(configItem.name.replace("{VALUE}", moneyFormatter.format(value)), TagResolver.empty())); + meta.lore(configItem.lore.stream() + .map(line -> miniMessage.deserialize(line, TagResolver.empty())) + .toList()); + }); + + if (configItem.glow) { + banknoteItem.editMeta(meta -> { + meta.addEnchant(Enchantment.DURABILITY, 1, true); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + }); + } + + return banknoteItem; + } + + private ItemStack attachBanknoteValue(ItemStack banknoteItem, BigDecimal value) { + ItemStack taggedItem = banknoteItem.clone(); + taggedItem.editMeta(meta -> + meta.getPersistentDataContainer().set(banknoteValueKey, PersistentDataType.STRING, value.toPlainString()) + ); + return taggedItem; + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java index 15cd4bed..67e622e2 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java @@ -4,32 +4,38 @@ import eu.okaeri.configs.OkaeriConfig; public class WithdrawMessageConfig extends OkaeriConfig { - public Notice noItemHeld = Notice.chat( - "ECONOMY You must hold an item to create a banknote!" + public Notice noItemInHand = Notice.chat( + "ECONOMY " + + "You must hold an item to create a banknote!" ); public Notice noBanknoteInHand = Notice.chat( - "ECONOMY You must hold a banknote in your hand to redeem it!" + "ECONOMY " + + "You must hold a banknote in your hand to redeem it!" ); - public Notice itemSet = Notice.chat( - "ECONOMY You have set the banknote item to {ITEM}!" + public Notice itemSetSuccess = Notice.chat( + "ECONOMY " + + "You have set the banknote item to {ITEM}!" ); public Notice banknoteWithdrawn = Notice.chat( - "ECONOMY You have withdrawn a banknote worth {VALUE}!" + "ECONOMY " + + "You have withdrawn a banknote worth {VALUE}!" ); public Notice banknoteRedeemed = Notice.chat( - "ECONOMY You have redeemed your banknote worth {VALUE}!" + "ECONOMY " + + "You have redeemed your banknote worth {VALUE}!" ); - public Notice inventoryInteract = Notice.chat( - "ECONOMY You cannot use the " - + "banknote item in an anvil or crafting!" + public Notice invalidInteraction = Notice.chat( + "ECONOMY " + + "You cannot use the banknote item in an anvil or crafting table!" ); - public Notice noSpace = Notice.chat( - "ECONOMY You do not have enough space in your inventory to withdraw a banknote!" + public Notice noInventorySpace = Notice.chat( + "ECONOMY " + + "You do not have enough space in your inventory to withdraw a banknote!" ); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java index f3746f11..71326890 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java @@ -7,19 +7,17 @@ import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import java.math.BigDecimal; +import java.util.List; import java.util.Objects; import java.util.UUID; import java.util.concurrent.CompletableFuture; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.minimessage.MiniMessage; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; public class WithdrawService { private final Server server; @@ -60,28 +58,36 @@ public void setItem(Player player) { if (item.getType() == Material.AIR) { noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.noItemHeld) + .notice(messageConfig -> messageConfig.withdraw.noItemInHand) .player(player.getUniqueId()) .send(); return; } - this.config.currencyItem.item = item; - Component displayName = Objects.requireNonNull(item.getItemMeta()).displayName(); if (displayName != null) { - this.config.currencyItem.name = PlainTextComponentSerializer.plainText().serialize(displayName); - } else { - this.config.currencyItem.name = item.getType().name(); + this.config.currencyItem.item.name = PlainTextComponentSerializer.plainText().serialize(displayName); + } + else { + this.config.currencyItem.item.name = item.getType().name(); } + this.config.currencyItem.item.glow = item.getItemMeta().hasEnchants(); + this.config.currencyItem.item.lore = Objects.requireNonNullElse(item.getItemMeta().lore(), List.of()) + .stream() + .map(miniMessage::serialize) + .toList(); + + this.config.currencyItem.item.material = item.getType(); + this.config.currencyItem.item.texture = item.getItemMeta().getCustomModelData(); + CompletableFuture.runAsync(this.config::save); noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.itemSet) - .placeholder("{ITEM}", this.config.currencyItem.name) + .notice(messageConfig -> messageConfig.withdraw.itemSetSuccess) + .placeholder("{ITEM}", this.config.currencyItem.item.name) .player(player.getUniqueId()) .send(); } @@ -93,15 +99,15 @@ public void addBanknote(UUID uuid, BigDecimal value) { return; } - if(player.getInventory().firstEmpty() == -1) { + if (player.getInventory().firstEmpty() == -1) { noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.noSpace) + .notice(messageConfig -> messageConfig.withdraw.noInventorySpace) .player(player.getUniqueId()) .send(); return; } - ItemStack item = withdrawItemService.markAsBanknote(withdrawItemService.setUpItem(value), value); + ItemStack item = withdrawItemService.createBanknote(value); player.getInventory().addItem(item); Account account = accountManager.getAccount(player.getUniqueId()); @@ -115,7 +121,7 @@ public void addBanknote(UUID uuid, BigDecimal value) { } public void redeem(Player player, ItemStack item, BigDecimal value, int amount) { - if(item.getType() == Material.AIR) { + if (item.getType() == Material.AIR) { noticeService.create() .notice(messageConfig -> messageConfig.withdraw.noBanknoteInHand) .player(player.getUniqueId()) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java index 71922f42..df700142 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java @@ -7,7 +7,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.inventory.AnvilInventory; import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.Inventory; @@ -42,28 +41,15 @@ public void onAnvilUse(InventoryClickEvent event) { } if (this.withdrawItemService.isBanknote(item)) { + event.setCancelled(true); event.getView().close(); this.noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.inventoryInteract) + .notice(messageConfig -> messageConfig.withdraw.invalidInteraction) .viewer(event.getWhoClicked()) .send(); } } - // @EventHandler - // public void onPlayerDrag(InventoryDragEvent event) { - // Inventory topInventory = event.getView().getTopInventory(); - // - // if (!(topInventory instanceof AnvilInventory) && !(topInventory instanceof CraftingInventory)) { - // return; - // } - // event.get - // int topSize = topInventory.getSize(); - // for (int rawSlot : event.getRawSlots()) { - // if (rawSlot < topSize) { - // event.setCancelled(true); - // } - // } - // } + // TODO: Implement blocking item drag into inventory } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java index d69e75b3..b248b500 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java @@ -7,11 +7,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.AnvilInventory; -import org.bukkit.inventory.CraftingInventory; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; public class WithdrawController implements Listener { From 40bf52c84fcf50c5726bab1daca4f4eb7122c5b7 Mon Sep 17 00:00:00 2001 From: Qbiter Date: Thu, 16 Oct 2025 00:14:39 +0200 Subject: [PATCH 12/17] Refactor PluginConfig and command classes: replace ItemStack with ConfigItem for better item handling and improve code readability --- .../config/implementation/PluginConfig.java | 16 +++++++++------- .../economy/withdraw/WithdrawCommand.java | 1 + .../economy/withdraw/WithdrawSetItemCommand.java | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java index af7d9e61..e22ccc7a 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java @@ -1,11 +1,12 @@ package com.eternalcode.economy.config.implementation; +import com.eternalcode.economy.config.item.ConfigItem; import com.eternalcode.economy.database.DatabaseConfig; import com.eternalcode.economy.format.DecimalUnit; import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; +import net.kyori.adventure.text.Component; import org.bukkit.Material; -import org.bukkit.inventory.ItemStack; import java.math.BigDecimal; import java.util.Arrays; @@ -48,11 +49,12 @@ public static class Units extends OkaeriConfig { } public static class CurrencyItem extends OkaeriConfig { - @Comment({"Name of the item", - "{VALUE} - value of the check",}) - public String name = "Check worth {VALUE}$"; - - @Comment("Item") - public ItemStack item = new ItemStack(Material.PAPER); + public ConfigItem item = new ConfigItem( + "Check worth {VALUE}$", + List.of("Right click to redeem"), + Material.PAPER, + null, + true + ); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java index 947722e3..cae0b624 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java @@ -16,6 +16,7 @@ @Command(name = "withdraw", aliases = {"paycheck", "check"}) @Permission(EconomyPermissionConstant.PLAYER_WITHDRAW_PERMISSION) public class WithdrawCommand { + private final WithdrawService withdrawService; private final NoticeService noticeService; private final DecimalFormatter decimalFormatter; diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java index 38f18c75..d46c430b 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java @@ -10,6 +10,7 @@ @Command(name = "economy withdraw setitem") @Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) public class WithdrawSetItemCommand { + private final WithdrawService withdrawService; public WithdrawSetItemCommand(WithdrawService withdrawService) { @@ -18,6 +19,6 @@ public WithdrawSetItemCommand(WithdrawService withdrawService) { @Execute void execute(@Context Player player) { - withdrawService.setItem(player); + this.withdrawService.setItem(player); } } From 94d83d2b17bb0ea9a86b8c59ee5f16bb401f8e4a Mon Sep 17 00:00:00 2001 From: Qbiter Date: Thu, 16 Oct 2025 00:21:38 +0200 Subject: [PATCH 13/17] Refactor WithdrawService: update texture assignment to handle absence of custom model data --- eternaleconomy-core/build.gradle.kts | 6 ------ .../com/eternalcode/economy/withdraw/WithdrawService.java | 3 ++- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/eternaleconomy-core/build.gradle.kts b/eternaleconomy-core/build.gradle.kts index b55efb74..4cdaea1d 100644 --- a/eternaleconomy-core/build.gradle.kts +++ b/eternaleconomy-core/build.gradle.kts @@ -3,7 +3,6 @@ import net.minecrell.pluginyml.bukkit.BukkitPluginDescription plugins { `economy-java` `economy-repositories` -// `economy-checkstyle` id("net.minecrell.plugin-yml.bukkit") id("com.gradleup.shadow") @@ -47,10 +46,6 @@ dependencies { implementation("com.eternalcode:multification-bukkit:${Versions.MULTIFICATION}") implementation("com.eternalcode:multification-okaeri:${Versions.MULTIFICATION}") - // kyori -// implementation("net.kyori:adventure-platform-bukkit:${Versions.ADVENTURE_PLATFORM_BUKKIT}") -// implementation("net.kyori:adventure-text-minimessage:${Versions.ADVENTURE_API}") - // vault compileOnly("com.github.MilkBowl:VaultAPI:${Versions.VAULT_API}") @@ -115,7 +110,6 @@ tasks.shadowJar { "eu.okaeri", "panda", "org.yaml", -// "net.kyori", "com.eternalcode.commons", "net.jodah", ).forEach { relocate(it, prefix) } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java index 71326890..9d8fac35 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java @@ -81,7 +81,8 @@ public void setItem(Player player) { .toList(); this.config.currencyItem.item.material = item.getType(); - this.config.currencyItem.item.texture = item.getItemMeta().getCustomModelData(); + this.config.currencyItem.item.texture = item.getItemMeta().hasCustomModelData() ? + item.getItemMeta().getCustomModelData() : null; CompletableFuture.runAsync(this.config::save); From 55aa9b616ee8d989eb5271028c588e401715d09b Mon Sep 17 00:00:00 2001 From: Qbiter Date: Fri, 17 Oct 2025 21:53:24 +0200 Subject: [PATCH 14/17] Refactor command argument handling: replace PriceArgumentResolver with MoneyFormatArgument for improved money value parsing --- eternaleconomy-api/build.gradle.kts | 3 +- .../economy/EconomyBukkitPlugin.java | 21 +++---- ...Resolver.java => MoneyFormatArgument.java} | 18 +++--- .../command/impl/MoneyTransferCommand.java | 5 +- .../command/impl/admin/AdminAddCommand.java | 5 +- .../impl/admin/AdminRemoveCommand.java | 5 +- .../command/impl/admin/AdminSetCommand.java | 4 +- .../messages/MessageConfig.java | 6 +- .../economy/withdraw/WithdrawCommand.java | 4 +- .../economy/withdraw/WithdrawItemService.java | 2 + .../withdraw/WithdrawItemServiceImpl.java | 57 ++++++++++++++++++- .../economy/withdraw/WithdrawService.java | 50 +--------------- .../withdraw/WithdrawSetItemCommand.java | 8 +-- 13 files changed, 91 insertions(+), 97 deletions(-) rename eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/{PriceArgumentResolver.java => MoneyFormatArgument.java} (78%) diff --git a/eternaleconomy-api/build.gradle.kts b/eternaleconomy-api/build.gradle.kts index c82efc96..fe91090c 100644 --- a/eternaleconomy-api/build.gradle.kts +++ b/eternaleconomy-api/build.gradle.kts @@ -1,12 +1,11 @@ plugins { `economy-java` `economy-repositories` - // `economy-checkstyle` `economy-publish` } dependencies { - compileOnly("org.spigotmc:spigot-api:${Versions.PAPER_API}") + compileOnly("io.papermc.paper:paper-api:${Versions.PAPER_API}") api("org.jetbrains:annotations:${Versions.JETBRAINS_ANNOTATIONS}") } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index c432bd40..15b68c82 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -12,7 +12,7 @@ import com.eternalcode.economy.account.database.AccountRepositoryImpl; import com.eternalcode.economy.bridge.BridgeManager; import com.eternalcode.economy.command.argument.AccountArgument; -import com.eternalcode.economy.command.argument.PriceArgumentResolver; +import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.command.context.AccountContext; import com.eternalcode.economy.command.cooldown.CommandCooldownEditor; import com.eternalcode.economy.command.cooldown.CommandCooldownMessage; @@ -70,7 +70,6 @@ public class EconomyBukkitPlugin extends JavaPlugin { private static final String PLUGIN_STARTED = "EternalEconomy has been enabled in %dms."; - // private AudienceProvider audienceProvider; private DatabaseManager databaseManager; private LiteCommands liteCommands; @@ -80,7 +79,6 @@ public void onEnable() { Stopwatch started = Stopwatch.createStarted(); Server server = this.getServer(); - // this.audienceProvider = BukkitAudiences.create(this); MiniMessage miniMessage = MiniMessage.builder() .postProcessor(new AdventureUrlPostProcessor()) .postProcessor(new AdventureLegacyColorPostProcessor()) @@ -109,10 +107,11 @@ public void onEnable() { AccountPaymentService accountPaymentService = new AccountPaymentService(accountManager, pluginConfig); WithdrawItemServiceImpl - withdrawItemServiceImpl = new WithdrawItemServiceImpl(this, pluginConfig, decimalFormatter, miniMessage); + withdrawItemServiceImpl = new WithdrawItemServiceImpl(this, pluginConfig, decimalFormatter, + noticeService, miniMessage); WithdrawService withdrawService = new WithdrawService( - server, noticeService, pluginConfig, decimalFormatter, - withdrawItemServiceImpl, accountPaymentService, accountManager, miniMessage); + server, noticeService, decimalFormatter, + withdrawItemServiceImpl, accountPaymentService, accountManager); VaultEconomyProvider vaultEconomyProvider = new VaultEconomyProvider(this, decimalFormatter, accountPaymentService, accountManager); @@ -146,7 +145,7 @@ public void onEnable() { new AdminSetCommand(accountPaymentService, decimalFormatter, noticeService), new AdminResetCommand(accountPaymentService, noticeService), new AdminBalanceCommand(noticeService, decimalFormatter), - new WithdrawSetItemCommand(withdrawService), + new WithdrawSetItemCommand(withdrawItemServiceImpl), new WithdrawCommand(withdrawService, noticeService, decimalFormatter), new MoneyBalanceCommand(noticeService, decimalFormatter), new MoneyTransferCommand(accountPaymentService, decimalFormatter, noticeService, pluginConfig), @@ -159,8 +158,8 @@ public void onEnable() { .argument( BigDecimal.class, - ArgumentKey.of(PriceArgumentResolver.KEY), - new PriceArgumentResolver(pluginConfig, messageConfig)) + ArgumentKey.of(MoneyFormatArgument.KEY), + new MoneyFormatArgument(pluginConfig, messageConfig)) .result(Notice.class, new NoticeHandler(noticeService)) .result(NoticeBroadcast.class, new NoticeBroadcastHandler()) @@ -188,10 +187,6 @@ public void onEnable() { @Override public void onDisable() { - // if (this.audienceProvider != null) { - // this.audienceProvider.close(); - // } - if (this.liteCommands != null) { this.liteCommands.unregister(); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java similarity index 78% rename from eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java rename to eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java index 4f8c28d5..b62b4a34 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/PriceArgumentResolver.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java @@ -14,7 +14,7 @@ import java.util.regex.Pattern; import org.bukkit.command.CommandSender; -public class PriceArgumentResolver extends ArgumentResolver { +public class MoneyFormatArgument extends ArgumentResolver { public static final String KEY = "price"; @@ -25,7 +25,7 @@ public class PriceArgumentResolver extends ArgumentResolver multipliers; - public PriceArgumentResolver(PluginConfig config, MessageConfig messages) { + public MoneyFormatArgument(PluginConfig config, MessageConfig messages) { this.config = config; this.messages = messages; this.multipliers = new HashMap<>(); @@ -37,10 +37,6 @@ public PriceArgumentResolver(PluginConfig config, MessageConfig messages) { }); this.pricePattern = Pattern.compile("^(\\d+(?:[.,]\\d+)?)([" + suffixes + "])?$", Pattern.CASE_INSENSITIVE); - - config.units.format.forEach(unit -> { - this.multipliers.put(unit.getSuffix(), BigDecimal.valueOf(unit.getFactor())); - }); } @Override @@ -49,7 +45,7 @@ protected ParseResult parse( Matcher matcher = this.pricePattern.matcher(raw.toLowerCase()); if (!matcher.matches()) { - return ParseResult.failure(this.messages.invalidPrice); + return ParseResult.failure(this.messages.invalidMoney); } String numberPart = matcher.group(1).replace(',', '.'); @@ -62,20 +58,20 @@ protected ParseResult parse( BigDecimal multiplier = multipliers.get(Character.toLowerCase(suffix.charAt(0))); if (multiplier == null) { - return ParseResult.failure(this.messages.invalidPrice); + return ParseResult.failure(this.messages.invalidMoney); } value = value.multiply(multiplier); } - if (value.compareTo(BigDecimal.valueOf(0.09)) <= 0.09) { - return ParseResult.failure(this.messages.priceNeedToBeGreaterThanZero); + if (value.compareTo(BigDecimal.valueOf(0.09)) < 0.09) { + return ParseResult.failure(this.messages.incorrectMoneyArgument); } return ParseResult.success(value.setScale(2, RoundingMode.DOWN)); } catch (NumberFormatException exception) { - return ParseResult.failure(this.messages.invalidPrice); + return ParseResult.failure(this.messages.invalidMoney); } } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java index 08e68471..2ccce09f 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java @@ -3,7 +3,7 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.PriceArgumentResolver; +import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.command.validator.notsender.NotSender; import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.format.DecimalFormatter; @@ -14,7 +14,6 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; -import jakarta.validation.constraints.Positive; import java.math.BigDecimal; @Command(name = "pay", aliases = "transfer") @@ -39,7 +38,7 @@ public MoneyTransferCommand( } @Execute - void execute(@Context Account payer, @Arg @NotSender Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { + void execute(@Context Account payer, @Arg @NotSender Account receiver, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { if (payer.balance().compareTo(amount) < 1) { BigDecimal subtract = amount.subtract(payer.balance()); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java index 214d854b..75b9acb4 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java @@ -3,7 +3,7 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.PriceArgumentResolver; +import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -12,7 +12,6 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; -import jakarta.validation.constraints.Positive; import java.math.BigDecimal; import org.bukkit.command.CommandSender; @@ -35,7 +34,7 @@ public AdminAddCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { this.accountPaymentService.addBalance(receiver, amount); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java index 545e2d6a..a378aa34 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java @@ -3,7 +3,7 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.PriceArgumentResolver; +import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -12,7 +12,6 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; -import jakarta.validation.constraints.Positive; import java.math.BigDecimal; import org.bukkit.command.CommandSender; @@ -35,7 +34,7 @@ public AdminRemoveCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { if (receiver.balance().compareTo(amount) < 0) { BigDecimal subtract = amount.subtract(receiver.balance()); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java index 487e4acb..739a0173 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java @@ -3,7 +3,7 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.PriceArgumentResolver; +import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -37,7 +37,7 @@ public AdminSetCommand( void execute( @Context CommandSender sender, @Arg Account receiver, - @Arg @Key(PriceArgumentResolver.KEY) BigDecimal amount) { + @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { this.accountPaymentService.setBalance(receiver, amount); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java index 45a5ca51..1e58d6fb 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java @@ -22,9 +22,9 @@ public class MessageConfig extends OkaeriConfig { public Notice missingPermission = Notice.chat( "ECONOMY " + "Missing permission: {PERMISSION}."); - public Notice invalidPrice = - Notice.chat("ECONOMY " + "Invalid price format! Use: 1000, 1k, 1.5k, 1m, etc."); - public Notice priceNeedToBeGreaterThanZero = Notice.chat("ECONOMY " + "Price must be greater than 0!"); + public Notice invalidMoney = + Notice.chat("ECONOMY " + "Invalid money value format! Use: 1000, 1k, 1.5k, 1m, etc."); + public Notice incorrectMoneyArgument = Notice.chat("ECONOMY " + "Price must be greater than or equal 0.1!"); public MessageAdminSubSection admin = new MessageAdminSubSection(); public MessagesPlayerSubSection player = new MessagesPlayerSubSection(); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java index cae0b624..c1bd768a 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java @@ -2,7 +2,7 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; -import com.eternalcode.economy.command.argument.PriceArgumentResolver; +import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -32,7 +32,7 @@ public WithdrawCommand( } @Execute - void execute(@Context Account account, @Arg @Key(PriceArgumentResolver.KEY) BigDecimal value) { + void execute(@Context Account account, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal value) { BigDecimal balance = account.balance(); BigDecimal subtract = balance.subtract(value); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java index 7d8b226e..941d178f 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java @@ -1,10 +1,12 @@ package com.eternalcode.economy.withdraw; import java.math.BigDecimal; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; public interface WithdrawItemService { BigDecimal getValue(ItemStack itemStack); boolean isBanknote(ItemStack itemStack); ItemStack createBanknote(BigDecimal value); + void setItem(Player player); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java index 15a238ed..7c7ef1b1 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java @@ -3,11 +3,19 @@ import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.config.item.ConfigItem; import com.eternalcode.economy.format.DecimalFormatter; +import com.eternalcode.economy.multification.NoticeService; import java.math.BigDecimal; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -20,17 +28,20 @@ public class WithdrawItemServiceImpl implements WithdrawItemService { private final DecimalFormatter moneyFormatter; private final MiniMessage miniMessage; private final NamespacedKey banknoteValueKey; + private final NoticeService noticeService; public WithdrawItemServiceImpl( Plugin plugin, PluginConfig pluginConfig, DecimalFormatter moneyFormatter, + NoticeService noticeService, MiniMessage miniMessage ) { - this.banknoteValueKey = new NamespacedKey(plugin, "eternaleconomy_withdraw_value"); + this.banknoteValueKey = new NamespacedKey(plugin, "withdraw_value"); this.pluginConfig = pluginConfig; this.moneyFormatter = moneyFormatter; this.miniMessage = miniMessage; + this.noticeService = noticeService; } @Override @@ -59,6 +70,47 @@ public BigDecimal getValue(ItemStack itemStack) { return storedValue != null ? new BigDecimal(storedValue) : BigDecimal.ZERO; } + @Override + public void setItem(Player player) { + ItemStack item = player.getInventory().getItemInMainHand(); + + if (item.getType() == Material.AIR) { + noticeService.create() + .notice(messageConfig -> messageConfig.withdraw.noItemInHand) + .player(player.getUniqueId()) + .send(); + + return; + } + + Component displayName = Objects.requireNonNull(item.getItemMeta()).displayName(); + + if (displayName != null) { + this.pluginConfig.currencyItem.item.name = PlainTextComponentSerializer.plainText().serialize(displayName); + } + else { + this.pluginConfig.currencyItem.item.name = item.getType().name(); + } + + this.pluginConfig.currencyItem.item.glow = item.getItemMeta().hasEnchants(); + this.pluginConfig.currencyItem.item.lore = Objects.requireNonNullElse(item.getItemMeta().lore(), List.of()) + .stream() + .map(miniMessage::serialize) + .toList(); + + this.pluginConfig.currencyItem.item.material = item.getType(); + this.pluginConfig.currencyItem.item.texture = item.getItemMeta().hasCustomModelData() ? + item.getItemMeta().getCustomModelData() : null; + + CompletableFuture.runAsync(this.pluginConfig::save); + + noticeService.create() + .notice(messageConfig -> messageConfig.withdraw.itemSetSuccess) + .placeholder("{ITEM}", this.pluginConfig.currencyItem.item.name) + .player(player.getUniqueId()) + .send(); + } + private ItemStack createBaseBanknoteItem(BigDecimal value) { ConfigItem configItem = this.pluginConfig.currencyItem.item; @@ -68,7 +120,8 @@ private ItemStack createBaseBanknoteItem(BigDecimal value) { meta.setCustomModelData(configItem.texture); meta.displayName(miniMessage.deserialize(configItem.name.replace("{VALUE}", moneyFormatter.format(value)), TagResolver.empty())); meta.lore(configItem.lore.stream() - .map(line -> miniMessage.deserialize(line, TagResolver.empty())) + .map(line -> miniMessage.deserialize(line.replace("{VALUE}", moneyFormatter.format(value)), + TagResolver.empty())) .toList()); }); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java index 9d8fac35..9645eafb 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java @@ -22,75 +22,27 @@ public class WithdrawService { private final Server server; private final NoticeService noticeService; - private final PluginConfig config; private final WithdrawItemService withdrawItemService; private final DecimalFormatter decimalFormatter; private final AccountPaymentService accountPaymentService; private final AccountManager accountManager; - private final MiniMessage miniMessage; - public WithdrawService( Server server, NoticeService noticeService, - PluginConfig pluginConfig, DecimalFormatter decimalFormatter, WithdrawItemService withdrawItemService, AccountPaymentService accountPaymentService, - AccountManager accountManager, - MiniMessage miniMessage + AccountManager accountManager ) { this.server = server; this.noticeService = noticeService; - this.config = pluginConfig; this.decimalFormatter = decimalFormatter; this.withdrawItemService = withdrawItemService; this.accountPaymentService = accountPaymentService; this.accountManager = accountManager; - - this.miniMessage = miniMessage; - } - - public void setItem(Player player) { - ItemStack item = player.getInventory().getItemInMainHand(); - - if (item.getType() == Material.AIR) { - noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.noItemInHand) - .player(player.getUniqueId()) - .send(); - - return; - } - - Component displayName = Objects.requireNonNull(item.getItemMeta()).displayName(); - - if (displayName != null) { - this.config.currencyItem.item.name = PlainTextComponentSerializer.plainText().serialize(displayName); - } - else { - this.config.currencyItem.item.name = item.getType().name(); - } - - this.config.currencyItem.item.glow = item.getItemMeta().hasEnchants(); - this.config.currencyItem.item.lore = Objects.requireNonNullElse(item.getItemMeta().lore(), List.of()) - .stream() - .map(miniMessage::serialize) - .toList(); - - this.config.currencyItem.item.material = item.getType(); - this.config.currencyItem.item.texture = item.getItemMeta().hasCustomModelData() ? - item.getItemMeta().getCustomModelData() : null; - - CompletableFuture.runAsync(this.config::save); - - noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.itemSetSuccess) - .placeholder("{ITEM}", this.config.currencyItem.item.name) - .player(player.getUniqueId()) - .send(); } public void addBanknote(UUID uuid, BigDecimal value) { diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java index d46c430b..e9b1a2df 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java @@ -11,14 +11,14 @@ @Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) public class WithdrawSetItemCommand { - private final WithdrawService withdrawService; + private final WithdrawItemService withdrawItemService; - public WithdrawSetItemCommand(WithdrawService withdrawService) { - this.withdrawService = withdrawService; + public WithdrawSetItemCommand(WithdrawItemService withdrawItemService) { + this.withdrawItemService = withdrawItemService; } @Execute void execute(@Context Player player) { - this.withdrawService.setItem(player); + this.withdrawItemService.setItem(player); } } From df22aa574ca57a6d9ab1f6814635bca461f6cc16 Mon Sep 17 00:00:00 2001 From: Qbiter Date: Sat, 18 Oct 2025 18:26:24 +0200 Subject: [PATCH 15/17] Refactor message formatting: streamline chat notices for improved readability and consistency --- .../messages/MessageAdminSubSection.java | 22 +++------- .../messages/MessageConfig.java | 14 +++---- .../messages/MessagesPlayerSubSection.java | 42 ++++++------------- .../withdraw/WithdrawMessageConfig.java | 21 ++++------ 4 files changed, 33 insertions(+), 66 deletions(-) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java index 61adcb04..7805414d 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageAdminSubSection.java @@ -8,23 +8,13 @@ public class MessageAdminSubSection extends OkaeriConfig { public Notice insufficientFunds = - Notice.chat("ECONOMY " - + "Player {PLAYER} has insufficient funds, they are missing {MISSING_BALANCE}."); + Notice.chat("ECONOMY Player {PLAYER} has insufficient funds, they are missing {MISSING_BALANCE}."); - public Notice added = Notice.chat("ECONOMY " - + "Added {AMOUNT} to " - + "{PLAYER}."); - public Notice removed = Notice.chat("ECONOMY " - + " Removed {AMOUNT} from " - + "{PLAYER}."); - public Notice set = Notice.chat("ECONOMY " - + "Set {PLAYER}'s balance to " - + "{AMOUNT}."); - public Notice reset = Notice.chat("ECONOMY " - + "Reset {PLAYER}'s balance."); - public Notice balance = Notice.chat("ECONOMY " - + " {PLAYER}'s balance is " - + "{BALANCE}."); + public Notice added = Notice.chat("ECONOMY Added {AMOUNT} to {PLAYER}."); + public Notice removed = Notice.chat("ECONOMY Removed {AMOUNT} from {PLAYER}."); + public Notice set = Notice.chat("ECONOMY Set {PLAYER}'s balance to {AMOUNT}."); + public Notice reset = Notice.chat("ECONOMY Reset {PLAYER}'s balance."); + public Notice balance = Notice.chat("ECONOMY {PLAYER}'s balance is {BALANCE}."); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java index 1e58d6fb..0d1ce57c 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessageConfig.java @@ -8,23 +8,23 @@ public class MessageConfig extends OkaeriConfig { public Notice positiveNumberRequired = Notice.chat( - "ECONOMY " + "Invalid '{AMOUNT}' value, positive number required!"); + "ECONOMY Invalid '{AMOUNT}' value, positive number required!"); public Notice invalidPlayer = Notice.chat( - "ECONOMY " + "Invalid player, please provide a valid player."); + "ECONOMY Invalid player, please provide a valid player."); public Notice notSender = Notice.chat( - "ECONOMY " + "You cannot perform this action on yourself."); + "ECONOMY You cannot perform this action on yourself."); public Notice correctUsage = - Notice.chat("ECONOMY " + "Correct usage:"); + Notice.chat("ECONOMY Correct usage:"); public Notice correctUsageHeader = Notice.chat(" &fCorrect usage:"); public Notice correctUsageEntry = Notice.chat(" &f{USAGE}"); public Notice missingPermission = Notice.chat( - "ECONOMY " + "Missing permission: {PERMISSION}."); + "ECONOMY Missing permission: {PERMISSION}."); public Notice invalidMoney = - Notice.chat("ECONOMY " + "Invalid money value format! Use: 1000, 1k, 1.5k, 1m, etc."); - public Notice incorrectMoneyArgument = Notice.chat("ECONOMY " + "Price must be greater than or equal 0.1!"); + Notice.chat("ECONOMY Invalid money value format! Use: 1000, 1k, 1.5k, 1m, etc."); + public Notice incorrectMoneyArgument = Notice.chat("ECONOMY Price must be greater than or equal 0.1!"); public MessageAdminSubSection admin = new MessageAdminSubSection(); public MessagesPlayerSubSection player = new MessagesPlayerSubSection(); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java index 1c48554f..9d3f293a 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/messages/MessagesPlayerSubSection.java @@ -6,37 +6,23 @@ public class MessagesPlayerSubSection extends OkaeriConfig { - public Notice added = Notice.chat("ECONOMY " - + "Added {AMOUNT} to your account."); - public Notice removed = Notice.chat("ECONOMY " - + " Removed {AMOUNT} from your account."); - public Notice set = Notice.chat("ECONOMY " - + "Set your balance to {AMOUNT}."); - public Notice reset = Notice.chat("ECONOMY " - + "Resetted your balance."); - public Notice balance = Notice.chat("ECONOMY " - + "Your balance is {BALANCE}."); + public Notice added = Notice.chat("ECONOMY Added {AMOUNT} to your account."); + public Notice removed = Notice.chat("ECONOMY Removed {AMOUNT} from your account."); + public Notice set = Notice.chat("ECONOMY Set your balance to {AMOUNT}."); + public Notice reset = Notice.chat("ECONOMY Resetted your balance."); + public Notice balance = Notice.chat("ECONOMY Your balance is {BALANCE}."); public Notice balanceOther = - Notice.chat("ECONOMY " - + "{PLAYER}'s balance is {BALANCE}."); - public Notice insufficientBalance = Notice.chat("ECONOMY " - + "Insufficient funds," - + " you are missing {MISSING_BALANCE}."); - public Notice transferSuccess = Notice.chat("ECONOMY " - + "Successfully transferred {AMOUNT} to " - + "{PLAYER}."); - public Notice transferReceived = Notice.chat("ECONOMY " - + "Received {AMOUNT} from " - + "{PLAYER}."); - public Notice transferLimit = Notice.chat("ECONOMY " - + "Transaction limit is {LIMIT}."); + Notice.chat("ECONOMY {PLAYER}'s balance is {BALANCE}."); + public Notice insufficientBalance = Notice.chat("ECONOMY Insufficient funds, you are missing {MISSING_BALANCE}."); + public Notice transferSuccess = Notice.chat("ECONOMY Successfully transferred {AMOUNT} to {PLAYER}."); + public Notice transferReceived = Notice.chat("ECONOMY Received {AMOUNT} from {PLAYER}."); + public Notice transferLimit = Notice.chat("ECONOMY Transaction limit is {LIMIT}."); @Comment({ "Use {PAGE} placeholder to show the current page number", "Use {TOTAL_PAGES} placeholder to show the total number of pages" }) - public Notice leaderboardHeader = Notice.chat(" Balance leaderboard (Page " - + "{PAGE}/{TOTAL_PAGES}): "); + public Notice leaderboardHeader = Notice.chat(" Balance leaderboard (Page {PAGE}/{TOTAL_PAGES}): "); @Comment({ "Leaderboard entry notice, only displayed if showLeaderboardPosition is set to true in the config.yml", @@ -45,8 +31,7 @@ public class MessagesPlayerSubSection extends OkaeriConfig { "Use {BALANCE} placeholder to show the player's formatted balance", "Use {BALANCE_RAW} placeholder to show the player's raw balance" }) - public Notice leaderboardEntry = Notice.chat(" #{POSITION} {PLAYER} -" - + " {BALANCE}"); + public Notice leaderboardEntry = Notice.chat(" #{POSITION} {PLAYER} - {BALANCE}"); @Comment({ "Leaderboard position notice, only displayed if showLeaderboardPosition is set to true in the config.yml", @@ -65,6 +50,5 @@ public class MessagesPlayerSubSection extends OkaeriConfig { .build(); @Comment("Leaderboard is empty notice") - public Notice leaderboardEmpty = Notice.chat("ECONOMY " - + "Leaderboard is empty :("); + public Notice leaderboardEmpty = Notice.chat("ECONOMY Leaderboard is empty :("); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java index 67e622e2..11f6adef 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawMessageConfig.java @@ -5,37 +5,30 @@ public class WithdrawMessageConfig extends OkaeriConfig { public Notice noItemInHand = Notice.chat( - "ECONOMY " - + "You must hold an item to create a banknote!" + "ECONOMY You must hold an item to create a banknote!" ); public Notice noBanknoteInHand = Notice.chat( - "ECONOMY " - + "You must hold a banknote in your hand to redeem it!" + "ECONOMY You must hold a banknote in your hand to redeem it!" ); public Notice itemSetSuccess = Notice.chat( - "ECONOMY " - + "You have set the banknote item to {ITEM}!" + "ECONOMY You have set the banknote item to {ITEM}!" ); public Notice banknoteWithdrawn = Notice.chat( - "ECONOMY " - + "You have withdrawn a banknote worth {VALUE}!" + "ECONOMY You have withdrawn a banknote worth {VALUE}!" ); public Notice banknoteRedeemed = Notice.chat( - "ECONOMY " - + "You have redeemed your banknote worth {VALUE}!" + "ECONOMY You have redeemed your banknote worth {VALUE}!" ); public Notice invalidInteraction = Notice.chat( - "ECONOMY " - + "You cannot use the banknote item in an anvil or crafting table!" + "ECONOMY You cannot use the banknote item in an anvil or crafting table!" ); public Notice noInventorySpace = Notice.chat( - "ECONOMY " - + "You do not have enough space in your inventory to withdraw a banknote!" + "ECONOMY You do not have enough space in your inventory to withdraw a banknote!" ); } From 4b312cd69c347adcfa4f4a01ec34adf222260b3a Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Tue, 4 Nov 2025 16:48:23 +0100 Subject: [PATCH 16/17] Review. Use proper paper-api loader, fix multiple bug's. --- buildSrc/src/main/kotlin/Versions.kt | 2 +- .../kotlin/economy-repositories.gradle.kts | 4 +- eternaleconomy-core/build.gradle.kts | 76 ++++++----- .../economy/EconomyBukkitLoader.java | 55 ++++++++ .../economy/EconomyBukkitPlugin.java | 4 +- .../command/argument/MoneyFormatArgument.java | 37 ++++-- .../config/implementation/PluginConfig.java | 19 ++- .../economy/config/item/ConfigItem.java | 46 ++++--- .../economy/withdraw/WithdrawCommand.java | 8 +- .../economy/withdraw/WithdrawItemService.java | 4 +- .../withdraw/WithdrawItemServiceImpl.java | 122 +++++++++--------- .../economy/withdraw/WithdrawService.java | 56 ++++---- .../withdraw/WithdrawSetItemCommand.java | 24 ---- .../controller/WithdrawAnvilController.java | 67 ++++++++-- .../controller/WithdrawController.java | 34 ++--- 15 files changed, 331 insertions(+), 227 deletions(-) create mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitLoader.java delete mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index d2744035..b5c2f85c 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -1,6 +1,6 @@ object Versions { - const val PAPER_API = "1.19.4-R0.1-SNAPSHOT" + const val PAPER_API = "1.20.4-R0.1-SNAPSHOT" const val OKAERI_CONFIGS = "5.0.5" const val LITE_COMMANDS = "3.10.5" diff --git a/buildSrc/src/main/kotlin/economy-repositories.gradle.kts b/buildSrc/src/main/kotlin/economy-repositories.gradle.kts index 5e501f5f..6ae90508 100644 --- a/buildSrc/src/main/kotlin/economy-repositories.gradle.kts +++ b/buildSrc/src/main/kotlin/economy-repositories.gradle.kts @@ -3,7 +3,7 @@ plugins { } repositories { - mavenCentral() + maven("https://maven-central.storage-download.googleapis.com/maven2") maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") maven("https://repo.papermc.io/repository/maven-public/") maven("https://repo.panda-lang.org/releases/") @@ -13,4 +13,4 @@ repositories { maven("https://jitpack.io") maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") -} \ No newline at end of file +} diff --git a/eternaleconomy-core/build.gradle.kts b/eternaleconomy-core/build.gradle.kts index 4cdaea1d..275ce346 100644 --- a/eternaleconomy-core/build.gradle.kts +++ b/eternaleconomy-core/build.gradle.kts @@ -1,10 +1,11 @@ import net.minecrell.pluginyml.bukkit.BukkitPluginDescription +import net.minecrell.pluginyml.paper.PaperPluginDescription plugins { `economy-java` `economy-repositories` - id("net.minecrell.plugin-yml.bukkit") + id("net.minecrell.plugin-yml.paper") id("com.gradleup.shadow") id("xyz.jpenilla.run-paper") id("me.champeau.jmh") @@ -26,33 +27,35 @@ dependencies { compileOnly("io.papermc.paper:paper-api:${Versions.PAPER_API}") // eternalcode commons - implementation("com.eternalcode:eternalcode-commons-adventure:${Versions.ETERNALCODE_COMMONS}") - implementation("com.eternalcode:eternalcode-commons-bukkit:${Versions.ETERNALCODE_COMMONS}") - implementation("com.eternalcode:eternalcode-commons-shared:${Versions.ETERNALCODE_COMMONS}") - implementation("com.eternalcode:eternalcode-commons-folia:${Versions.ETERNALCODE_COMMONS}") - - bukkitLibrary("org.mariadb.jdbc:mariadb-java-client:${Versions.MARIA_DB}") - bukkitLibrary("org.postgresql:postgresql:${Versions.POSTGRESQL}") - bukkitLibrary("com.h2database:h2:${Versions.H2}") - bukkitLibrary("com.j256.ormlite:ormlite-core:${Versions.ORMLITE}") - bukkitLibrary("com.j256.ormlite:ormlite-jdbc:${Versions.ORMLITE}") - bukkitLibrary("com.zaxxer:HikariCP:${Versions.HIKARI_CP}") - - implementation("dev.rollczi:litecommands-bukkit:${Versions.LITE_COMMANDS}") - implementation("dev.rollczi:litecommands-adventure:${Versions.LITE_COMMANDS}") - implementation("dev.rollczi:litecommands-jakarta:${Versions.LITE_COMMANDS}") + paperLibrary("com.eternalcode:eternalcode-commons-adventure:${Versions.ETERNALCODE_COMMONS}") + paperLibrary("com.eternalcode:eternalcode-commons-bukkit:${Versions.ETERNALCODE_COMMONS}") + paperLibrary("com.eternalcode:eternalcode-commons-shared:${Versions.ETERNALCODE_COMMONS}") + paperLibrary("com.eternalcode:eternalcode-commons-folia:${Versions.ETERNALCODE_COMMONS}") + + paperLibrary("org.mariadb.jdbc:mariadb-java-client:${Versions.MARIA_DB}") + paperLibrary("org.postgresql:postgresql:${Versions.POSTGRESQL}") + paperLibrary("com.h2database:h2:${Versions.H2}") + paperLibrary("com.j256.ormlite:ormlite-core:${Versions.ORMLITE}") + paperLibrary("com.j256.ormlite:ormlite-jdbc:${Versions.ORMLITE}") + paperLibrary("com.zaxxer:HikariCP:${Versions.HIKARI_CP}") + + paperLibrary("dev.rollczi:litecommands-bukkit:${Versions.LITE_COMMANDS}") + paperLibrary("dev.rollczi:litecommands-adventure:${Versions.LITE_COMMANDS}") + paperLibrary("dev.rollczi:litecommands-jakarta:${Versions.LITE_COMMANDS}") // multification - implementation("com.eternalcode:multification-bukkit:${Versions.MULTIFICATION}") - implementation("com.eternalcode:multification-okaeri:${Versions.MULTIFICATION}") + paperLibrary("com.eternalcode:multification-bukkit:${Versions.MULTIFICATION}") + paperLibrary("com.eternalcode:multification-okaeri:${Versions.MULTIFICATION}") // vault compileOnly("com.github.MilkBowl:VaultAPI:${Versions.VAULT_API}") // okaeri configs - implementation("eu.okaeri:okaeri-configs-yaml-snakeyaml:${Versions.OKAERI_CONFIGS}") - implementation("eu.okaeri:okaeri-configs-serdes-commons:${Versions.OKAERI_CONFIGS}") - implementation("eu.okaeri:okaeri-configs-serdes-bukkit:${Versions.OKAERI_CONFIGS}") + paperLibrary("eu.okaeri:okaeri-configs-yaml-snakeyaml:${Versions.OKAERI_CONFIGS}") + paperLibrary("eu.okaeri:okaeri-configs-serdes-commons:${Versions.OKAERI_CONFIGS}") + paperLibrary("eu.okaeri:okaeri-configs-serdes-bukkit:${Versions.OKAERI_CONFIGS}") + + paperLibrary("com.github.cryptomorin:XSeries:13.5.1") compileOnly("me.clip:placeholderapi:${Versions.PLACEHOLDER_API}") @@ -70,9 +73,10 @@ tasks.test { useJUnitPlatform() } -bukkit { +paper { main = "com.eternalcode.economy.EconomyBukkitPlugin" - apiVersion = "1.13" + loader = "com.eternalcode.economy.EconomyBukkitLoader" + apiVersion = "1.19" prefix = "EternalEconomy" author = "EternalCodeTeam" name = "EternalEconomy" @@ -83,10 +87,19 @@ bukkit { load = BukkitPluginDescription.PluginLoadOrder.STARTUP version = "${project.version}" - depend = listOf("Vault") - softDepend = listOf("PlaceholderAPI") + serverDependencies { + register("Vault") { + required = true + load = PaperPluginDescription.RelativeLoadOrder.BEFORE + } + register("PlaceholderAPI") { + required = false + load = PaperPluginDescription.RelativeLoadOrder.BEFORE + } + } foliaSupported = true + generateLibrariesJson = true } tasks.runServer { @@ -104,13 +117,8 @@ tasks.shadowJar { "org/jetbrains/annotations/**" ) - val prefix = "com.eternalcode.economy.libs" - listOf( - "dev.rollczi", - "eu.okaeri", - "panda", - "org.yaml", - "com.eternalcode.commons", - "net.jodah", - ).forEach { relocate(it, prefix) } +// val prefix = "com.eternalcode.economy.libs" +// listOf( +// +// ).forEach { relocate(it, prefix) } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitLoader.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitLoader.java new file mode 100644 index 00000000..1249bd22 --- /dev/null +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitLoader.java @@ -0,0 +1,55 @@ +package com.eternalcode.economy; + +import com.google.gson.Gson; +import io.papermc.paper.plugin.loader.PluginClasspathBuilder; +import io.papermc.paper.plugin.loader.PluginLoader; +import io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.graph.Dependency; +import org.eclipse.aether.repository.RemoteRepository; +import org.jetbrains.annotations.NotNull; + +public class EconomyBukkitLoader implements PluginLoader { + + public static final Gson GSON = new Gson(); + public static final String LIBRARIES_JSON_PATH = "/paper-libraries.json"; + + @Override + public void classloader(@NotNull PluginClasspathBuilder classpathBuilder) { + MavenLibraryResolver resolver = new MavenLibraryResolver(); + + PluginLibraries pluginLibraries = this.load(); + + pluginLibraries.asDependencies().forEach(resolver::addDependency); + pluginLibraries.asRepositories().forEach(resolver::addRepository); + + classpathBuilder.addLibrary(resolver); + } + + public PluginLibraries load() { + try (InputStream in = getClass().getResourceAsStream(LIBRARIES_JSON_PATH)) { + return GSON.fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), PluginLibraries.class); + } + catch (IOException exception) { + throw new RuntimeException(exception); + } + } + + private record PluginLibraries(Map repositories, List dependencies) { + public Stream asDependencies() { + return this.dependencies.stream().map(d -> new Dependency(new DefaultArtifact(d), null)); + } + + public Stream asRepositories() { + return this.repositories.entrySet().stream() + .map(e -> new RemoteRepository.Builder(e.getKey(), "default", e.getValue()).build()); + } + } +} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 15b68c82..0ba60b6b 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -43,7 +43,6 @@ import com.eternalcode.economy.withdraw.WithdrawCommand; import com.eternalcode.economy.withdraw.WithdrawItemServiceImpl; import com.eternalcode.economy.withdraw.WithdrawService; -import com.eternalcode.economy.withdraw.WithdrawSetItemCommand; import com.eternalcode.economy.withdraw.controller.WithdrawAnvilController; import com.eternalcode.economy.withdraw.controller.WithdrawController; import com.eternalcode.multification.notice.Notice; @@ -108,7 +107,7 @@ public void onEnable() { WithdrawItemServiceImpl withdrawItemServiceImpl = new WithdrawItemServiceImpl(this, pluginConfig, decimalFormatter, - noticeService, miniMessage); + miniMessage); WithdrawService withdrawService = new WithdrawService( server, noticeService, decimalFormatter, withdrawItemServiceImpl, accountPaymentService, accountManager); @@ -145,7 +144,6 @@ public void onEnable() { new AdminSetCommand(accountPaymentService, decimalFormatter, noticeService), new AdminResetCommand(accountPaymentService, noticeService), new AdminBalanceCommand(noticeService, decimalFormatter), - new WithdrawSetItemCommand(withdrawItemServiceImpl), new WithdrawCommand(withdrawService, noticeService, decimalFormatter), new MoneyBalanceCommand(noticeService, decimalFormatter), new MoneyTransferCommand(accountPaymentService, decimalFormatter, noticeService, pluginConfig), diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java index b62b4a34..a4e3eee1 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java @@ -10,6 +10,7 @@ import java.math.RoundingMode; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.bukkit.command.CommandSender; @@ -17,12 +18,12 @@ public class MoneyFormatArgument extends ArgumentResolver { public static final String KEY = "price"; + private static final BigDecimal MINIMUM_VALUE = new BigDecimal("0.10"); + private static final int DECIMAL_SCALE = 2; private final Pattern pricePattern; - private final PluginConfig config; private final MessageConfig messages; - private final Map multipliers; public MoneyFormatArgument(PluginConfig config, MessageConfig messages) { @@ -30,18 +31,15 @@ public MoneyFormatArgument(PluginConfig config, MessageConfig messages) { this.messages = messages; this.multipliers = new HashMap<>(); - StringBuilder suffixes = new StringBuilder(); - this.config.units.format.forEach(unit -> { - this.multipliers.put(unit.getSuffix(), BigDecimal.valueOf(unit.getFactor())); - suffixes.append(unit.getSuffix()); - }); - - this.pricePattern = Pattern.compile("^(\\d+(?:[.,]\\d+)?)([" + suffixes + "])?$", Pattern.CASE_INSENSITIVE); + this.pricePattern = this.buildPricePattern(); } @Override protected ParseResult parse( - Invocation invocation, Argument argument, String raw) { + Invocation invocation, + Argument argument, + String raw + ) { Matcher matcher = this.pricePattern.matcher(raw.toLowerCase()); if (!matcher.matches()) { @@ -55,7 +53,7 @@ protected ParseResult parse( BigDecimal value = new BigDecimal(numberPart); if (suffix != null) { - BigDecimal multiplier = multipliers.get(Character.toLowerCase(suffix.charAt(0))); + BigDecimal multiplier = this.multipliers.get(Character.toLowerCase(suffix.charAt(0))); if (multiplier == null) { return ParseResult.failure(this.messages.invalidMoney); @@ -64,14 +62,27 @@ protected ParseResult parse( value = value.multiply(multiplier); } - if (value.compareTo(BigDecimal.valueOf(0.09)) < 0.09) { + if (value.compareTo(MINIMUM_VALUE) < 0) { return ParseResult.failure(this.messages.incorrectMoneyArgument); } - return ParseResult.success(value.setScale(2, RoundingMode.DOWN)); + return ParseResult.success(value.setScale(DECIMAL_SCALE, RoundingMode.DOWN)); } catch (NumberFormatException exception) { return ParseResult.failure(this.messages.invalidMoney); } } + + private Pattern buildPricePattern() { + StringBuilder suffixes = new StringBuilder(); + + this.config.units.format.forEach(unit -> { + char suffix = unit.getSuffix(); + this.multipliers.put(suffix, BigDecimal.valueOf(unit.getFactor())); + suffixes.append(suffix); + }); + + String patternString = "^(\\d+(?:[.,]\\d+)?)([" + suffixes + "])?$"; + return Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); + } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java index e22ccc7a..3ce17b2b 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java @@ -5,7 +5,6 @@ import com.eternalcode.economy.format.DecimalUnit; import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; -import net.kyori.adventure.text.Component; import org.bukkit.Material; import java.math.BigDecimal; @@ -34,7 +33,7 @@ public class PluginConfig extends OkaeriConfig { public boolean showLeaderboardPosition = true; @Comment("Currency item settings") - public CurrencyItem currencyItem = new CurrencyItem(); + public WithdrawItem withdrawItem = new WithdrawItem(); public static class Units extends OkaeriConfig { @@ -48,13 +47,13 @@ public static class Units extends OkaeriConfig { ); } - public static class CurrencyItem extends OkaeriConfig { - public ConfigItem item = new ConfigItem( - "Check worth {VALUE}$", - List.of("Right click to redeem"), - Material.PAPER, - null, - true - ); + public static class WithdrawItem extends OkaeriConfig { + public ConfigItem item = ConfigItem.builder() + .withName("Check worth {VALUE}$") + .withLore(List.of("Right click to redeem")) + .withMaterial(Material.PAPER) + .withTexture(0) + .withGlow(true) + .build(); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java index d9813936..b79e4d49 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/item/ConfigItem.java @@ -1,29 +1,34 @@ package com.eternalcode.economy.config.item; import eu.okaeri.configs.OkaeriConfig; +import java.util.ArrayList; import java.util.Collections; import java.util.List; -import net.kyori.adventure.text.Component; +import java.util.Objects; import org.bukkit.Material; public class ConfigItem extends OkaeriConfig { - public String name = "&6Item"; - public List lore = Collections.singletonList("&7This is an item"); - public Material material = Material.PLAYER_HEAD; - public Integer texture = null; - public boolean glow = false; + private String name; + private List lore; + private Material material; + private Integer texture; + private boolean glow; public ConfigItem(String name, List lore, Material material, Integer texture, boolean glow) { this.name = name; - this.lore = lore; + this.lore = lore != null ? new ArrayList<>(lore) : new ArrayList<>(); this.material = material; this.texture = texture; this.glow = glow; } public ConfigItem() { - + this.name = "&6Item"; + this.lore = new ArrayList<>(Collections.singletonList("&7This is an item")); + this.material = Material.PLAYER_HEAD; + this.texture = null; + this.glow = false; } public static Builder builder() { @@ -35,7 +40,7 @@ public String name() { } public List lore() { - return this.lore; + return Collections.unmodifiableList(this.lore); } public Material material() { @@ -51,40 +56,39 @@ public boolean glow() { } public static class Builder { - private final ConfigItem configItem = new ConfigItem(); + private String name = "&6Item"; + private List lore = new ArrayList<>(Collections.singletonList("&7This is an item")); + private Material material = Material.PLAYER_HEAD; + private Integer texture = null; + private boolean glow = false; public Builder withName(String name) { - this.configItem.name = name; - + this.name = name; return this; } public Builder withLore(List lore) { - this.configItem.lore = lore; - + this.lore = lore != null ? new ArrayList<>(lore) : new ArrayList<>(); return this; } public Builder withMaterial(Material material) { - this.configItem.material = material; - + this.material = material; return this; } public Builder withTexture(Integer texture) { - this.configItem.texture = texture; - + this.texture = texture; return this; } public Builder withGlow(boolean glow) { - this.configItem.glow = glow; - + this.glow = glow; return this; } public ConfigItem build() { - return this.configItem; + return new ConfigItem(this.name, this.lore, this.material, this.texture, this.glow); } } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java index c1bd768a..38e61f2e 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java @@ -32,20 +32,20 @@ public WithdrawCommand( } @Execute - void execute(@Context Account account, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal value) { + void execute(@Context Account account, @Arg("amount") @Key(MoneyFormatArgument.KEY) BigDecimal value) { BigDecimal balance = account.balance(); BigDecimal subtract = balance.subtract(value); if (subtract.compareTo(BigDecimal.ZERO) < 0) { - noticeService.create() + this.noticeService.create() .notice(messageConfig -> messageConfig.player.insufficientBalance) - .placeholder("{MISSING_BALANCE}", decimalFormatter.format(subtract.abs())) + .placeholder("{MISSING_BALANCE}", this.decimalFormatter.format(subtract.abs())) .player(account.uuid()) .send(); return; } - withdrawService.addBanknote(account.uuid(), value); + this.withdrawService.addBanknote(account.uuid(), value); } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java index 941d178f..8d64cee8 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemService.java @@ -5,8 +5,10 @@ import org.bukkit.inventory.ItemStack; public interface WithdrawItemService { + BigDecimal getValue(ItemStack itemStack); + boolean isBanknote(ItemStack itemStack); + ItemStack createBanknote(BigDecimal value); - void setItem(Player player); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java index 7c7ef1b1..4a311d43 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java @@ -1,21 +1,17 @@ package com.eternalcode.economy.withdraw; +import com.cryptomorin.xseries.XEnchantment; import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.config.item.ConfigItem; import com.eternalcode.economy.format.DecimalFormatter; -import com.eternalcode.economy.multification.NoticeService; import java.math.BigDecimal; import java.util.List; -import java.util.Objects; -import java.util.concurrent.CompletableFuture; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; -import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -24,110 +20,106 @@ public class WithdrawItemServiceImpl implements WithdrawItemService { + private static final String VALUE_PLACEHOLDER = "{VALUE}"; + private static final String WITHDRAW_VALUE_KEY = "withdraw_value"; + private final PluginConfig pluginConfig; private final DecimalFormatter moneyFormatter; private final MiniMessage miniMessage; private final NamespacedKey banknoteValueKey; - private final NoticeService noticeService; public WithdrawItemServiceImpl( Plugin plugin, PluginConfig pluginConfig, DecimalFormatter moneyFormatter, - NoticeService noticeService, MiniMessage miniMessage ) { - this.banknoteValueKey = new NamespacedKey(plugin, "withdraw_value"); + this.banknoteValueKey = new NamespacedKey(plugin, WITHDRAW_VALUE_KEY); this.pluginConfig = pluginConfig; this.moneyFormatter = moneyFormatter; this.miniMessage = miniMessage; - this.noticeService = noticeService; } @Override public ItemStack createBanknote(BigDecimal value) { - ItemStack banknoteItem = createBaseBanknoteItem(value); - return attachBanknoteValue(banknoteItem, value); + if (value.compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Banknote value must be positive, got: " + value); + } + + ItemStack banknoteItem = this.createBaseBanknoteItem(value); + return this.attachBanknoteValue(banknoteItem, value); } @Override public boolean isBanknote(ItemStack itemStack) { - if (itemStack == null || !itemStack.hasItemMeta()) { + if (itemStack == null) { + return false; + } + + if (!itemStack.hasItemMeta()) { return false; } + ItemMeta itemMeta = itemStack.getItemMeta(); - return itemMeta.getPersistentDataContainer().has(banknoteValueKey, PersistentDataType.STRING); + return itemMeta.getPersistentDataContainer().has(this.banknoteValueKey, PersistentDataType.STRING); } @Override public BigDecimal getValue(ItemStack itemStack) { - if (itemStack == null || !itemStack.hasItemMeta()) { + if (itemStack == null) { return BigDecimal.ZERO; } - ItemMeta itemMeta = itemStack.getItemMeta(); - String storedValue = itemMeta.getPersistentDataContainer() - .get(banknoteValueKey, PersistentDataType.STRING); - return storedValue != null ? new BigDecimal(storedValue) : BigDecimal.ZERO; - } - @Override - public void setItem(Player player) { - ItemStack item = player.getInventory().getItemInMainHand(); + if (!itemStack.hasItemMeta()) { + return BigDecimal.ZERO; + } - if (item.getType() == Material.AIR) { - noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.noItemInHand) - .player(player.getUniqueId()) - .send(); + ItemMeta itemMeta = itemStack.getItemMeta(); + String storedValue = itemMeta.getPersistentDataContainer() + .get(this.banknoteValueKey, PersistentDataType.STRING); - return; + if (storedValue == null) { + return BigDecimal.ZERO; } - Component displayName = Objects.requireNonNull(item.getItemMeta()).displayName(); - - if (displayName != null) { - this.pluginConfig.currencyItem.item.name = PlainTextComponentSerializer.plainText().serialize(displayName); + try { + return new BigDecimal(storedValue); } - else { - this.pluginConfig.currencyItem.item.name = item.getType().name(); + catch (NumberFormatException exception) { + return BigDecimal.ZERO; } - - this.pluginConfig.currencyItem.item.glow = item.getItemMeta().hasEnchants(); - this.pluginConfig.currencyItem.item.lore = Objects.requireNonNullElse(item.getItemMeta().lore(), List.of()) - .stream() - .map(miniMessage::serialize) - .toList(); - - this.pluginConfig.currencyItem.item.material = item.getType(); - this.pluginConfig.currencyItem.item.texture = item.getItemMeta().hasCustomModelData() ? - item.getItemMeta().getCustomModelData() : null; - - CompletableFuture.runAsync(this.pluginConfig::save); - - noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.itemSetSuccess) - .placeholder("{ITEM}", this.pluginConfig.currencyItem.item.name) - .player(player.getUniqueId()) - .send(); } private ItemStack createBaseBanknoteItem(BigDecimal value) { - ConfigItem configItem = this.pluginConfig.currencyItem.item; + ConfigItem configItem = this.pluginConfig.withdrawItem.item; + String formattedValue = this.moneyFormatter.format(value); ItemStack banknoteItem = new ItemStack(configItem.material()); banknoteItem.editMeta(meta -> { - meta.setCustomModelData(configItem.texture); - meta.displayName(miniMessage.deserialize(configItem.name.replace("{VALUE}", moneyFormatter.format(value)), TagResolver.empty())); - meta.lore(configItem.lore.stream() - .map(line -> miniMessage.deserialize(line.replace("{VALUE}", moneyFormatter.format(value)), - TagResolver.empty())) - .toList()); + if (configItem.texture() != null) { + meta.setCustomModelData(configItem.texture()); + } + + Component displayName = this.miniMessage.deserialize( + configItem.name().replace(VALUE_PLACEHOLDER, formattedValue), + TagResolver.empty() + ).decoration(TextDecoration.ITALIC, false); + meta.displayName(displayName); + + List loreComponents = configItem.lore().stream() + .map(line -> this.miniMessage.deserialize( + line.replace(VALUE_PLACEHOLDER, formattedValue), + TagResolver.empty() + ).decoration(TextDecoration.ITALIC, false)) + .toList(); + meta.lore(loreComponents); }); - if (configItem.glow) { + if (configItem.glow()) { banknoteItem.editMeta(meta -> { - meta.addEnchant(Enchantment.DURABILITY, 1, true); + Enchantment enchantment = XEnchantment.UNBREAKING.get(); // for version compatibility + meta.addEnchant(enchantment, 1, true); meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); }); } @@ -138,7 +130,11 @@ private ItemStack createBaseBanknoteItem(BigDecimal value) { private ItemStack attachBanknoteValue(ItemStack banknoteItem, BigDecimal value) { ItemStack taggedItem = banknoteItem.clone(); taggedItem.editMeta(meta -> - meta.getPersistentDataContainer().set(banknoteValueKey, PersistentDataType.STRING, value.toPlainString()) + meta.getPersistentDataContainer().set( + this.banknoteValueKey, + PersistentDataType.STRING, + value.toPlainString() + ) ); return taggedItem; } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java index 9645eafb..a3c00cd1 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawService.java @@ -3,28 +3,24 @@ import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountManager; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import java.math.BigDecimal; -import java.util.List; import java.util.Objects; import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.MiniMessage; -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; public class WithdrawService { + + private static final int MINIMUM_AMOUNT = 1; + private final Server server; private final NoticeService noticeService; private final WithdrawItemService withdrawItemService; private final DecimalFormatter decimalFormatter; - private final AccountPaymentService accountPaymentService; private final AccountManager accountManager; @@ -40,58 +36,74 @@ public WithdrawService( this.noticeService = noticeService; this.decimalFormatter = decimalFormatter; this.withdrawItemService = withdrawItemService; - this.accountPaymentService = accountPaymentService; this.accountManager = accountManager; } public void addBanknote(UUID uuid, BigDecimal value) { - Player player = server.getPlayer(uuid); + if (value.compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Banknote value must be positive, got: " + value); + } + Player player = this.server.getPlayer(uuid); if (player == null) { return; } if (player.getInventory().firstEmpty() == -1) { - noticeService.create() + this.noticeService.create() .notice(messageConfig -> messageConfig.withdraw.noInventorySpace) .player(player.getUniqueId()) .send(); return; } - ItemStack item = withdrawItemService.createBanknote(value); - player.getInventory().addItem(item); + ItemStack banknote = this.withdrawItemService.createBanknote(value); + player.getInventory().addItem(banknote); - Account account = accountManager.getAccount(player.getUniqueId()); - accountPaymentService.removeBalance(account, value); + Account account = this.accountManager.getAccount(player.getUniqueId()); + this.accountPaymentService.removeBalance(account, value); - noticeService.create() + this.noticeService.create() .notice(messageConfig -> messageConfig.withdraw.banknoteWithdrawn) - .placeholder("{VALUE}", decimalFormatter.format(value)) + .placeholder("{VALUE}", this.decimalFormatter.format(value)) .player(player.getUniqueId()) .send(); } public void redeem(Player player, ItemStack item, BigDecimal value, int amount) { + if (amount < MINIMUM_AMOUNT) { + throw new IllegalArgumentException("Amount must be at least " + MINIMUM_AMOUNT + ", got: " + amount); + } + + if (value.compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Banknote value must be positive, got: " + value); + } + if (item.getType() == Material.AIR) { - noticeService.create() + this.noticeService.create() .notice(messageConfig -> messageConfig.withdraw.noBanknoteInHand) .player(player.getUniqueId()) .send(); return; } + if (item.getAmount() < amount) { + throw new IllegalArgumentException( + "Cannot redeem " + amount + " items when only " + item.getAmount() + " are available" + ); + } + item.setAmount(item.getAmount() - amount); - BigDecimal finalValue = value.multiply(BigDecimal.valueOf(amount)); + BigDecimal totalValue = value.multiply(BigDecimal.valueOf(amount)); - Account account = accountManager.getAccount(player.getUniqueId()); - accountPaymentService.addBalance(account, finalValue); + Account account = this.accountManager.getAccount(player.getUniqueId()); + this.accountPaymentService.addBalance(account, totalValue); - noticeService.create() + this.noticeService.create() .notice(messageConfig -> messageConfig.withdraw.banknoteRedeemed) - .placeholder("{VALUE}", decimalFormatter.format(finalValue)) + .placeholder("{VALUE}", this.decimalFormatter.format(totalValue)) .player(player.getUniqueId()) .send(); } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java deleted file mode 100644 index e9b1a2df..00000000 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawSetItemCommand.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.eternalcode.economy.withdraw; - -import com.eternalcode.economy.EconomyPermissionConstant; -import dev.rollczi.litecommands.annotations.command.Command; -import dev.rollczi.litecommands.annotations.context.Context; -import dev.rollczi.litecommands.annotations.execute.Execute; -import dev.rollczi.litecommands.annotations.permission.Permission; -import org.bukkit.entity.Player; - -@Command(name = "economy withdraw setitem") -@Permission(EconomyPermissionConstant.ADMIN_ITEM_PERMISSION) -public class WithdrawSetItemCommand { - - private final WithdrawItemService withdrawItemService; - - public WithdrawSetItemCommand(WithdrawItemService withdrawItemService) { - this.withdrawItemService = withdrawItemService; - } - - @Execute - void execute(@Context Player player) { - this.withdrawItemService.setItem(player); - } -} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java index df700142..90bb4663 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawAnvilController.java @@ -2,13 +2,14 @@ import com.eternalcode.economy.multification.NoticeService; import com.eternalcode.economy.withdraw.WithdrawItemService; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.inventory.AnvilInventory; -import org.bukkit.inventory.CraftingInventory; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -22,34 +23,72 @@ public WithdrawAnvilController(WithdrawItemService withdrawItemService, NoticeSe this.noticeService = noticeService; } - @EventHandler(priority = EventPriority.MONITOR) - public void onAnvilUse(InventoryClickEvent event) { + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onAnvilClick(InventoryClickEvent event) { Inventory topInventory = event.getInventory(); - if (!(topInventory instanceof AnvilInventory) && !(topInventory instanceof CraftingInventory)) { + if (this.isRestrictedInventory(topInventory)) { return; } - ItemStack item = event.getCurrentItem(); + ItemStack clickedItem = event.getCurrentItem(); + ItemStack cursorItem = event.getCursor(); - if (item == null) { + if (this.isBanknoteInteraction(clickedItem, cursorItem, event.getAction())) { + event.setCancelled(true); + event.getView().close(); + + this.noticeService.create() + .notice(messageConfig -> messageConfig.withdraw.invalidInteraction) + .viewer(event.getWhoClicked()) + .send(); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onAnvilDrag(InventoryDragEvent event) { + Inventory inventory = event.getInventory(); + + if (this.isRestrictedInventory(inventory)) { return; } - if (event.getAction() != InventoryAction.MOVE_TO_OTHER_INVENTORY) { + ItemStack draggedItem = event.getOldCursor(); + if (draggedItem == null) { return; } - if (this.withdrawItemService.isBanknote(item)) { + boolean isDraggingToTopInventory = event.getRawSlots().stream() + .anyMatch(slot -> slot < inventory.getSize()); + + if (isDraggingToTopInventory && this.withdrawItemService.isBanknote(draggedItem)) { event.setCancelled(true); event.getView().close(); - this.noticeService.create() - .notice(messageConfig -> messageConfig.withdraw.invalidInteraction) - .viewer(event.getWhoClicked()) - .send(); + if (event.getWhoClicked() instanceof Player) { + this.noticeService.create() + .notice(messageConfig -> messageConfig.withdraw.invalidInteraction) + .viewer(event.getWhoClicked()) + .send(); + } } } - // TODO: Implement blocking item drag into inventory + private boolean isRestrictedInventory(Inventory inventory) { + InventoryType type = inventory.getType(); + return type != InventoryType.ANVIL && type != InventoryType.CRAFTING && type != InventoryType.WORKBENCH; + } + + private boolean isBanknoteInteraction(ItemStack clickedItem, ItemStack cursorItem, InventoryAction action) { + if (action == InventoryAction.MOVE_TO_OTHER_INVENTORY && this.withdrawItemService.isBanknote(clickedItem)) { + return true; + } + + if (action == InventoryAction.PLACE_ALL || action == InventoryAction.PLACE_ONE + || action == InventoryAction.PLACE_SOME) { + return this.withdrawItemService.isBanknote(cursorItem); + } + + return false; + } } diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java index b248b500..ee8a54e7 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/controller/WithdrawController.java @@ -3,14 +3,18 @@ import com.eternalcode.economy.withdraw.WithdrawItemService; import com.eternalcode.economy.withdraw.WithdrawService; import java.math.BigDecimal; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; public class WithdrawController implements Listener { + private final WithdrawService withdrawService; private final WithdrawItemService withdrawItemService; @@ -19,35 +23,35 @@ public WithdrawController(WithdrawService withdrawService, WithdrawItemService w this.withdrawItemService = withdrawItemService; } - @EventHandler + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false) public void onItemUse(PlayerInteractEvent event) { - ItemStack item = event.getItem(); + Action action = event.getAction(); + if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) { + return; + } - if (item == null) { + Player player = event.getPlayer(); + EquipmentSlot hand = event.getHand(); + if (hand == null) { return; } - BigDecimal value = withdrawItemService.getValue(item); + ItemStack item = hand == EquipmentSlot.HAND + ? player.getInventory().getItemInMainHand() + : player.getInventory().getItemInOffHand(); - if (value == null || value.compareTo(BigDecimal.ZERO) == 0) { + if (item == null || item.getType() == Material.AIR) { return; } - Player player = event.getPlayer(); - Action action = event.getAction(); - - if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) { + BigDecimal value = this.withdrawItemService.getValue(item); + if (value == null || value.compareTo(BigDecimal.ZERO) <= 0) { return; } event.setCancelled(true); - int itemAmount = item.getAmount(); - - if (!player.isSneaking()) { - itemAmount = 1; - } - + int itemAmount = player.isSneaking() ? item.getAmount() : 1; this.withdrawService.redeem(player, item, value, itemAmount); } } From 43aed796c3e36383604fd0ec5a464a0ba321914c Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Tue, 4 Nov 2025 21:53:04 +0100 Subject: [PATCH 17/17] Fix tabulation. Remove useless changes. --- .../economy/EconomyBukkitPlugin.java | 7 -- .../command/argument/MoneyFormatArgument.java | 88 ------------------- .../command/impl/MoneyTransferCommand.java | 4 +- .../command/impl/admin/AdminAddCommand.java | 5 +- .../impl/admin/AdminRemoveCommand.java | 4 +- .../command/impl/admin/AdminSetCommand.java | 7 +- .../config/implementation/PluginConfig.java | 2 +- .../economy/withdraw/WithdrawCommand.java | 4 +- .../withdraw/WithdrawItemServiceImpl.java | 2 +- 9 files changed, 12 insertions(+), 111 deletions(-) delete mode 100644 eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java index 0ba60b6b..a2bd63d6 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/EconomyBukkitPlugin.java @@ -12,7 +12,6 @@ import com.eternalcode.economy.account.database.AccountRepositoryImpl; import com.eternalcode.economy.bridge.BridgeManager; import com.eternalcode.economy.command.argument.AccountArgument; -import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.command.context.AccountContext; import com.eternalcode.economy.command.cooldown.CommandCooldownEditor; import com.eternalcode.economy.command.cooldown.CommandCooldownMessage; @@ -49,7 +48,6 @@ import com.eternalcode.multification.notice.NoticeBroadcast; import com.google.common.base.Stopwatch; import dev.rollczi.litecommands.LiteCommands; -import dev.rollczi.litecommands.argument.ArgumentKey; import dev.rollczi.litecommands.bukkit.LiteBukkitFactory; import dev.rollczi.litecommands.jakarta.LiteJakartaExtension; import dev.rollczi.litecommands.message.LiteMessages; @@ -154,11 +152,6 @@ public void onEnable() { .context(Account.class, new AccountContext(accountManager, messageConfig)) .argument(Account.class, new AccountArgument(accountManager, noticeService, server)) - .argument( - BigDecimal.class, - ArgumentKey.of(MoneyFormatArgument.KEY), - new MoneyFormatArgument(pluginConfig, messageConfig)) - .result(Notice.class, new NoticeHandler(noticeService)) .result(NoticeBroadcast.class, new NoticeBroadcastHandler()) diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java deleted file mode 100644 index a4e3eee1..00000000 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/argument/MoneyFormatArgument.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.eternalcode.economy.command.argument; - -import com.eternalcode.economy.config.implementation.PluginConfig; -import com.eternalcode.economy.config.implementation.messages.MessageConfig; -import dev.rollczi.litecommands.argument.Argument; -import dev.rollczi.litecommands.argument.parser.ParseResult; -import dev.rollczi.litecommands.argument.resolver.ArgumentResolver; -import dev.rollczi.litecommands.invocation.Invocation; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.bukkit.command.CommandSender; - -public class MoneyFormatArgument extends ArgumentResolver { - - public static final String KEY = "price"; - private static final BigDecimal MINIMUM_VALUE = new BigDecimal("0.10"); - private static final int DECIMAL_SCALE = 2; - - private final Pattern pricePattern; - private final PluginConfig config; - private final MessageConfig messages; - private final Map multipliers; - - public MoneyFormatArgument(PluginConfig config, MessageConfig messages) { - this.config = config; - this.messages = messages; - this.multipliers = new HashMap<>(); - - this.pricePattern = this.buildPricePattern(); - } - - @Override - protected ParseResult parse( - Invocation invocation, - Argument argument, - String raw - ) { - Matcher matcher = this.pricePattern.matcher(raw.toLowerCase()); - - if (!matcher.matches()) { - return ParseResult.failure(this.messages.invalidMoney); - } - - String numberPart = matcher.group(1).replace(',', '.'); - String suffix = matcher.group(2); - - try { - BigDecimal value = new BigDecimal(numberPart); - - if (suffix != null) { - BigDecimal multiplier = this.multipliers.get(Character.toLowerCase(suffix.charAt(0))); - - if (multiplier == null) { - return ParseResult.failure(this.messages.invalidMoney); - } - - value = value.multiply(multiplier); - } - - if (value.compareTo(MINIMUM_VALUE) < 0) { - return ParseResult.failure(this.messages.incorrectMoneyArgument); - } - - return ParseResult.success(value.setScale(DECIMAL_SCALE, RoundingMode.DOWN)); - } - catch (NumberFormatException exception) { - return ParseResult.failure(this.messages.invalidMoney); - } - } - - private Pattern buildPricePattern() { - StringBuilder suffixes = new StringBuilder(); - - this.config.units.format.forEach(unit -> { - char suffix = unit.getSuffix(); - this.multipliers.put(suffix, BigDecimal.valueOf(unit.getFactor())); - suffixes.append(suffix); - }); - - String patternString = "^(\\d+(?:[.,]\\d+)?)([" + suffixes + "])?$"; - return Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); - } -} diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java index 2ccce09f..989fb2d6 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/MoneyTransferCommand.java @@ -3,7 +3,6 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.command.validator.notsender.NotSender; import com.eternalcode.economy.config.implementation.PluginConfig; import com.eternalcode.economy.format.DecimalFormatter; @@ -14,6 +13,7 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; +import jakarta.validation.constraints.Positive; import java.math.BigDecimal; @Command(name = "pay", aliases = "transfer") @@ -38,7 +38,7 @@ public MoneyTransferCommand( } @Execute - void execute(@Context Account payer, @Arg @NotSender Account receiver, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { + void execute(@Context Account payer, @Arg @NotSender Account receiver, @Arg @Positive BigDecimal amount) { if (payer.balance().compareTo(amount) < 1) { BigDecimal subtract = amount.subtract(payer.balance()); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java index 75b9acb4..0cd2b242 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminAddCommand.java @@ -3,15 +3,14 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; -import dev.rollczi.litecommands.annotations.argument.Key; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; +import jakarta.validation.constraints.Positive; import java.math.BigDecimal; import org.bukkit.command.CommandSender; @@ -34,7 +33,7 @@ public AdminAddCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Positive BigDecimal amount) { this.accountPaymentService.addBalance(receiver, amount); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java index a378aa34..e849afac 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminRemoveCommand.java @@ -3,7 +3,6 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -12,6 +11,7 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; +import jakarta.validation.constraints.Positive; import java.math.BigDecimal; import org.bukkit.command.CommandSender; @@ -34,7 +34,7 @@ public AdminRemoveCommand( } @Execute - void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Positive BigDecimal amount) { if (receiver.balance().compareTo(amount) < 0) { BigDecimal subtract = amount.subtract(receiver.balance()); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java index 739a0173..588e3746 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/command/impl/admin/AdminSetCommand.java @@ -3,7 +3,6 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; import com.eternalcode.economy.account.AccountPaymentService; -import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -12,6 +11,7 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; +import jakarta.validation.constraints.Positive; import java.math.BigDecimal; import org.bukkit.command.CommandSender; @@ -34,10 +34,7 @@ public AdminSetCommand( } @Execute - void execute( - @Context CommandSender sender, - @Arg Account receiver, - @Arg @Key(MoneyFormatArgument.KEY) BigDecimal amount) { + void execute(@Context CommandSender sender, @Arg Account receiver, @Arg @Positive BigDecimal amount) { this.accountPaymentService.setBalance(receiver, amount); this.noticeService.create() diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java index 3ce17b2b..0bb435bb 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/config/implementation/PluginConfig.java @@ -33,7 +33,7 @@ public class PluginConfig extends OkaeriConfig { public boolean showLeaderboardPosition = true; @Comment("Currency item settings") - public WithdrawItem withdrawItem = new WithdrawItem(); + public WithdrawItem withdraw = new WithdrawItem(); public static class Units extends OkaeriConfig { diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java index 38e61f2e..2ba646c3 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawCommand.java @@ -2,7 +2,6 @@ import com.eternalcode.economy.EconomyPermissionConstant; import com.eternalcode.economy.account.Account; -import com.eternalcode.economy.command.argument.MoneyFormatArgument; import com.eternalcode.economy.format.DecimalFormatter; import com.eternalcode.economy.multification.NoticeService; import dev.rollczi.litecommands.annotations.argument.Arg; @@ -11,6 +10,7 @@ import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; +import jakarta.validation.constraints.Positive; import java.math.BigDecimal; @Command(name = "withdraw", aliases = {"paycheck", "check"}) @@ -32,7 +32,7 @@ public WithdrawCommand( } @Execute - void execute(@Context Account account, @Arg("amount") @Key(MoneyFormatArgument.KEY) BigDecimal value) { + void execute(@Context Account account, @Arg("amount") @Positive BigDecimal value) { BigDecimal balance = account.balance(); BigDecimal subtract = balance.subtract(value); diff --git a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java index 4a311d43..94450437 100644 --- a/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java +++ b/eternaleconomy-core/src/main/java/com/eternalcode/economy/withdraw/WithdrawItemServiceImpl.java @@ -91,7 +91,7 @@ public BigDecimal getValue(ItemStack itemStack) { } private ItemStack createBaseBanknoteItem(BigDecimal value) { - ConfigItem configItem = this.pluginConfig.withdrawItem.item; + ConfigItem configItem = this.pluginConfig.withdraw.item; String formattedValue = this.moneyFormatter.format(value); ItemStack banknoteItem = new ItemStack(configItem.material());