1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-12-22 20:41: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.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,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<CommandSource> 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);
}
}
}

View file

@ -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();
}

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 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>";

View file

@ -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.";
}