mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-22 15:01:05 +01:00
feat: handle missing user
This commit is contained in:
parent
4b14501c7c
commit
f677ab002f
7 changed files with 26 additions and 1 deletions
|
@ -5,8 +5,11 @@ import com.mojang.brigadier.arguments.ArgumentType;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
import dev.xhyrom.lighteco.common.command.CommandSource;
|
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.model.user.User;
|
||||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
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;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@ -16,9 +19,14 @@ public class OfflineUserArgument implements ArgumentType<String> {
|
||||||
public static User getOfflineUser(CommandContext<CommandSource> context, String name) {
|
public static User getOfflineUser(CommandContext<CommandSource> context, String name) {
|
||||||
String userName = context.getArgument(name, String.class);
|
String userName = context.getArgument(name, String.class);
|
||||||
LightEcoPlugin plugin = context.getSource().plugin();
|
LightEcoPlugin plugin = context.getSource().plugin();
|
||||||
|
CommandSender sender = context.getSource().sender();
|
||||||
|
|
||||||
UUID uniqueId = plugin.getBootstrap().lookupUniqueId(userName).orElse(null);
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +36,17 @@ public class OfflineUserArgument implements ArgumentType<String> {
|
||||||
return plugin.getUserManager().loadUser(uniqueId).join();
|
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
|
@Override
|
||||||
public String parse(StringReader reader) throws CommandSyntaxException {
|
public String parse(StringReader reader) throws CommandSyntaxException {
|
||||||
return reader.readString();
|
return reader.readString();
|
||||||
|
|
|
@ -47,6 +47,7 @@ public class BalanceCommand extends Command {
|
||||||
|
|
||||||
User target = OfflineUserArgument.getOfflineUser(context, "target");
|
User target = OfflineUserArgument.getOfflineUser(context, "target");
|
||||||
if (target == null || target.getUsername() == null) {
|
if (target == null || target.getUsername() == null) {
|
||||||
|
OfflineUserArgument.handleMissing(context, "target");
|
||||||
return SINGLE_SUCCESS;
|
return SINGLE_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ public class GiveCommand extends Command {
|
||||||
|
|
||||||
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
||||||
if (target == null || target.getUsername() == null) {
|
if (target == null || target.getUsername() == null) {
|
||||||
|
OfflineUserArgument.handleMissing(context, "target");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ public class PayCommand extends Command {
|
||||||
|
|
||||||
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
||||||
if (target == null || target.getUsername() == null) {
|
if (target == null || target.getUsername() == null) {
|
||||||
|
OfflineUserArgument.handleMissing(context, "target");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ public class SetCommand extends Command {
|
||||||
|
|
||||||
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
||||||
if (target == null || target.getUsername() == null) {
|
if (target == null || target.getUsername() == null) {
|
||||||
|
OfflineUserArgument.handleMissing(context, "target");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ public class TakeCommand extends Command {
|
||||||
|
|
||||||
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
final User target = OfflineUserArgument.getOfflineUser(context, "target");
|
||||||
if (target == null || target.getUsername() == null) {
|
if (target == null || target.getUsername() == null) {
|
||||||
|
OfflineUserArgument.handleMissing(context, "target");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,4 +9,5 @@ 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.";
|
public String wait = "<red>Please wait a moment before using this command again.";
|
||||||
|
public String invalidUser = "<red>User <username> not found.";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue