Search in sources :

Example 1 with AutoUser

use of com.easterlyn.user.AutoUser in project Easterlyn by Easterlyn.

the class ListCommand method list.

@CommandAlias("list|ls")
@Description("{@@sink.module.list.description}")
@CommandPermission("easterlyn.command.list")
@Syntax("")
@CommandCompletion("")
public void list(BukkitCommandIssuer issuer) {
    Player sender = issuer.getPlayer();
    UserRank[] ranks = UserRank.values();
    Multimap<String, User> groupedUsers = HashMultimap.create();
    int total = 0;
    for (Player player : Bukkit.getOnlinePlayers()) {
        if (sender != null && !sender.canSee(player)) {
            continue;
        }
        ++total;
        for (int i = ranks.length - 1; i >= 0; --i) {
            UserRank rank = ranks[i];
            if (i == 0 || player.hasPermission(rank.getPermission())) {
                groupedUsers.put(rank.getFriendlyName(), core.getUserManager().getUser(player.getUniqueId()));
                break;
            }
        }
    }
    if (ThreadLocalRandom.current().nextDouble() < .001) {
        Map<String, String> easterEgg = new HashMap<>();
        easterEgg.put("name", "Herobrine");
        easterEgg.put("color", ChatColor.BLACK.toString());
        groupedUsers.put(UserRank.MEMBER.getFriendlyName(), new AutoUser(core, easterEgg));
    }
    issuer.sendInfo(MessageKey.of("sink.module.list.header"), "{value}", String.valueOf(total), "{max}", String.valueOf(Bukkit.getMaxPlayers()));
    for (int i = ranks.length - 1; i >= 0; --i) {
        String groupName = ranks[i].getFriendlyName();
        if (i > 0 && groupName.equals(ranks[i - 1].getFriendlyName()) || !groupedUsers.containsKey(groupName)) {
            continue;
        }
        Collection<User> users = groupedUsers.get(groupName);
        List<BaseComponent> components = new ArrayList<>(users.size() * 2 + 2);
        TextComponent component = new TextComponent(groupName);
        component.setColor(ranks[i].getColor());
        components.add(component);
        component = new TextComponent(": ");
        component.setColor(ChatColor.YELLOW);
        components.add(component);
        TextComponent separator = new TextComponent(", ");
        separator.setColor(ChatColor.YELLOW);
        users.forEach(user -> {
            components.add(user.getMention());
            components.add(separator);
        });
        components.remove(components.size() - 1);
        issuer.getIssuer().spigot().sendMessage(components.toArray(new BaseComponent[0]));
    }
}
Also used : UserRank(com.easterlyn.user.UserRank) TextComponent(net.md_5.bungee.api.chat.TextComponent) Player(org.bukkit.entity.Player) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) AutoUser(com.easterlyn.user.AutoUser) User(com.easterlyn.user.User) HashMap(java.util.HashMap) AutoUser(com.easterlyn.user.AutoUser) ArrayList(java.util.ArrayList) 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 2 with AutoUser

use of com.easterlyn.user.AutoUser 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 AutoUser

use of com.easterlyn.user.AutoUser in project Easterlyn by Easterlyn.

the class ChannelManagementListener method onUserCreate.

@EventHandler
public void onUserCreate(UserCreationEvent event) {
    RegisteredServiceProvider<EasterlynCore> easterlynProvider = chat.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynProvider != null) {
        ConfigurationSection userSection = chat.getConfig().getConfigurationSection("auto_user");
        Map<String, String> userData = new HashMap<>();
        if (userSection != null) {
            userSection.getKeys(false).forEach(key -> userData.put(key, userSection.getString(key)));
        }
        Player player = event.getUser().getPlayer();
        if (player != null && !player.hasPlayedBefore()) {
            // TODO lang?
            new UserChatEvent(new AutoUser(easterlynProvider.getProvider(), userData), EasterlynChat.DEFAULT, event.getUser().getDisplayName() + " is new! Please welcome them.");
        }
    }
    addMainChannel(event.getUser(), null);
}
Also used : Player(org.bukkit.entity.Player) EasterlynCore(com.easterlyn.EasterlynCore) HashMap(java.util.HashMap) UserChatEvent(com.easterlyn.chat.event.UserChatEvent) AutoUser(com.easterlyn.user.AutoUser) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) EventHandler(org.bukkit.event.EventHandler)

Example 4 with AutoUser

use of com.easterlyn.user.AutoUser in project Easterlyn by Easterlyn.

the class UserChatEvent method send.

public void send(Collection<UUID> additionalRecipients) {
    Bukkit.getPluginManager().callEvent(this);
    if (this.isCancelled()) {
        return;
    }
    TextComponent channelElement = new TextComponent();
    TextComponent[] channelHover = new TextComponent[] { new TextComponent("/join "), new TextComponent(channel.getDisplayName()) };
    channelHover[0].setColor(Colors.COMMAND);
    channelHover[1].setColor(Colors.CHANNEL);
    channelElement.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(channelHover)));
    channelElement.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + channel.getDisplayName()));
    TextComponent channelName = new TextComponent(channel.getDisplayName());
    channelName.setColor((channel.isOwner(getUser()) ? Colors.CHANNEL_OWNER : channel.isModerator(getUser()) ? Colors.CHANNEL_MODERATOR : Colors.CHANNEL_MEMBER));
    TextComponent nameElement = new TextComponent(thirdPerson ? "> " : " <");
    TextComponent userElement = getUser().getMention();
    nameElement.setHoverEvent(userElement.getHoverEvent());
    nameElement.setClickEvent(userElement.getClickEvent());
    TextComponent nameText = new TextComponent(userElement.getText().substring(1));
    nameText.setColor(userElement.getColor());
    nameElement.addExtra(nameText);
    nameElement.addExtra(new TextComponent(thirdPerson ? " " : "> "));
    Collection<TextComponent> messageComponents;
    Player player = getUser().getPlayer();
    if (player == null) {
        messageComponents = StringUtil.toJSON(message);
    } else {
        messageComponents = StringUtil.toJSON(message, Collections.singleton(new StaticQuoteConsumer(PLAYER_ITEMS) {

            @Override
            public void addComponents(@NotNull ParsedText components, @NotNull Supplier<Matcher> matcherSupplier) {
                Matcher matcher = matcherSupplier.get();
                int slot;
                try {
                    slot = Integer.parseInt(matcher.group(1));
                } catch (NumberFormatException e) {
                    components.addText(matcher.group());
                    return;
                }
                if (slot < 0 || slot >= player.getInventory().getSize()) {
                    components.addText(matcher.group());
                    return;
                }
                ItemStack item = player.getInventory().getItem(slot);
                if (item == null) {
                    item = ItemUtil.AIR;
                }
                components.addComponent(ItemUtil.getItemComponent(item));
            }
        }));
    }
    Stream.concat(additionalRecipients.stream(), channel.getMembers().stream()).distinct().map(uuid -> getUser().getPlugin().getUserManager().getUser(uuid)).forEach(user -> {
        boolean highlight = false;
        // Copy and convert TextComponents from parsed message
        List<TextComponent> highlightedComponents = new LinkedList<>();
        for (TextComponent textComponent : messageComponents) {
            String text = textComponent.getText();
            Matcher matcher = getHighlightPattern(user).matcher(text);
            int previousMatch = 0;
            while (matcher.find()) {
                highlight = true;
                if (matcher.start() > 0) {
                    TextComponent start = new TextComponent(textComponent);
                    start.setText(text.substring(previousMatch, matcher.start()));
                    highlightedComponents.add(start);
                }
                TextComponent mention = user.getMention();
                mention.setColor(Colors.HIGHLIGHT);
                highlightedComponents.add(mention);
                // Set previous match to end of group 1 so next will pick up group 2 if it exists.
                previousMatch = matcher.end(1);
            }
            if (previousMatch == 0) {
                // No matches, no need to modify component for user.
                highlightedComponents.add(textComponent);
                continue;
            }
            if (previousMatch == text.length()) {
                // Last match coincided with the end of the text.
                continue;
            }
            TextComponent end = new TextComponent(textComponent);
            end.setText(text.substring(previousMatch));
            highlightedComponents.add(end);
        }
        TextComponent finalMessage = new TextComponent();
        // Set text a nice relaxing grey if not focused or explicitly set
        finalMessage.setColor(channel.getName().equals("pm") || channel.getName().equals(user.getStorage().getString(EasterlynChat.USER_CURRENT)) ? ChatColor.WHITE : ChatColor.GRAY);
        TextComponent channelBrace;
        if (highlight) {
            channelBrace = new TextComponent("!!");
            channelBrace.setColor(Colors.HIGHLIGHT);
        } else {
            channelBrace = new TextComponent("[");
        }
        BaseComponent finalChannel = channelElement.duplicate();
        finalChannel.addExtra(channelBrace);
        finalChannel.addExtra(channelName);
        if (!highlight) {
            channelBrace = new TextComponent("]");
        }
        finalChannel.addExtra(channelBrace);
        finalMessage.addExtra(finalChannel);
        finalMessage.addExtra(nameElement);
        for (TextComponent component : highlightedComponents) {
            finalMessage.addExtra(component);
        }
        user.sendMessage(getUser() instanceof AutoUser ? null : getUser().getUniqueId(), finalMessage);
    });
    Bukkit.getConsoleSender().sendMessage(ChatColor.stripColor(String.format("[%1$s]" + (thirdPerson ? "> %2$s " : " <%2$s> ") + "%3$s", channel.getDisplayName(), getUser().getDisplayName(), message)));
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) HandlerList(org.bukkit.event.HandlerList) Cancellable(org.bukkit.event.Cancellable) Text(net.md_5.bungee.api.chat.hover.content.Text) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) Player(org.bukkit.entity.Player) Supplier(java.util.function.Supplier) TextComponent(net.md_5.bungee.api.chat.TextComponent) AutoUser(com.easterlyn.user.AutoUser) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) Matcher(java.util.regex.Matcher) StringUtil(com.easterlyn.util.StringUtil) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer) ParsedText(com.easterlyn.util.text.ParsedText) LinkedList(java.util.LinkedList) ChatColor(net.md_5.bungee.api.ChatColor) Bukkit(org.bukkit.Bukkit) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) User(com.easterlyn.user.User) ItemUtil(com.easterlyn.util.inventory.ItemUtil) Collection(java.util.Collection) Channel(com.easterlyn.chat.channel.Channel) UUID(java.util.UUID) UserEvent(com.easterlyn.event.UserEvent) ItemStack(org.bukkit.inventory.ItemStack) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) EasterlynChat(com.easterlyn.EasterlynChat) Colors(com.easterlyn.util.Colors) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) Player(org.bukkit.entity.Player) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) Matcher(java.util.regex.Matcher) AutoUser(com.easterlyn.user.AutoUser) 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) LinkedList(java.util.LinkedList) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

AutoUser (com.easterlyn.user.AutoUser)4 User (com.easterlyn.user.User)3 HashMap (java.util.HashMap)3 Player (org.bukkit.entity.Player)3 CommandAlias (co.aikar.commands.annotation.CommandAlias)2 CommandCompletion (co.aikar.commands.annotation.CommandCompletion)2 CommandPermission (co.aikar.commands.annotation.CommandPermission)2 Description (co.aikar.commands.annotation.Description)2 Syntax (co.aikar.commands.annotation.Syntax)2 Channel (com.easterlyn.chat.channel.Channel)2 UserChatEvent (com.easterlyn.chat.event.UserChatEvent)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 BaseComponent (net.md_5.bungee.api.chat.BaseComponent)2 TextComponent (net.md_5.bungee.api.chat.TextComponent)2 EasterlynChat (com.easterlyn.EasterlynChat)1 EasterlynCore (com.easterlyn.EasterlynCore)1 UserEvent (com.easterlyn.event.UserEvent)1 UserRank (com.easterlyn.user.UserRank)1 Colors (com.easterlyn.util.Colors)1