From 5b2806fd2a0b6b8cc3b0f3912d556fe47b0e9ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Tue, 16 Jul 2024 17:05:45 +0200 Subject: [PATCH] refactor: move methods to CommandHelper --- .../common/command/CommandHelper.java | 55 +++++++++++++++++++ .../common/command/abstraction/Command.java | 17 ------ .../argument/type/OfflineUserArgument.java | 16 +----- .../exception/LockedUserException.java | 9 +++ .../common/commands/BalanceCommand.java | 10 +--- .../commands/CurrencyParentCommand.java | 1 + .../lighteco/common/commands/GiveCommand.java | 12 ++-- .../lighteco/common/commands/PayCommand.java | 20 +++---- .../lighteco/common/commands/SetCommand.java | 12 ++-- .../lighteco/common/commands/TakeCommand.java | 12 ++-- .../common/config/message/MessageConfig.java | 2 +- test/paper/src/main/resources/plugin.yml | 2 +- 12 files changed, 97 insertions(+), 71 deletions(-) create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java new file mode 100644 index 0000000..acc6359 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java @@ -0,0 +1,55 @@ +package dev.xhyrom.lighteco.common.command; + +import com.mojang.brigadier.context.CommandContext; +import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument; +import dev.xhyrom.lighteco.common.command.exception.LockedUserException; +import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig; +import dev.xhyrom.lighteco.common.model.chat.CommandSender; +import dev.xhyrom.lighteco.common.model.currency.Currency; +import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import lombok.experimental.UtilityClass; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + +import java.util.Map; + +@UtilityClass +public class CommandHelper { + public static User getUser(CommandContext context) { + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); + + User target = null; + try { + target = OfflineUserArgument.getOfflineUser(context, "target"); + } catch (LockedUserException e) { + sender.sendMessage(MiniMessage.miniMessage().deserialize( + plugin.getConfig().messages.wait + )); + } + + if (target == null || target.getUsername() == null) { + String userName = context.getArgument("target", String.class); + + sender.sendMessage(MiniMessage.miniMessage().deserialize( + plugin.getConfig().messages.userNotFound, + Placeholder.parsed("username", userName) + )); + return null; + } + + return target; + } + + public static CurrencyMessageConfig getCurrencyMessageConfig(LightEcoPlugin plugin, Currency currency) { + Map config = plugin.getConfig().messages.currency; + CurrencyMessageConfig currencyMessageConfig = config.get(currency.getIdentifier()); + + if (currencyMessageConfig == null) { + return config.get("default"); + } + + return currencyMessageConfig; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java index 13183ea..c44f2af 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java @@ -2,19 +2,13 @@ package dev.xhyrom.lighteco.common.command.abstraction; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; -import com.mojang.brigadier.tree.LiteralCommandNode; import dev.xhyrom.lighteco.common.command.CommandSource; -import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig; -import dev.xhyrom.lighteco.common.model.currency.Currency; -import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import lombok.Getter; import org.checkerframework.checker.nullness.qual.NonNull; -import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; @Getter public abstract class Command { @@ -39,15 +33,4 @@ public abstract class Command { protected LiteralArgumentBuilder builder() { return LiteralArgumentBuilder.literal(name); } - - protected CurrencyMessageConfig getCurrencyMessageConfig(LightEcoPlugin plugin, Currency currency) { - Map config = plugin.getConfig().messages.currency; - CurrencyMessageConfig currencyMessageConfig = config.get(currency.getIdentifier()); - - if (currencyMessageConfig == null) { - return config.get("default"); - } - - return currencyMessageConfig; - } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java index 64d0c1d..ff80db7 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java @@ -5,6 +5,7 @@ import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.exception.LockedUserException; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; @@ -16,7 +17,7 @@ import java.util.UUID; public class OfflineUserArgument implements ArgumentType { private OfflineUserArgument() {} - public static User getOfflineUser(CommandContext context, String name) { + public static User getOfflineUser(CommandContext context, String name) throws LockedUserException { String userName = context.getArgument(name, String.class); LightEcoPlugin plugin = context.getSource().plugin(); CommandSender sender = context.getSource().sender(); @@ -27,7 +28,7 @@ public class OfflineUserArgument implements ArgumentType { } if (sender.getUniqueId() != uniqueId && plugin.getCommandManager().getLocks().contains(uniqueId)) { - return null; + throw new LockedUserException(uniqueId); } // Lock the user to prevent race conditions @@ -36,17 +37,6 @@ public class OfflineUserArgument implements ArgumentType { return plugin.getUserManager().loadUser(uniqueId).join(); } - public static void handleMissing(CommandContext context, String name) { - String userName = context.getArgument(name, String.class); - LightEcoPlugin plugin = context.getSource().plugin(); - CommandSender sender = context.getSource().sender(); - - sender.sendMessage(MiniMessage.miniMessage().deserialize( - plugin.getConfig().messages.invalidUser, - Placeholder.parsed("username", userName) - )); - } - @Override public String parse(StringReader reader) throws CommandSyntaxException { return reader.readString(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java new file mode 100644 index 0000000..994d0d1 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java @@ -0,0 +1,9 @@ +package dev.xhyrom.lighteco.common.command.exception; + +import java.util.UUID; + +public class LockedUserException extends Exception { + public LockedUserException(UUID uniqueId) { + super("User with uuid " + uniqueId + " is currently locked thus cannot be modified"); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java index 74a5e6e..471b675 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java @@ -2,12 +2,9 @@ package dev.xhyrom.lighteco.common.commands; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.tree.CommandNode; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; -import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument; import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; @@ -20,6 +17,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; public class BalanceCommand extends Command { private final Currency currency; @@ -45,11 +43,9 @@ public class BalanceCommand extends Command { LightEcoPlugin plugin = context.getSource().plugin(); CommandSender sender = context.getSource().sender(); - User target = OfflineUserArgument.getOfflineUser(context, "target"); - if (target == null || target.getUsername() == null) { - OfflineUserArgument.handleMissing(context, "target"); + final User target = getUser(context); + if (target == null) return SINGLE_SUCCESS; - } BigDecimal balance = target.getBalance(currency); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java index 32f4cb9..84ef42b 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java @@ -15,6 +15,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.getCurrencyMessageConfig; public class CurrencyParentCommand extends Command { private final Currency currency; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java index ed69d54..7e247d5 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java @@ -9,7 +9,6 @@ import com.mojang.brigadier.tree.CommandNode; import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; -import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument; import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; @@ -23,6 +22,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; public class GiveCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); @@ -42,11 +42,9 @@ public class GiveCommand extends Command { final LightEcoPlugin plugin = context.getSource().plugin(); final CommandSender sender = context.getSource().sender(); - final User target = OfflineUserArgument.getOfflineUser(context, "target"); - if (target == null || target.getUsername() == null) { - OfflineUserArgument.handleMissing(context, "target"); + final User target = getUser(context); + if (target == null) return; - } BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 ? context.getArgument("amount", Double.class) @@ -59,7 +57,7 @@ public class GiveCommand extends Command { } catch (CannotBeGreaterThan e) { sender.sendMessage( miniMessage.deserialize( - this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) ) ); @@ -69,7 +67,7 @@ public class GiveCommand extends Command { sender.sendMessage( miniMessage.deserialize( - this.getCurrencyMessageConfig(plugin, this.currency).set, + getCurrencyMessageConfig(plugin, this.currency).set, Placeholder.parsed("currency", currency.getIdentifier()), Placeholder.parsed("target", target.getUsername()), Placeholder.parsed("amount", amount.toPlainString()) diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java index e69325b..692212a 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java @@ -9,7 +9,6 @@ import com.mojang.brigadier.tree.CommandNode; import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; -import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument; import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; @@ -23,6 +22,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; public class PayCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); @@ -42,11 +42,9 @@ public class PayCommand extends Command { final LightEcoPlugin plugin = context.getSource().plugin(); final CommandSender sender = context.getSource().sender(); - final User target = OfflineUserArgument.getOfflineUser(context, "target"); - if (target == null || target.getUsername() == null) { - OfflineUserArgument.handleMissing(context, "target"); + final User target = getUser(context); + if (target == null) return; - } BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 ? context.getArgument("amount", Double.class) @@ -61,7 +59,7 @@ public class PayCommand extends Command { if (user.getBalance(this.currency).compareTo(amount) < 0) { sender.sendMessage( - miniMessage.deserialize(this.getCurrencyMessageConfig(plugin, this.currency).notEnoughMoney) + miniMessage.deserialize(getCurrencyMessageConfig(plugin, this.currency).notEnoughMoney) ); return; @@ -80,7 +78,7 @@ public class PayCommand extends Command { } catch (CannotBeGreaterThan e) { sender.sendMessage( miniMessage.deserialize( - this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) ) ); @@ -89,12 +87,12 @@ public class PayCommand extends Command { } String template = tax.compareTo(BigDecimal.ZERO) > 0 - ? this.getCurrencyMessageConfig(plugin, this.currency).payWithTax - : this.getCurrencyMessageConfig(plugin, this.currency).pay; + ? getCurrencyMessageConfig(plugin, this.currency).payWithTax + : getCurrencyMessageConfig(plugin, this.currency).pay; String templateReceived = tax.compareTo(BigDecimal.ZERO) > 0 - ? this.getCurrencyMessageConfig(plugin, this.currency).payReceivedWithTax - : this.getCurrencyMessageConfig(plugin, this.currency).payReceived; + ? getCurrencyMessageConfig(plugin, this.currency).payReceivedWithTax + : getCurrencyMessageConfig(plugin, this.currency).payReceived; sender.sendMessage( miniMessage.deserialize( diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java index 57ad8f7..06b3ef2 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java @@ -9,7 +9,6 @@ import com.mojang.brigadier.tree.CommandNode; import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; -import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument; import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; @@ -23,6 +22,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; public class SetCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); @@ -42,11 +42,9 @@ public class SetCommand extends Command { final LightEcoPlugin plugin = context.getSource().plugin(); final CommandSender sender = context.getSource().sender(); - final User target = OfflineUserArgument.getOfflineUser(context, "target"); - if (target == null || target.getUsername() == null) { - OfflineUserArgument.handleMissing(context, "target"); + final User target = getUser(context); + if (target == null) return; - } BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 ? context.getArgument("amount", Double.class) @@ -59,7 +57,7 @@ public class SetCommand extends Command { } catch (CannotBeGreaterThan e) { sender.sendMessage( miniMessage.deserialize( - this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) ) ); @@ -69,7 +67,7 @@ public class SetCommand extends Command { sender.sendMessage( miniMessage.deserialize( - this.getCurrencyMessageConfig(plugin, this.currency).set, + getCurrencyMessageConfig(plugin, this.currency).set, Placeholder.parsed("currency", currency.getIdentifier()), Placeholder.parsed("target", target.getUsername()), Placeholder.parsed("amount", amount.toPlainString()) diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java index abad5d7..75187b1 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java @@ -9,7 +9,6 @@ import com.mojang.brigadier.tree.CommandNode; import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; -import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument; import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; @@ -23,6 +22,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; public class TakeCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); @@ -42,11 +42,9 @@ public class TakeCommand extends Command { final LightEcoPlugin plugin = context.getSource().plugin(); final CommandSender sender = context.getSource().sender(); - final User target = OfflineUserArgument.getOfflineUser(context, "target"); - if (target == null || target.getUsername() == null) { - OfflineUserArgument.handleMissing(context, "target"); + final User target = getUser(context); + if (target == null) return; - } BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 ? context.getArgument("amount", Double.class) @@ -59,7 +57,7 @@ public class TakeCommand extends Command { } catch (CannotBeGreaterThan e) { sender.sendMessage( miniMessage.deserialize( - this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) ) ); @@ -69,7 +67,7 @@ public class TakeCommand extends Command { sender.sendMessage( miniMessage.deserialize( - this.getCurrencyMessageConfig(plugin, this.currency).set, + getCurrencyMessageConfig(plugin, this.currency).set, Placeholder.parsed("currency", currency.getIdentifier()), Placeholder.parsed("target", target.getUsername()), Placeholder.parsed("amount", amount.toPlainString()) diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java index 6a918d2..1e0a5ef 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java @@ -9,5 +9,5 @@ public class MessageConfig extends OkaeriConfig { public Map currency = Collections.singletonMap("default", new CurrencyMessageConfig()); public String wait = "Please wait a moment before using this command again."; - public String invalidUser = "User not found."; + public String userNotFound = "User not found."; } diff --git a/test/paper/src/main/resources/plugin.yml b/test/paper/src/main/resources/plugin.yml index 9696301..31d3857 100644 --- a/test/paper/src/main/resources/plugin.yml +++ b/test/paper/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: ${name} description: ${description} version: ${version} -main: dev.xhyrom.lighteco.bukkittest.TestPlugin +main: dev.xhyrom.lighteco.test.paper.TestPlugin author: ${author} api-version: 1.20