Allow changing channel command names, aliases, and registration

This commit is contained in:
Draycia committed 2021-11-30 18:06:45 -08:00
1 parent fb8c773494
commit 18f2826e0e
4 files changed
+82 -9

No files matched your search

@@ -87,6 +87,30 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
*/ */
@Nullable String quickPrefix(); @Nullable String quickPrefix();
/**
* If commands should be registered for this channel.
*
* @return if commands should be registered for this channel.
* @since 2.0.0
*/
boolean shouldRegisterCommands();
/**
* The text that can be used to refer to this channel in commands.
*
* @return this channel's name when used in commands
* @since 2.0.0
*/
String commandName();
/**
* Alternative command names for this channel.
*
* @return alternative command names
* @since 2.0.0
*/
List<String> commandAliases();
/** /**
* Represents the result of a channel permission check. * Represents the result of a channel permission check.
* *
@@ -22,6 +22,7 @@ package net.draycia.carbon.common.channels;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.CarbonChat;
@@ -77,6 +78,21 @@ public final class BasicChatChannel implements ChatChannel {
return null; return null;
} }
@Override
public boolean shouldRegisterCommands() {
return false;
}
@Override
public String commandName() {
return "basic";
}
@Override
public List<String> commandAliases() {
return Collections.emptyList();
}
@Override @Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer) { public ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer) {
return ChannelPermissionResult.allowed(); return ChannelPermissionResult.allowed();
@@ -123,21 +123,33 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> implem
} }
try (final Stream<Path> paths = Files.walk(channelDirectory)) { try (final Stream<Path> paths = Files.walk(channelDirectory)) {
final CommandManager<Commander> commandManager = this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {})); final CommandManager<Commander> commandManager =
this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<CommandManager<Commander>>() {}));
paths.forEach(path -> { paths.forEach(path -> {
final String fileName = path.getFileName().toString(); final String fileName = path.getFileName().toString();
if (fileName.endsWith(".conf")) { if (!fileName.endsWith(".conf")) {
return;
}
final @Nullable ChatChannel channel = this.loadChannel(path); final @Nullable ChatChannel channel = this.loadChannel(path);
if (channel != null) { if (channel == null) {
return;
}
final Key channelKey = channel.key(); final Key channelKey = channel.key();
this.logger.info("Registering channel with key [" + channelKey + "]"); this.logger.info("Registering channel with key [" + channelKey + "]");
register(channelKey, channel); register(channelKey, channel);
final var command = commandManager.commandBuilder(channelKey.value()) if (!channel.shouldRegisterCommands()) {
return;
}
final var command = commandManager.commandBuilder(channel.commandName(),
channel.commandAliases(), commandManager.createDefaultCommandMeta())
.argument(StringArgument.<Commander>newBuilder("message").greedy().asOptional().build()) .argument(StringArgument.<Commander>newBuilder("message").greedy().asOptional().build())
.permission("carbon.channel." + channelKey) .permission("carbon.channel." + channelKey)
.senderType(PlayerCommander.class) .senderType(PlayerCommander.class)
@@ -174,8 +186,6 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> implem
.build(); .build();
commandManager.command(channelCommand); commandManager.command(channelCommand);
}
}
}); });
} catch (final IOException exception) { } catch (final IOException exception) {
exception.printStackTrace(); exception.printStackTrace();
@@ -21,6 +21,7 @@ package net.draycia.carbon.common.channels;
import io.leangen.geantyref.TypeToken; import io.leangen.geantyref.TypeToken;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@@ -66,7 +67,7 @@ public final class ConfigChatChannel implements ChatChannel {
You only need to change the second part of the key. "global" by default. You only need to change the second part of the key. "global" by default.
The value is what's used in commands, this is probably what you want to change. The value is what's used in commands, this is probably what you want to change.
""") """)
private Key key = Key.key("carbon", "basic"); private @Nullable Key key = Key.key("carbon", "basic");
@Comment(""" @Comment("""
The permission required to use the channel. The permission required to use the channel.
@@ -74,15 +75,21 @@ public final class ConfigChatChannel implements ChatChannel {
To send messages you must have the permission carbon.channel.basic.speak To send messages you must have the permission carbon.channel.basic.speak
If you want to give both, grant carbon.channel.basic or carbon.channel.basic.* If you want to give both, grant carbon.channel.basic or carbon.channel.basic.*
""") """)
private String permission = "carbon.channel.basic"; private @Nullable String permission = "carbon.channel.basic";
@Setting("format") @Setting("format")
@Comment("The chat formats for this channel.") @Comment("The chat formats for this channel.")
private ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource(); private @Nullable ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource();
@Comment("Messages will be sent in this channel if they start with this prefix.") @Comment("Messages will be sent in this channel if they start with this prefix.")
private @Nullable String quickPrefix = ""; private @Nullable String quickPrefix = "";
private @Nullable Boolean shouldRegisterCommands = true;
private @Nullable String commandName = null;
private @Nullable List<String> commandAliases = Collections.emptyList();
private transient @Nullable ConfigChannelMessageService messageService = null; private transient @Nullable ConfigChannelMessageService messageService = null;
@Override @Override
@@ -94,6 +101,22 @@ public final class ConfigChatChannel implements ChatChannel {
return this.quickPrefix; return this.quickPrefix;
} }
@Override
public boolean shouldRegisterCommands() {
return Objects.requireNonNullElse(this.shouldRegisterCommands, true);
}
@Override
public String commandName() {
return Objects.requireNonNullElse(this.commandName, this.key.value());
}
@Override
public List<String> commandAliases() {
return Objects.requireNonNullElse(this.commandAliases, Collections.emptyList());
}
@Override @Override
public @NotNull RenderedMessage render( public @NotNull RenderedMessage render(
final CarbonPlayer sender, final CarbonPlayer sender,