Command improvements

This commit is contained in:
Draycia committed 2020-10-14 02:02:58 -07:00
1 parent 07b3ee9708
commit 0f104ab212
13 files changed
+96 -84

No files matched your search

@@ -96,6 +96,7 @@ public class ChannelManager {
if (channel != null) {
this.registerChannel(channel);
// TODO: register AliasedChannelCommand for each channel
String prefix = null;
if (channel instanceof TextChannel) {
@@ -25,23 +25,20 @@ public class AliasedChannelCommand {
this.commandName = chatChannel.key();
commandManager.command(
commandManager.commandBuilder(this.commandName)
commandManager.commandBuilder(this.commandName,
commandManager.createDefaultCommandMeta())
.withSenderType(PlayerUser.class) // player
.withPermission("carbonchat.channel")
.handler(this::channel)
.build()
.withPermission("carbonchat.channel") // carbonchat.channel.message
.argument(StringArgument.<CarbonUser>newBuilder("message").greedy().asOptional().build())
.handler(context -> {
if (context.get("message").isPresent()) {
this.sendMessage(context);
} else {
this.channel(context);
}
})
.build() // TODO: use chain to register two executors
);
commandManager.command(
commandManager.commandBuilder(this.commandName)
.withSenderType(PlayerUser.class) // player
.withPermission("carbonchat.channel.message")
.argument(StringArgument.required("message"))
.handler(this::sendMessage)
.build()
);
// TODO: don't register same command twice lol
}
private void channel(@NonNull final CommandContext<CarbonUser> context) {
@@ -8,7 +8,7 @@ import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
@@ -38,7 +38,7 @@ public class ChannelListCommand {
.withSenderType(CarbonUser.class) // console & player
.withPermission("carbonchat.channellist")
// TODO: make this return a PlayerUser
.argument(CommandUtils.optionalChatUserArgument()) // carbonchat.channellist.other
.argument(PlayerUserArgument.optionalPlayerUserArgument()) // carbonchat.channellist.other
.handler(context -> {
if (context.get("user").isPresent()) {
this.channelListOther(context);
@@ -7,7 +7,7 @@ import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -30,7 +30,7 @@ public class IgnoreCommand {
commandManager.createDefaultCommandMeta())
.withSenderType(PlayerUser.class) // player
.withPermission("carbonchat.ignore")
.argument(CommandUtils.chatUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.handler(this::ignoreUser)
.build()
);
@@ -11,7 +11,7 @@ import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.api.users.UserChannelSettings;
import net.draycia.carbon.common.channels.CarbonWhisperChannel;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -36,7 +36,7 @@ public class MessageCommand {
commandManager.createDefaultCommandMeta())
.withSenderType(PlayerUser.class) // player
.withPermission("carbonchat.message")
.argument(CommandUtils.chatUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(StringArgument.<CarbonUser>newBuilder("message").greedy().asOptional().build())
.handler(this::sendMessage)
.build()
@@ -9,7 +9,7 @@ import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.api.users.UserChannelSettings;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -31,7 +31,7 @@ public class MsgToggleCommand {
commandManager.createDefaultCommandMeta())
.withSenderType(CarbonUser.class) // player & console
.withPermission("carbonchat.msgtoggle")
.argument(CommandUtils.optionalChatUserArgument()) // carbonchat.msgtoggle.other
.argument(PlayerUserArgument.optionalPlayerUserArgument()) // carbonchat.msgtoggle.other
.handler(context -> {
if (context.get("user").isPresent()) {
this.toggleOther(context);
@@ -7,7 +7,7 @@ import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -30,7 +30,7 @@ public class MuteCommand {
commandManager.createDefaultCommandMeta())
.withSenderType(CarbonUser.class) // player & console
.withPermission("carbonchat.mute")
.argument(CommandUtils.chatUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.handler(this::mute)
.build()
);
@@ -8,7 +8,7 @@ import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -31,7 +31,7 @@ public class NicknameCommand {
.withSenderType(CarbonUser.class) // player & console
.withPermission("carbonchat.nickname")
.argument(StringArgument.required("nickname"))
.argument(CommandUtils.optionalChatUserArgument()) // carbonchat.nickname.other
.argument(PlayerUserArgument.optionalPlayerUserArgument()) // carbonchat.nickname.other
.handler(context -> {
if (context.get("user").isPresent()) {
this.nicknameOther(context);
@@ -7,7 +7,7 @@ import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -30,7 +30,7 @@ public class ShadowMuteCommand {
commandManager.createDefaultCommandMeta())
.withSenderType(CarbonUser.class) // player & console
.withPermission("carbonchat.shadowmute")
.argument(CommandUtils.chatUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.handler(this::shadowMute)
.build()
);
@@ -10,7 +10,7 @@ import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.common.commands.arguments.ChannelArgument;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -30,9 +30,9 @@ public class SudoChannelCommand {
commandManager.command(
commandManager.commandBuilder(commandSettings.name(), commandSettings.aliases(),
commandManager.createDefaultCommandMeta())
.withSenderType(CarbonUser.class) // player
.withSenderType(CarbonUser.class) // player & console
.withPermission("carbonchat.channel.others.message")
.argument(CommandUtils.chatUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(ChannelArgument.requiredChannelArgument())
.argument(StringArgument.<CarbonUser>newBuilder("message").greedy().asOptional().build())
.handler(context -> {
@@ -10,7 +10,7 @@ import net.draycia.carbon.api.commands.settings.CommandSettings;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.api.users.UserChannelSettings;
import net.draycia.carbon.common.commands.arguments.ChannelArgument;
import net.draycia.carbon.common.utils.CommandUtils;
import net.draycia.carbon.common.commands.arguments.PlayerUserArgument;
import net.kyori.adventure.identity.Identity;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -33,7 +33,7 @@ public class ToggleCommand {
.withSenderType(CarbonUser.class) // player & console
.withPermission("carbonchat.toggle")
.argument(ChannelArgument.requiredChannelArgument())
.argument(CommandUtils.optionalChatUserArgument()) // carbonchat.toggle.other
.argument(PlayerUserArgument.optionalPlayerUserArgument()) // carbonchat.toggle.other
.handler(context -> {
if (context.get("user").isPresent()) {
this.toggleOther(context);
@@ -0,0 +1,64 @@
package net.draycia.carbon.common.commands.arguments;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.parser.ArgumentParseResult;
import com.intellectualsites.commands.context.CommandContext;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.users.PlayerUser;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
import java.util.Queue;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public final class PlayerUserArgument<C> extends CommandArgument<C, PlayerUser> {
private PlayerUserArgument(final boolean required) {
super(required, "channel", PlayerUserArgument::parser,
"",
PlayerUser.class, PlayerUserArgument::suggestions);
}
private static <C> ArgumentParseResult<PlayerUser> parser(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<String> inputs) {
final String input = inputs.poll();
if (input == null) {
// TODO: make exception messages configurable
return ArgumentParseResult.failure(new IllegalArgumentException("Player not found!"));
}
final UUID uuid = CarbonChatProvider.carbonChat().userService().resolve(input);
if (uuid == null) {
return ArgumentParseResult.failure(new IllegalArgumentException("Player not found!"));
}
final PlayerUser user = CarbonChatProvider.carbonChat().userService().wrap(uuid);
if (user == null) {
return ArgumentParseResult.failure(new IllegalArgumentException("Player not found!"));
}
return ArgumentParseResult.success(user);
}
private static <C> List<String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
return StreamSupport
.stream(CarbonChatProvider.carbonChat().userService().onlineUsers().spliterator(), false)
.map(PlayerUser::name)
.collect(Collectors.toList());
}
public static <C> PlayerUserArgument<C> requiredPlayerUserArgument() {
return new PlayerUserArgument<>(true);
}
public static <C> PlayerUserArgument<C> optionalPlayerUserArgument() {
return new PlayerUserArgument<>(false);
}
}
@@ -1,50 +0,0 @@
package net.draycia.carbon.common.utils;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.parser.ArgumentParseResult;
import com.intellectualsites.commands.context.CommandContext;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.users.UserService;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Queue;
public final class CommandUtils {
private CommandUtils() {
}
public static CommandArgument<CarbonUser, CarbonUser> optionalChatUserArgument() {
return CommandArgument.<CarbonUser, CarbonUser>ofType(CarbonUser.class, "user")
.asOptional()
.withParser(CommandUtils::parse)
.build();
}
// TODO: turn this into a proper class that handles suggestions
public static CommandArgument<CarbonUser, CarbonUser> chatUserArgument() {
return CommandArgument.<CarbonUser, CarbonUser>ofType(CarbonUser.class, "user")
.asRequired()
.withParser(CommandUtils::parse)
.build();
}
private static @NonNull ArgumentParseResult<CarbonUser> parse(final CommandContext<CarbonUser> c, final Queue<String> i) {
final String input = i.poll();
if (input == null) {
return ArgumentParseResult.failure(new IllegalArgumentException("Player cannot be null"));
}
final UserService<?> userService = CarbonChatProvider.carbonChat().userService();
final CarbonUser user = userService.wrap(userService.resolve(input));
if (user != null) {
return ArgumentParseResult.success(user);
} else {
return ArgumentParseResult.failure(new IllegalArgumentException("Player does not exist"));
}
}
}