mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-22 15:01:05 +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.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,36 +52,51 @@ 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
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
locks.add(sender.getUniqueId());
|
||||||
dispatcher.execute(parseResults);
|
|
||||||
} catch (CommandSyntaxException e) {
|
|
||||||
sender.sendMessage(Component.text(e.getRawMessage().getString(), NamedTextColor.RED));
|
|
||||||
|
|
||||||
if (e.getInput() != null && e.getCursor() >= 0) {
|
this.plugin.getBootstrap().getScheduler().async().execute(() -> {
|
||||||
int j = Math.min(e.getInput().length(), e.getCursor());
|
try {
|
||||||
|
dispatcher.execute(parseResults);
|
||||||
Component msg = Component.empty().color(NamedTextColor.GRAY).clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + name));
|
} catch (CommandSyntaxException e) {
|
||||||
|
this.sendError(sender, name, e);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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>";
|
||||||
|
|
|
@ -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.";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue