Search in sources :

Example 16 with User

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

Example 17 with User

use of com.easterlyn.user.User 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 18 with User

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

Example 19 with User

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

the class PVPKeepInventory method onEntityDamageByEntity.

@EventHandler(ignoreCancelled = true)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    if (!(event.getEntity() instanceof Player)) {
        return;
    }
    boolean playerDamage = false;
    if (event.getDamager() instanceof Player) {
        playerDamage = true;
    } else if (event.getDamager() instanceof Firework) {
        Firework firework = (Firework) event.getDamager();
        playerDamage = firework.getSpawningEntity() != null || firework.getShooter() instanceof Player;
    } else if (event.getDamager() instanceof Projectile) {
        playerDamage = ((Projectile) event.getDamager()).getShooter() instanceof Player;
    }
    if (!playerDamage) {
        return;
    }
    RegisteredServiceProvider<EasterlynCore> easterlynProvider = Bukkit.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynProvider == null) {
        return;
    }
    User user = easterlynProvider.getProvider().getUserManager().getUser(event.getEntity().getUniqueId());
    user.getTemporaryStorage().put(key, System.currentTimeMillis());
}
Also used : Player(org.bukkit.entity.Player) User(com.easterlyn.user.User) EasterlynCore(com.easterlyn.EasterlynCore) Firework(org.bukkit.entity.Firework) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 20 with User

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

the class EasterlynSpectators method onDisable.

@Override
public void onDisable() {
    RegisteredServiceProvider<EasterlynCore> registration = getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (registration == null) {
        return;
    }
    for (Player player : Bukkit.getOnlinePlayers()) {
        if (player.getGameMode() != GameMode.SPECTATOR) {
            continue;
        }
        User user = registration.getProvider().getUserManager().getUser(player.getUniqueId());
        Location spectateReturn = user.getStorage().getSerializable(USER_SPECTATE_RETURN, Location.class);
        if (spectateReturn == null) {
            continue;
        }
        user.getStorage().set(USER_SPECTATE_RETURN, null);
        player.teleport(spectateReturn);
        player.setGameMode(GameMode.SURVIVAL);
        user.getStorage().set(USER_SPECTATE_COOLDOWN, System.currentTimeMillis() + 480000L);
    }
}
Also used : Player(org.bukkit.entity.Player) User(com.easterlyn.user.User) Location(org.bukkit.Location)

Aggregations

User (com.easterlyn.user.User)22 EasterlynCore (com.easterlyn.EasterlynCore)12 Player (org.bukkit.entity.Player)12 EventHandler (org.bukkit.event.EventHandler)11 Channel (com.easterlyn.chat.channel.Channel)7 AutoUser (com.easterlyn.user.AutoUser)7 TextComponent (net.md_5.bungee.api.chat.TextComponent)6 StringUtil (com.easterlyn.util.StringUtil)5 List (java.util.List)5 BaseComponent (net.md_5.bungee.api.chat.BaseComponent)5 Nullable (org.jetbrains.annotations.Nullable)5 EasterlynChat (com.easterlyn.EasterlynChat)4 UserChatEvent (com.easterlyn.chat.event.UserChatEvent)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Date (java.util.Date)4 UUID (java.util.UUID)4 Collectors (java.util.stream.Collectors)4 NotNull (org.jetbrains.annotations.NotNull)4 CommandAlias (co.aikar.commands.annotation.CommandAlias)3