1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-22 15:01:05 +01:00

feat: locks

This commit is contained in:
Jozef Steinhübl 2024-07-11 22:08:06 +02:00
parent 5bf14e840f
commit 76477aa3f8
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
4 changed files with 50 additions and 24 deletions

View file

@ -16,11 +16,18 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration; 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 { public class CommandManager {
protected final LightEcoPlugin plugin; protected final LightEcoPlugin plugin;
@Getter @Getter
private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>(); private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
@Getter
private final List<UUID> locks = new CopyOnWriteArrayList<>();
public CommandManager(LightEcoPlugin plugin) { public CommandManager(LightEcoPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -45,15 +52,31 @@ public class CommandManager {
} }
public void execute(CommandSender sender, String name, String[] args) { 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 CommandSource source = new CommandSource(this.plugin, sender);
final ParseResults<CommandSource> parseResults = dispatcher.parse( final ParseResults<CommandSource> parseResults = dispatcher.parse(
name + (args.length > 0 ? " " + String.join(" ", args) : ""), name + (args.length > 0 ? " " + String.join(" ", args) : ""),
source source
); );
locks.add(sender.getUniqueId());
this.plugin.getBootstrap().getScheduler().async().execute(() -> {
try { try {
dispatcher.execute(parseResults); dispatcher.execute(parseResults);
} catch (CommandSyntaxException e) { } 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)); sender.sendMessage(Component.text(e.getRawMessage().getString(), NamedTextColor.RED));
if (e.getInput() != null && e.getCursor() >= 0) { if (e.getInput() != null && e.getCursor() >= 0) {
@ -77,4 +100,3 @@ public class CommandManager {
} }
} }
} }
}

View file

@ -18,10 +18,13 @@ public class OfflineUserArgument implements ArgumentType<String> {
LightEcoPlugin plugin = context.getSource().plugin(); LightEcoPlugin plugin = context.getSource().plugin();
UUID uniqueId = plugin.getBootstrap().lookupUniqueId(userName).orElse(null); UUID uniqueId = plugin.getBootstrap().lookupUniqueId(userName).orElse(null);
if (uniqueId == null) { if (uniqueId == null || plugin.getCommandManager().getLocks().contains(uniqueId)) {
return null; return null;
} }
// Lock the user to prevent race conditions
plugin.getCommandManager().getLocks().add(uniqueId);
return plugin.getUserManager().loadUser(uniqueId).join(); return plugin.getUserManager().loadUser(uniqueId).join();
} }

View file

@ -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 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 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 notEnoughMoney = "<red>You don't have enough money!";
public String cannotPaySelf = "<red>You cannot pay yourself!"; public String cannotPaySelf = "<red>You cannot pay yourself!";
public String cannotBeGreaterThan = "<red>Amount cannot be greater than <gold><max>"; public String cannotBeGreaterThan = "<red>Amount cannot be greater than <gold><max>";

View file

@ -7,4 +7,6 @@ import java.util.Map;
public class MessageConfig extends OkaeriConfig { public class MessageConfig extends OkaeriConfig {
public Map<String, CurrencyMessageConfig> currency = Collections.singletonMap("default", new CurrencyMessageConfig()); public Map<String, CurrencyMessageConfig> currency = Collections.singletonMap("default", new CurrencyMessageConfig());
public String wait = "<red>Please wait a moment before using this command again.";
} }