From 76477aa3f82849eae276eca1b318e1e196132a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Thu, 11 Jul 2024 22:08:06 +0200 Subject: [PATCH] feat: locks --- .../common/command/CommandManager.java | 66 ++++++++++++------- .../argument/type/OfflineUserArgument.java | 5 +- .../config/message/CurrencyMessageConfig.java | 1 - .../common/config/message/MessageConfig.java | 2 + 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java index 6e1b6c4..9248e88 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java @@ -16,11 +16,18 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.minimessage.MiniMessage; + +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CopyOnWriteArrayList; public class CommandManager { protected final LightEcoPlugin plugin; @Getter private final CommandDispatcher dispatcher = new CommandDispatcher<>(); + @Getter + private final List locks = new CopyOnWriteArrayList<>(); public CommandManager(LightEcoPlugin plugin) { this.plugin = plugin; @@ -45,36 +52,51 @@ public class CommandManager { } public void execute(CommandSender sender, String name, String[] args) { + if (locks.contains(sender.getUniqueId())) { + sender.sendMessage(MiniMessage.miniMessage().deserialize( + this.plugin.getConfig().messages.wait + )); + return; + } + final CommandSource source = new CommandSource(this.plugin, sender); final ParseResults parseResults = dispatcher.parse( name + (args.length > 0 ? " " + String.join(" ", args) : ""), source ); - try { - dispatcher.execute(parseResults); - } catch (CommandSyntaxException e) { - sender.sendMessage(Component.text(e.getRawMessage().getString(), NamedTextColor.RED)); + locks.add(sender.getUniqueId()); - if (e.getInput() != null && e.getCursor() >= 0) { - int j = Math.min(e.getInput().length(), e.getCursor()); - - Component msg = Component.empty().color(NamedTextColor.GRAY).clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + name)); - - if (j > 10) { - msg = msg.append(Component.text("...")); - } - - msg = msg.append(Component.text(e.getInput().substring(Math.max(0, j - 10), j))); - - if (j < e.getInput().length()) { - Component component = Component.text(e.getInput().substring(j)).color(NamedTextColor.RED).decorate(TextDecoration.UNDERLINED); - msg = msg.append(component); - } - - msg = msg.append(Component.translatable("command.context.here").color(NamedTextColor.RED).decorate(TextDecoration.ITALIC)); - sender.sendMessage(msg); + this.plugin.getBootstrap().getScheduler().async().execute(() -> { + try { + dispatcher.execute(parseResults); + } catch (CommandSyntaxException e) { + this.sendError(sender, name, e); } + }); + } + + private void sendError(CommandSender sender, String name, CommandSyntaxException e) { + sender.sendMessage(Component.text(e.getRawMessage().getString(), NamedTextColor.RED)); + + if (e.getInput() != null && e.getCursor() >= 0) { + int j = Math.min(e.getInput().length(), e.getCursor()); + + Component msg = Component.empty().color(NamedTextColor.GRAY).clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + name)); + + if (j > 10) { + msg = msg.append(Component.text("...")); + } + + msg = msg.append(Component.text(e.getInput().substring(Math.max(0, j - 10), j))); + + if (j < e.getInput().length()) { + Component component = Component.text(e.getInput().substring(j)).color(NamedTextColor.RED).decorate(TextDecoration.UNDERLINED); + msg = msg.append(component); + } + + msg = msg.append(Component.translatable("command.context.here").color(NamedTextColor.RED).decorate(TextDecoration.ITALIC)); + sender.sendMessage(msg); } } } 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 ca81e99..0615fda 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 @@ -18,10 +18,13 @@ public class OfflineUserArgument implements ArgumentType { LightEcoPlugin plugin = context.getSource().plugin(); UUID uniqueId = plugin.getBootstrap().lookupUniqueId(userName).orElse(null); - if (uniqueId == null) { + if (uniqueId == null || plugin.getCommandManager().getLocks().contains(uniqueId)) { return null; } + // Lock the user to prevent race conditions + plugin.getCommandManager().getLocks().add(uniqueId); + return plugin.getUserManager().loadUser(uniqueId).join(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java index 00380ce..d5589a2 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java @@ -17,7 +17,6 @@ public class CurrencyMessageConfig extends OkaeriConfig { public String payReceived = " | Received from "; public String payReceivedWithTax = " | Received from ( after tax)"; - public String wait = "Please wait a moment before using this command again."; public String notEnoughMoney = "You don't have enough money!"; public String cannotPaySelf = "You cannot pay yourself!"; public String cannotBeGreaterThan = "Amount cannot be greater than "; 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 88be91a..bb9def7 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 @@ -7,4 +7,6 @@ import java.util.Map; 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."; }