Search in sources :

Example 6 with Channel

use of com.easterlyn.chat.channel.Channel 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);
    });
}
Also used : CommandAlias(co.aikar.commands.annotation.CommandAlias) Date(java.util.Date) NormalChannel(com.easterlyn.chat.channel.NormalChannel) CoreContexts(com.easterlyn.command.CoreContexts) SimpleDateFormat(java.text.SimpleDateFormat) Optional(co.aikar.commands.annotation.Optional) Player(org.bukkit.entity.Player) TextComponent(net.md_5.bungee.api.chat.TextComponent) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) Private(co.aikar.commands.annotation.Private) StringUtil(com.easterlyn.util.StringUtil) AddRemove(com.easterlyn.util.command.AddRemove) CommandPermission(co.aikar.commands.annotation.CommandPermission) Description(co.aikar.commands.annotation.Description) Flags(co.aikar.commands.annotation.Flags) Bukkit(org.bukkit.Bukkit) Subcommand(co.aikar.commands.annotation.Subcommand) User(com.easterlyn.user.User) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) BaseCommand(co.aikar.commands.BaseCommand) EasterlynCore(com.easterlyn.EasterlynCore) Channel(com.easterlyn.chat.channel.Channel) Single(co.aikar.commands.annotation.Single) List(java.util.List) Dependency(co.aikar.commands.annotation.Dependency) Syntax(co.aikar.commands.annotation.Syntax) EasterlynChat(com.easterlyn.EasterlynChat) TextComponent(net.md_5.bungee.api.chat.TextComponent) Player(org.bukkit.entity.Player) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) Description(co.aikar.commands.annotation.Description) Subcommand(co.aikar.commands.annotation.Subcommand) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) Syntax(co.aikar.commands.annotation.Syntax)

Example 7 with Channel

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

the class ChannelManagementListener method addMainChannel.

private void addMainChannel(@NotNull User user, @Nullable Collection<String> channels) {
    String channelName = user.getStorage().getString(EasterlynChat.USER_CURRENT);
    Channel current = channelName == null ? null : chat.getChannels().get(channelName);
    if (current == null && (channels == null || channels.isEmpty() || channels.contains(EasterlynChat.DEFAULT.getName()))) {
        user.getStorage().set(EasterlynChat.USER_CURRENT, EasterlynChat.DEFAULT.getName());
    }
    if (channels != null && !channels.isEmpty()) {
        return;
    }
    user.getStorage().set(EasterlynChat.USER_CHANNELS, Collections.singletonList(EasterlynChat.DEFAULT.getName()));
    EasterlynChat.DEFAULT.getMembers().add(user.getUniqueId());
}
Also used : Channel(com.easterlyn.chat.channel.Channel)

Example 8 with Channel

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

the class ChannelManagementListener method onPlayerChat.

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onPlayerChat(AsyncPlayerChatEvent event) {
    RegisteredServiceProvider<EasterlynCore> easterlynRSP = chat.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynRSP == null) {
        event.getPlayer().sendMessage("Easterlyn core plugin is not enabled! " + "Please report this to @Staff on Discord immediately!");
        return;
    }
    event.setCancelled(true);
    EasterlynCore core = easterlynRSP.getProvider();
    User user = core.getUserManager().getUser(event.getPlayer().getUniqueId());
    event.getPlayer().setDisplayName(user.getDisplayName());
    Channel channel = null;
    // #channel message parsing
    if (event.getMessage().length() > 0 && event.getMessage().charAt(0) == '#') {
        int space = event.getMessage().indexOf(' ');
        if (space == -1) {
            space = event.getMessage().length();
        }
        String channelName = event.getMessage().substring(1, space);
        if (space == event.getMessage().length()) {
            core.getLocaleManager().sendMessage(event.getPlayer(), "chat.common.no_content", "{value}", '#' + channelName);
            return;
        }
        channel = chat.getChannels().get(channelName);
        if (channel == null) {
            core.getLocaleManager().sendMessage(event.getPlayer(), "chat.common.no_matching_channel", "{value}", '#' + channelName);
            return;
        }
        if (!user.getStorage().getStringList(EasterlynChat.USER_CHANNELS).contains(channel.getName())) {
            core.getLocaleManager().sendMessage(event.getPlayer(), "chat.common.not_listening_to_channel", "{value}", channel.getDisplayName());
            return;
        }
        event.setMessage(event.getMessage().substring(space + 1));
    }
    // User's channel
    if (channel == null) {
        String currentChannelName = user.getStorage().getString(EasterlynChat.USER_CURRENT);
        if (currentChannelName != null) {
            channel = chat.getChannels().get(currentChannelName);
        }
        if (channel == null) {
            core.getLocaleManager().sendMessage(event.getPlayer(), "chat.common.not_listening_to_channel");
            return;
        }
    }
    new UserChatEvent(user, channel, event.getMessage()).send();
}
Also used : AutoUser(com.easterlyn.user.AutoUser) User(com.easterlyn.user.User) EasterlynCore(com.easterlyn.EasterlynCore) UserChatEvent(com.easterlyn.chat.event.UserChatEvent) Channel(com.easterlyn.chat.channel.Channel) EventHandler(org.bukkit.event.EventHandler)

Example 9 with Channel

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

the class ChannelManagementListener method onPlayerJoin.

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerJoin(PlayerJoinEvent event) {
    RegisteredServiceProvider<EasterlynCore> easterlynProvider = chat.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynProvider == null) {
        return;
    }
    EasterlynCore core = easterlynProvider.getProvider();
    User user = core.getUserManager().getUser(event.getPlayer().getUniqueId());
    event.getPlayer().setDisplayName(user.getDisplayName());
    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;
        }
        if (channel.isWhitelisted(user)) {
            channel.getMembers().add(user.getUniqueId());
            return true;
        }
        return false;
    }).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);
        }
    }
    addMainChannel(user, channels);
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
    String time = dateFormat.format(new Date());
    chat.getServer().getOnlinePlayers().forEach(player -> {
        User otherUser;
        if (player.getUniqueId().equals(user.getUniqueId())) {
            otherUser = user;
        } else {
            otherUser = core.getUserManager().getUser(player.getUniqueId());
        }
        BaseComponent component = new TextComponent();
        component.addExtra(user.getMention());
        String locale = core.getLocaleManager().getLocale(player);
        for (TextComponent textComponent : StringUtil.toJSON(core.getLocaleManager().getValue("chat.common.join", locale))) {
            component.addExtra(textComponent);
        }
        List<String> commonChannels = otherUser.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
        commonChannels.retainAll(channels);
        StringJoiner stringJoiner = new StringJoiner(", #", "#", " ").setEmptyValue("");
        for (String channel : commonChannels) {
            stringJoiner.add(channel);
        }
        String merged = stringJoiner.toString();
        if (commonChannels.size() > 1) {
            int lastComma = merged.lastIndexOf(',');
            int firstSegmentIndex = commonChannels.size() > 2 ? lastComma + 1 : lastComma;
            merged = merged.substring(0, firstSegmentIndex) + core.getLocaleManager().getValue("chat.common.and", locale) + merged.substring(lastComma + 1);
        }
        for (TextComponent textComponent : StringUtil.toJSON(merged)) {
            if (!textComponent.getText().isEmpty()) {
                component.addExtra(textComponent);
            }
        }
        for (TextComponent textComponent : StringUtil.toJSON(core.getLocaleManager().getValue("chat.common.at", locale, "{time}", time))) {
            component.addExtra(textComponent);
        }
        otherUser.sendMessage(component);
    });
    event.setJoinMessage("");
}
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) TextComponent(net.md_5.bungee.api.chat.TextComponent) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) AutoUser(com.easterlyn.user.AutoUser) User(com.easterlyn.user.User) EasterlynCore(com.easterlyn.EasterlynCore) Channel(com.easterlyn.chat.channel.Channel) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) StringJoiner(java.util.StringJoiner) EventHandler(org.bukkit.event.EventHandler)

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