Search in sources :

Example 1 with Channel

use of com.easterlyn.chat.channel.Channel in project Easterlyn by Easterlyn.

the class EasterlynChat method loadChannel.

private boolean loadChannel(@NotNull String name, @Nullable ConfigurationSection data) {
    if (data == null) {
        return false;
    }
    String className = data.getString("class");
    if (className == null) {
        return false;
    }
    Class<?> channelClass;
    try {
        channelClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        return false;
    }
    if (!Channel.class.isAssignableFrom(channelClass)) {
        return false;
    }
    Constructor<?> channelConstructor;
    try {
        channelConstructor = channelClass.getConstructor(String.class, UUID.class);
    } catch (NoSuchMethodException e) {
        return false;
    }
    String uuidString = data.getString("owner");
    if (uuidString == null) {
        return false;
    }
    UUID owner;
    try {
        owner = UUID.fromString(uuidString);
    } catch (Exception e) {
        return false;
    }
    Channel channel;
    try {
        channel = (Channel) channelConstructor.newInstance(name, owner);
    } catch (ReflectiveOperationException e) {
        return false;
    }
    if (!channel.isRecentlyAccessed()) {
        return false;
    }
    channel.load(data);
    channels.put(name, channel);
    return true;
}
Also used : NormalChannel(com.easterlyn.chat.channel.NormalChannel) Channel(com.easterlyn.chat.channel.Channel) SecretChannel(com.easterlyn.chat.channel.SecretChannel) UUID(java.util.UUID)

Example 2 with Channel

use of com.easterlyn.chat.channel.Channel in project Easterlyn by Easterlyn.

the class MessageCommand method sendMessage.

@CommandAlias("message|msg|m|whisper|w|pm|tell|t")
@Description("{@@chat.commands.message.description}")
@CommandPermission("easterlyn.command.message")
@Syntax("<recipient> <message>")
@CommandCompletion("@player")
public void sendMessage(BukkitCommandIssuer sender, @Flags(CoreContexts.ONLINE) User target, String message) {
    User issuer;
    if (sender.isPlayer()) {
        issuer = core.getUserManager().getUser(sender.getUniqueId());
    } else {
        Map<String, String> userData = new HashMap<>();
        userData.put("name", sender.getIssuer().getName());
        issuer = new AutoUser(core, userData);
        // For the purpose of allowing replies to console, set target's reply target.
        replies.put(target.getUniqueId(), issuer);
    }
    replies.put(issuer.getUniqueId(), target);
    Channel channel = chat.getChannels().get("pm");
    if (channel == null) {
        ReportableEvent.call("Channel #pm not set up when executing /message!");
        core.getLocaleManager().sendMessage(sender.getIssuer(), "chat.commands.message.error.pm_channel");
        return;
    }
    List<UUID> recipients = new ArrayList<>();
    if (!(issuer instanceof AutoUser)) {
        recipients.add(issuer.getUniqueId());
    }
    if (!(target instanceof AutoUser)) {
        recipients.add(target.getUniqueId());
    }
    new UserChatEvent(issuer, channel, target.getDisplayName() + ": " + message).send(recipients);
}
Also used : User(com.easterlyn.user.User) AutoUser(com.easterlyn.user.AutoUser) HashMap(java.util.HashMap) UserChatEvent(com.easterlyn.chat.event.UserChatEvent) AutoUser(com.easterlyn.user.AutoUser) Channel(com.easterlyn.chat.channel.Channel) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Description(co.aikar.commands.annotation.Description) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission) CommandAlias(co.aikar.commands.annotation.CommandAlias)

Example 3 with Channel

use of com.easterlyn.chat.channel.Channel in project Easterlyn by Easterlyn.

the class ChannelManagementListener method onPlayerQuit.

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
    RegisteredServiceProvider<EasterlynCore> easterlynProvider = chat.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynProvider == null) {
        return;
    }
    User user = easterlynProvider.getProvider().getUserManager().getUser(event.getPlayer().getUniqueId());
    List<String> savedChannels = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
    List<String> channels = savedChannels.stream().filter(channelName -> {
        Channel channel = chat.getChannels().get(channelName);
        if (channel == null) {
            return false;
        }
        channel.getMembers().remove(user.getUniqueId());
        return true;
    }).collect(Collectors.toList());
    if (channels.size() != savedChannels.size()) {
        user.getStorage().set(EasterlynChat.USER_CHANNELS, channels);
        if (!channels.contains(user.getStorage().getString(EasterlynChat.USER_CURRENT))) {
            user.getStorage().set(EasterlynChat.USER_CURRENT, null);
        }
    }
}
Also used : UserChatEvent(com.easterlyn.chat.event.UserChatEvent) Date(java.util.Date) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) UserCreationEvent(com.easterlyn.event.UserCreationEvent) TextComponent(net.md_5.bungee.api.chat.TextComponent) EventHandler(org.bukkit.event.EventHandler) AutoUser(com.easterlyn.user.AutoUser) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) StringUtil(com.easterlyn.util.StringUtil) Map(java.util.Map) AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) Listener(org.bukkit.event.Listener) User(com.easterlyn.user.User) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) EasterlynCore(com.easterlyn.EasterlynCore) Collection(java.util.Collection) Channel(com.easterlyn.chat.channel.Channel) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) EventPriority(org.bukkit.event.EventPriority) StringJoiner(java.util.StringJoiner) PlayerQuitEvent(org.bukkit.event.player.PlayerQuitEvent) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) RegisteredServiceProvider(org.bukkit.plugin.RegisteredServiceProvider) EasterlynChat(com.easterlyn.EasterlynChat) AutoUser(com.easterlyn.user.AutoUser) User(com.easterlyn.user.User) EasterlynCore(com.easterlyn.EasterlynCore) Channel(com.easterlyn.chat.channel.Channel) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Channel

use of com.easterlyn.chat.channel.Channel 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");
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) EasterlynPlugin(com.easterlyn.plugin.EasterlynPlugin) MessageKey(co.aikar.locales.MessageKey) Text(net.md_5.bungee.api.chat.hover.content.Text) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) NormalChannel(com.easterlyn.chat.channel.NormalChannel) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) PermissionUtil(com.easterlyn.util.PermissionUtil) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) Supplier(java.util.function.Supplier) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) HashSet(java.util.HashSet) TextComponent(net.md_5.bungee.api.chat.TextComponent) IssuerAwareContextResolver(co.aikar.commands.contexts.IssuerAwareContextResolver) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Matcher(java.util.regex.Matcher) ChannelFlag(com.easterlyn.chat.command.ChannelFlag) BukkitCommandCompletionContext(co.aikar.commands.BukkitCommandCompletionContext) StringUtil(com.easterlyn.util.StringUtil) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer) Map(java.util.Map) ParsedText(com.easterlyn.util.text.ParsedText) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) User(com.easterlyn.user.User) MuteListener(com.easterlyn.chat.listener.MuteListener) Collection(java.util.Collection) PermissionDefault(org.bukkit.permissions.PermissionDefault) Channel(com.easterlyn.chat.channel.Channel) ReportableEvent(com.easterlyn.event.ReportableEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) SecretChannel(com.easterlyn.chat.channel.SecretChannel) Collectors(java.util.stream.Collectors) ChannelManagementListener(com.easterlyn.chat.listener.ChannelManagementListener) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) UserRank(com.easterlyn.user.UserRank) ChatColor(org.bukkit.ChatColor) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) RegisteredServiceProvider(org.bukkit.plugin.RegisteredServiceProvider) CommandCompletions(co.aikar.commands.CommandCompletions) CoreLang(com.easterlyn.command.CoreLang) Colors(com.easterlyn.util.Colors) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) User(com.easterlyn.user.User) Matcher(java.util.regex.Matcher) NormalChannel(com.easterlyn.chat.channel.NormalChannel) Channel(com.easterlyn.chat.channel.Channel) SecretChannel(com.easterlyn.chat.channel.SecretChannel) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) ParsedText(com.easterlyn.util.text.ParsedText) Text(net.md_5.bungee.api.chat.hover.content.Text) ParsedText(com.easterlyn.util.text.ParsedText) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) List(java.util.List) NormalChannel(com.easterlyn.chat.channel.NormalChannel) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext)

Example 5 with Channel

use of com.easterlyn.chat.channel.Channel in project Easterlyn by Easterlyn.

the class AetherCommand method aether.

@CommandAlias("aether")
@Description("{@@chat.commands.aether.description}")
@CommandPermission("easterlyn.command.aether")
@Syntax("<name> <message content>")
@CommandCompletion("")
public void aether(BukkitCommandIssuer issuer, @Single String name, String text) {
    Map<String, String> userData = new HashMap<>();
    userData.put("name", name);
    userData.put("color", issuer.isPlayer() ? core.getUserManager().getUser(issuer.getUniqueId()).getColor().getName() : Colors.RANK_HEAD_ADMIN.getName());
    Channel channel = chat.getChannels().get("aether");
    if (channel == null) {
        ReportableEvent.call("Channel #aether not set up when executing /aether!");
        return;
    }
    // TODO async
    new UserChatEvent(new AetherUser(userData), channel, text).send(EasterlynChat.DEFAULT.getMembers());
}
Also used : HashMap(java.util.HashMap) UserChatEvent(com.easterlyn.chat.event.UserChatEvent) Channel(com.easterlyn.chat.channel.Channel) Description(co.aikar.commands.annotation.Description) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission) CommandAlias(co.aikar.commands.annotation.CommandAlias)

Aggregations

Channel (com.easterlyn.chat.channel.Channel)9 User (com.easterlyn.user.User)6 UserChatEvent (com.easterlyn.chat.event.UserChatEvent)5 EasterlynCore (com.easterlyn.EasterlynCore)4 AutoUser (com.easterlyn.user.AutoUser)4 StringUtil (com.easterlyn.util.StringUtil)4 HashMap (java.util.HashMap)4 List (java.util.List)4 TextComponent (net.md_5.bungee.api.chat.TextComponent)4 CommandAlias (co.aikar.commands.annotation.CommandAlias)3 CommandCompletion (co.aikar.commands.annotation.CommandCompletion)3 CommandPermission (co.aikar.commands.annotation.CommandPermission)3 Description (co.aikar.commands.annotation.Description)3 Syntax (co.aikar.commands.annotation.Syntax)3 EasterlynChat (com.easterlyn.EasterlynChat)3 NormalChannel (com.easterlyn.chat.channel.NormalChannel)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Date (java.util.Date)3