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
+124 -51

No files matched your search

@@ -87,6 +87,30 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
*/
@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.
*
@@ -22,6 +22,7 @@ package net.draycia.carbon.common.channels;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import net.draycia.carbon.api.CarbonChat;
@@ -77,6 +78,21 @@ public final class BasicChatChannel implements ChatChannel {
return null;
}
@Override
public boolean shouldRegisterCommands() {
return false;
}
@Override
public String commandName() {
return "basic";
}
@Override
public List<String> commandAliases() {
return Collections.emptyList();
}
@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer) {
return ChannelPermissionResult.allowed();
@@ -123,59 +123,69 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> implem
}
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 -> {
final String fileName = path.getFileName().toString();
if (fileName.endsWith(".conf")) {
final @Nullable ChatChannel channel = this.loadChannel(path);
if (channel != null) {
final Key channelKey = channel.key();
this.logger.info("Registering channel with key [" + channelKey + "]");
register(channelKey, channel);
final var command = commandManager.commandBuilder(channelKey.value())
.argument(StringArgument.<Commander>newBuilder("message").greedy().asOptional().build())
.permission("carbon.channel." + channelKey)
.senderType(PlayerCommander.class)
.handler(handler -> {
final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
if (sender.muted(channel)) {
// TODO: "you are muted in this channel!!!!"
return;
}
if (handler.contains("message")) {
final String message = handler.get("message");
final var component = Component.text(message);
// TODO: trigger platform events related to chat
// TODO: also make sure carbon events are also emitted properly?
for (final var recipient : channel.recipients(sender)) {
final var renderedMessage = channel.render(sender, recipient, component, component);
recipient.sendMessage(renderedMessage.component(), renderedMessage.messageType());
}
} else {
sender.selectedChannel(channel);
this.messageService.changedChannels(sender, channel.key().value());
}
})
.build(); // TODO: command aliases
commandManager.command(command);
final var channelCommand = commandManager.commandBuilder("channel", "ch")
.literal(channelKey.value())
.proxies(command)
.build();
commandManager.command(channelCommand);
}
if (!fileName.endsWith(".conf")) {
return;
}
final @Nullable ChatChannel channel = this.loadChannel(path);
if (channel == null) {
return;
}
final Key channelKey = channel.key();
this.logger.info("Registering channel with key [" + channelKey + "]");
register(channelKey, channel);
if (!channel.shouldRegisterCommands()) {
return;
}
final var command = commandManager.commandBuilder(channel.commandName(),
channel.commandAliases(), commandManager.createDefaultCommandMeta())
.argument(StringArgument.<Commander>newBuilder("message").greedy().asOptional().build())
.permission("carbon.channel." + channelKey)
.senderType(PlayerCommander.class)
.handler(handler -> {
final var sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
if (sender.muted(channel)) {
// TODO: "you are muted in this channel!!!!"
return;
}
if (handler.contains("message")) {
final String message = handler.get("message");
final var component = Component.text(message);
// TODO: trigger platform events related to chat
// TODO: also make sure carbon events are also emitted properly?
for (final var recipient : channel.recipients(sender)) {
final var renderedMessage = channel.render(sender, recipient, component, component);
recipient.sendMessage(renderedMessage.component(), renderedMessage.messageType());
}
} else {
sender.selectedChannel(channel);
this.messageService.changedChannels(sender, channel.key().value());
}
})
.build(); // TODO: command aliases
commandManager.command(command);
final var channelCommand = commandManager.commandBuilder("channel", "ch")
.literal(channelKey.value())
.proxies(command)
.build();
commandManager.command(channelCommand);
});
} catch (final IOException exception) {
exception.printStackTrace();
@@ -21,6 +21,7 @@ package net.draycia.carbon.common.channels;
import io.leangen.geantyref.TypeToken;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
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.
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("""
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
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")
@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.")
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;
@Override
@@ -94,6 +101,22 @@ public final class ConfigChatChannel implements ChatChannel {
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
public @NotNull RenderedMessage render(
final CarbonPlayer sender,