mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-10 01:18:07 +01:00
feat: locks
This commit is contained in:
parent
5bf14e840f
commit
76477aa3f8
4 changed files with 50 additions and 24 deletions
|
@ -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<CommandSource> dispatcher = new CommandDispatcher<>();
|
||||
@Getter
|
||||
private final List<UUID> locks = new CopyOnWriteArrayList<>();
|
||||
|
||||
public CommandManager(LightEcoPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
|
@ -45,15 +52,31 @@ 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<CommandSource> parseResults = dispatcher.parse(
|
||||
name + (args.length > 0 ? " " + String.join(" ", args) : ""),
|
||||
source
|
||||
);
|
||||
|
||||
locks.add(sender.getUniqueId());
|
||||
|
||||
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) {
|
||||
|
@ -76,5 +99,4 @@ public class CommandManager {
|
|||
sender.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,10 +18,13 @@ public class OfflineUserArgument implements ArgumentType<String> {
|
|||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@ public class CurrencyMessageConfig extends OkaeriConfig {
|
|||
public String payReceived = "<currency> <dark_gray>| <gray>Received <gold><amount> <yellow>from <gold><sender>";
|
||||
public String payReceivedWithTax = "<currency> <dark_gray>| <gray>Received <gold><amount> <yellow>from <gold><sender> <dark_gray>(<gold><taxed_amount> <yellow>after tax<dark_gray>)";
|
||||
|
||||
public String wait = "<red>Please wait a moment before using this command again.";
|
||||
public String notEnoughMoney = "<red>You don't have enough money!";
|
||||
public String cannotPaySelf = "<red>You cannot pay yourself!";
|
||||
public String cannotBeGreaterThan = "<red>Amount cannot be greater than <gold><max>";
|
||||
|
|
|
@ -7,4 +7,6 @@ import java.util.Map;
|
|||
|
||||
public class MessageConfig extends OkaeriConfig {
|
||||
public Map<String, CurrencyMessageConfig> currency = Collections.singletonMap("default", new CurrencyMessageConfig());
|
||||
|
||||
public String wait = "<red>Please wait a moment before using this command again.";
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue