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

feat: handle missing user

This commit is contained in:
Jozef Steinhübl 2024-07-15 23:58:40 +02:00
parent 4b14501c7c
commit f677ab002f
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
7 changed files with 26 additions and 1 deletions

View file

@ -5,8 +5,11 @@ import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.xhyrom.lighteco.common.command.CommandSource;
import dev.xhyrom.lighteco.common.model.chat.CommandSender;
import dev.xhyrom.lighteco.common.model.user.User;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.util.UUID;
@ -16,9 +19,14 @@ public class OfflineUserArgument implements ArgumentType<String> {
public static User getOfflineUser(CommandContext<CommandSource> context, String name) {
String userName = context.getArgument(name, String.class);
LightEcoPlugin plugin = context.getSource().plugin();
CommandSender sender = context.getSource().sender();
UUID uniqueId = plugin.getBootstrap().lookupUniqueId(userName).orElse(null);
if (uniqueId == null || plugin.getCommandManager().getLocks().contains(uniqueId)) {
if (uniqueId == null) {
return null;
}
if (sender.getUniqueId() != uniqueId && plugin.getCommandManager().getLocks().contains(uniqueId)) {
return null;
}
@ -28,6 +36,17 @@ public class OfflineUserArgument implements ArgumentType<String> {
return plugin.getUserManager().loadUser(uniqueId).join();
}
public static void handleMissing(CommandContext<CommandSource> context, String name) {
String userName = context.getArgument(name, String.class);
LightEcoPlugin plugin = context.getSource().plugin();
CommandSender sender = context.getSource().sender();
sender.sendMessage(MiniMessage.miniMessage().deserialize(
plugin.getConfig().messages.invalidUser,
Placeholder.parsed("username", userName)
));
}
@Override
public String parse(StringReader reader) throws CommandSyntaxException {
return reader.readString();

View file

@ -47,6 +47,7 @@ public class BalanceCommand extends Command {
User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
return SINGLE_SUCCESS;
}

View file

@ -44,6 +44,7 @@ public class GiveCommand extends Command {
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
return;
}

View file

@ -44,6 +44,7 @@ public class PayCommand extends Command {
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
return;
}

View file

@ -44,6 +44,7 @@ public class SetCommand extends Command {
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
return;
}

View file

@ -44,6 +44,7 @@ public class TakeCommand extends Command {
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
return;
}

View file

@ -9,4 +9,5 @@ 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.";
public String invalidUser = "<red>User <username> not found.";
}