use of com.easterlyn.chat.channel.NormalChannel in project Easterlyn by Easterlyn.
the class EasterlynChat method register.
@Override
protected void register(EasterlynCore plugin) {
StringUtil.addQuoteConsumer(new StaticQuoteConsumer(CHANNEL_PATTERN) {
@Override
public void addComponents(@NotNull ParsedText components, @NotNull Supplier<Matcher> matcherSupplier) {
Matcher matcher = matcherSupplier.get();
String channelName = matcher.group(1);
Channel channel = getChannels().get(channelName.toLowerCase().substring(1));
TextComponent component;
if (channel != null) {
component = channel.getMention();
} else {
int end = matcher.end(1);
component = new TextComponent(matcher.group().substring(0, end));
component.setColor(Colors.CHANNEL);
component.setUnderlined(true);
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(TextComponent.fromLegacyText(Colors.COMMAND + "/join " + Colors.CHANNEL + channelName))));
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + channelName));
}
components.addComponent(component);
String trailingPunctuation = matcher.group(2);
if (trailingPunctuation != null && !trailingPunctuation.isEmpty()) {
components.addText(trailingPunctuation, component.getHoverEvent(), component.getClickEvent());
}
}
});
IssuerAwareContextResolver<Channel, BukkitCommandExecutionContext> channelContext = context -> {
String firstArg = context.getFirstArg();
// Current channel or unspecified, defaulting to current
if (context.hasFlag(ChannelFlag.CURRENT) || (context.hasFlag(ChannelFlag.LISTENING_OR_CURRENT) || context.hasFlag(ChannelFlag.VISIBLE_OR_CURRENT)) && (firstArg == null || firstArg.indexOf('#') != 0)) {
if (!context.getIssuer().isPlayer()) {
// Console never has a current channel
throw new InvalidCommandArgument(CoreLang.NO_CONSOLE);
}
String channelName = plugin.getUserManager().getUser(context.getPlayer().getUniqueId()).getStorage().getString(USER_CURRENT);
Channel channel = channels.get(channelName);
if (channel == null) {
throw new InvalidCommandArgument(MessageKey.of("chat.common.no_current_channel"));
}
return channel;
}
// Channel name must be specified for anything beyond this point, pop argument
context.popFirstArg();
if (firstArg == null) {
throw new InvalidCommandArgument(MessageKey.of("chat.common.no_specified_channel"));
}
if (firstArg.length() == 0 || firstArg.charAt(0) != '#') {
throw new InvalidCommandArgument(MessageKey.of("chat.commands.channel.create.error.naming_conventions"));
}
Channel channel = channels.get(firstArg.substring(1).toLowerCase());
if (channel == null) {
throw new InvalidCommandArgument(MessageKey.of("chat.common.no_matching_channel"), "{value}", firstArg);
}
User user;
if (context.getIssuer().isPlayer()) {
user = getCore().getUserManager().getUser(context.getIssuer().getUniqueId());
} else {
user = null;
}
if (context.hasFlag(ChannelFlag.VISIBLE) || context.hasFlag(ChannelFlag.VISIBLE_OR_CURRENT)) {
if (user == null || !channel.isPrivate() || channel.isWhitelisted(user)) {
return channel;
}
throw new InvalidCommandArgument(MessageKey.of("chat.common.no_channel_access"), "{value}", channel.getDisplayName());
}
if (context.hasFlag(ChannelFlag.LISTENING) || context.hasFlag(ChannelFlag.LISTENING_OR_CURRENT)) {
if (user == null) {
return channel;
}
List<String> channels = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
if (!channels.contains(channel.getName())) {
throw new InvalidCommandArgument(MessageKey.of("chat.common.not_listening_to_channel"), "{value}", channel.getDisplayName());
}
return channel;
}
if (user == null) {
throw new InvalidCommandArgument(CoreLang.NO_CONSOLE);
}
if (context.hasFlag(ChannelFlag.NOT_LISTENING)) {
List<String> channels = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
if (channels.contains(channel.getName())) {
throw new InvalidCommandArgument(MessageKey.of("chat.common.listening_to_channel"), "{value}", channel.getDisplayName());
}
return channel;
}
ReportableEvent.call("Missing Channel context flag!", 10);
throw new InvalidCommandArgument(CoreLang.ERROR_LOGGED);
};
plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(Channel.class, channelContext);
plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(NormalChannel.class, context -> {
Channel channel = channelContext.getContext(context);
if (!(channel instanceof NormalChannel)) {
throw new InvalidCommandArgument(MessageKey.of("chat.common.channel_not_modifiable"));
}
return (NormalChannel) channel;
});
// TODO ACF is removing leading # from channel display names in completions
plugin.getCommandManager().getCommandCompletions().registerCompletion("channels", getUserHandler(user -> channels.values().stream().distinct().filter(channel -> channel.isWhitelisted(user)).map(Channel::getDisplayName).collect(Collectors.toSet())));
plugin.getCommandManager().getCommandCompletions().setDefaultCompletion("channels", Channel.class, NormalChannel.class);
plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsJoinable", getUserHandler(user -> {
List<String> channelsJoined = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
return channels.values().stream().distinct().filter(channel -> !channelsJoined.contains(channel.getName()) && channel.isWhitelisted(user)).map(Channel::getDisplayName).collect(Collectors.toSet());
}));
plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsListening", getUserHandler(user -> user.getStorage().getStringList(EasterlynChat.USER_CHANNELS)));
plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsModerated", getUserHandler(user -> channels.values().stream().distinct().filter(channel -> channel.isModerator(user)).map(Channel::getDisplayName).collect(Collectors.toSet())));
plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsOwned", getUserHandler(user -> channels.values().stream().distinct().filter(channel -> channel.isOwner(user)).map(Channel::getDisplayName).collect(Collectors.toSet())));
plugin.getLocaleManager().addLocaleSupplier(this);
plugin.registerCommands(this, getClassLoader(), "com.easterlyn.chat.command");
}
use of com.easterlyn.chat.channel.NormalChannel in project Easterlyn by Easterlyn.
the class ChannelCommand method delete.
@Subcommand("delete")
@Description("{@@chat.commands.channel.delete.description}")
@Syntax("<#channel>")
@CommandCompletion("@channelsOwned")
public void delete(@Flags(CoreContexts.SELF) User user, @Flags(ChannelFlag.VISIBLE_OR_CURRENT) NormalChannel channel, @Optional String name) {
if (!channel.isOwner(user)) {
core.getLocaleManager().sendMessage(user.getPlayer(), "chat.common.requires_owner");
return;
}
if (!channel.getDisplayName().equals(name)) {
core.getLocaleManager().sendMessage(user.getPlayer(), "chat.commands.channel.delete.error.confirm");
return;
}
chat.getChannels().remove(channel.getName());
channel.getMembers().stream().map(uuid -> core.getUserManager().getUser(uuid)).forEach(member -> {
List<String> channels = member.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
channels.remove(channel.getName());
member.getStorage().set(EasterlynChat.USER_CHANNELS, channels);
String currentChannelName = member.getStorage().getString(EasterlynChat.USER_CURRENT);
if (channel.getName().equals(currentChannelName) || currentChannelName == null) {
if (channels.contains(EasterlynChat.DEFAULT.getName())) {
member.getStorage().set(EasterlynChat.USER_CURRENT, EasterlynChat.DEFAULT.getName());
}
}
Player player = member.getPlayer();
if (player == null) {
return;
}
BaseComponent component = new TextComponent();
component.addExtra(channel.getMention());
String locale = core.getLocaleManager().getLocale(player);
for (TextComponent textComponent : StringUtil.toJSON(core.getLocaleManager().getValue("chat.commands.channel.delete.success", locale))) {
component.addExtra(textComponent);
}
player.spigot().sendMessage(component);
});
}
use of com.easterlyn.chat.channel.NormalChannel in project Easterlyn by Easterlyn.
the class ChannelCommand method create.
@Subcommand("create")
@Description("{@@chat.commands.channel.create.description}")
@Syntax("<#channelname> <private>")
@CommandCompletion("channelname")
@CommandPermission("easterlyn.command.channel.create")
public void create(@Flags(CoreContexts.SELF) User user, @Single String name) {
if (!user.hasPermission("easterlyn.command.channel.createany") && (name.length() < 2 || name.length() > 17 || name.charAt(0) != '#' || !StringUtil.stripNonAlphanumerics(name).equals(name.substring(1)))) {
core.getLocaleManager().sendMessage(user.getPlayer(), "chat.commands.channel.create.error.naming_conventions");
return;
}
if (name.length() > 1 && name.charAt(0) == '#') {
name = name.substring(1);
}
if (chat.getChannels().containsKey(name)) {
core.getLocaleManager().sendMessage(user.getPlayer(), "chat.commands.channel.create.error.duplicate");
return;
}
NormalChannel channel = new NormalChannel(name, user.getUniqueId());
chat.getChannels().put(name.toLowerCase(), channel);
Player player = user.getPlayer();
core.getLocaleManager().sendMessage(user.getPlayer(), "chat.commands.channel.create.success");
if (player != null) {
join(user, channel, channel.getPassword());
}
}
Aggregations