|
1 | 1 | package ru.overwrite.chat; |
2 | 2 |
|
3 | | -import it.unimi.dsi.fastutil.objects.ObjectArrayList; |
4 | | -import it.unimi.dsi.fastutil.objects.ObjectList; |
5 | | -import net.md_5.bungee.api.chat.BaseComponent; |
6 | | -import net.md_5.bungee.api.chat.ClickEvent; |
7 | | -import net.md_5.bungee.api.chat.HoverEvent; |
8 | | -import net.md_5.bungee.api.chat.HoverEvent.Action; |
9 | | -import net.md_5.bungee.api.chat.TextComponent; |
10 | | -import net.md_5.bungee.api.chat.hover.content.Text; |
11 | | -import org.bukkit.Bukkit; |
12 | | -import org.bukkit.Location; |
13 | 3 | import org.bukkit.entity.Player; |
14 | | -import org.bukkit.event.Cancellable; |
15 | 4 | import org.bukkit.event.EventHandler; |
16 | 5 | import org.bukkit.event.EventPriority; |
17 | 6 | import org.bukkit.event.Listener; |
18 | 7 | import org.bukkit.event.player.AsyncPlayerChatEvent; |
19 | 8 | import ru.overwrite.chat.configuration.Config; |
20 | | -import ru.overwrite.chat.configuration.data.ChatChannel; |
21 | | -import ru.overwrite.chat.configuration.data.NewbieChatSettings; |
22 | | -import ru.overwrite.chat.utils.Utils; |
23 | | - |
24 | | -import java.util.Collection; |
25 | 9 |
|
26 | 10 | public class ChatListener implements Listener { |
27 | 11 |
|
28 | | - private final PromisedChat plugin; |
| 12 | + private final ChatManager chatManager; |
29 | 13 | private final Config pluginConfig; |
30 | 14 |
|
31 | 15 | public ChatListener(PromisedChat plugin) { |
32 | | - this.plugin = plugin; |
| 16 | + this.chatManager = plugin.getChatManager(); |
33 | 17 | this.pluginConfig = plugin.getPluginConfig(); |
34 | 18 | } |
35 | 19 |
|
36 | | - private final String[] searchList = {"%player%", "%prefix%", "%suffix%", "%dph%"}; |
37 | | - |
38 | | - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) |
39 | | - public void onChat(AsyncPlayerChatEvent e) { |
40 | | - Player p = e.getPlayer(); |
| 20 | + // А в пизду захуячим 6 листенеров нахуй |
| 21 | + // Не ну а хуле делать |
41 | 22 |
|
42 | | - if (checkNewbie(p, e)) { |
| 23 | + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) |
| 24 | + public void onChatLowest(AsyncPlayerChatEvent e) { |
| 25 | + if (pluginConfig.getChatPriority() != EventPriority.LOWEST) { |
43 | 26 | return; |
44 | 27 | } |
| 28 | + process(e); |
| 29 | + } |
45 | 30 |
|
46 | | - String rawMessage = e.getMessage(); |
47 | | - |
48 | | - ChatChannel channel = pluginConfig.findChannel(rawMessage); |
49 | | - |
50 | | - if (!channel.equals(pluginConfig.getDefaultChannel()) && !p.hasPermission(channel.permission())) { |
51 | | - channel = pluginConfig.getDefaultChannel(); |
52 | | - } |
53 | | - |
54 | | - String message = (channel.prefix() != '\0' && rawMessage.charAt(0) == channel.prefix()) |
55 | | - ? rawMessage.substring(1).trim() : rawMessage; |
56 | | - |
57 | | - if (message.isEmpty()) { |
58 | | - e.setCancelled(true); |
| 31 | + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) |
| 32 | + public void onChatLow(AsyncPlayerChatEvent e) { |
| 33 | + if (pluginConfig.getChatPriority() != EventPriority.LOW) { |
59 | 34 | return; |
60 | 35 | } |
| 36 | + process(e); |
| 37 | + } |
61 | 38 |
|
62 | | - if (channel.cooldownSettings().process(p)) { |
63 | | - e.setCancelled(true); |
| 39 | + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) |
| 40 | + public void onChatNormal(AsyncPlayerChatEvent e) { |
| 41 | + if (pluginConfig.getChatPriority() != EventPriority.NORMAL) { |
64 | 42 | return; |
65 | 43 | } |
| 44 | + process(e); |
| 45 | + } |
66 | 46 |
|
67 | | - String donatePlaceholder = plugin.getPerms() != null ? getDonatePlaceholder(p, channel) : ""; |
68 | | - String prefix = plugin.getChat() != null ? plugin.getChat().getPlayerPrefix(p) : ""; |
69 | | - String suffix = plugin.getChat() != null ? plugin.getChat().getPlayerSuffix(p) : ""; |
70 | | - |
71 | | - String[] replacementList = {p.getName(), prefix, suffix, donatePlaceholder}; |
72 | | - |
73 | | - String colorizedMessage = Utils.formatByPerm(p, message); |
74 | | - |
75 | | - String chatFormat = Utils.colorize(Utils.replacePlaceholders(p, Utils.replaceEach(channel.format(), searchList, replacementList))); |
76 | | - |
77 | | - e.getRecipients().clear(); |
78 | | - |
79 | | - Collection<? extends Player> playersInRadius = getRadius(p, channel); |
80 | | - |
81 | | - String formatWithMessage = getFormatWithMessage(chatFormat, colorizedMessage); |
82 | | - |
83 | | - ChatChannel.HoverSettings hoverSettings = channel.hover(); |
84 | | - if (hoverSettings.hoverEnabled()) { |
85 | | - e.setCancelled(true); |
86 | | - sendHover(p, replacementList, playersInRadius, formatWithMessage, hoverSettings); |
| 47 | + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) |
| 48 | + public void onChatHigh(AsyncPlayerChatEvent e) { |
| 49 | + if (pluginConfig.getChatPriority() != EventPriority.HIGH) { |
87 | 50 | return; |
88 | 51 | } |
89 | | - e.getRecipients().addAll(playersInRadius); |
90 | | - e.setFormat(formatWithMessage); |
| 52 | + process(e); |
91 | 53 | } |
92 | 54 |
|
93 | | - private boolean checkNewbie(Player p, Cancellable e) { |
94 | | - NewbieChatSettings newbieChatSettings = pluginConfig.getNewbieChatSettings(); |
95 | | - if (newbieChatSettings.enabled()) { |
96 | | - if (p.hasPermission("pchat.bypass.newbie")) { |
97 | | - return false; |
98 | | - } |
99 | | - long time = (System.currentTimeMillis() - p.getFirstPlayed()) / 1000; |
100 | | - if (time <= newbieChatSettings.cooldown()) { |
101 | | - String cooldown = Utils.getTime((int) (newbieChatSettings.cooldown() - time), Config.timeHours, Config.timeMinutes, Config.timeSeconds); |
102 | | - p.sendMessage(newbieChatSettings.message().replace("%time%", cooldown)); |
103 | | - e.setCancelled(true); |
104 | | - return true; |
105 | | - } |
| 55 | + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) |
| 56 | + public void onChatHighest(AsyncPlayerChatEvent e) { |
| 57 | + if (pluginConfig.getChatPriority() != EventPriority.HIGHEST) { |
| 58 | + return; |
106 | 59 | } |
107 | | - return false; |
| 60 | + process(e); |
108 | 61 | } |
109 | 62 |
|
110 | | - private void sendHover(Player p, String[] replacementList, Collection<? extends Player> recipients, String formatWithMessage, ChatChannel.HoverSettings hoverSettings) { |
111 | | - String hoverText = Utils.colorize(Utils.replacePlaceholders(p, Utils.replaceEach(hoverSettings.hoverMessage(), searchList, replacementList))); |
112 | | - HoverEvent hoverEvent = new HoverEvent(Action.SHOW_TEXT, new Text(TextComponent.fromLegacyText(hoverText))); |
113 | | - BaseComponent[] comp = TextComponent.fromLegacyText(formatWithMessage); |
114 | | - for (BaseComponent component : comp) { |
115 | | - component.setHoverEvent(hoverEvent); |
116 | | - } |
117 | | - if (hoverSettings.clickEventEnabled()) { |
118 | | - ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.valueOf(hoverSettings.clickAction()), Utils.replacePlaceholders(p, Utils.replaceEach(hoverSettings.clickActionValue(), searchList, replacementList))); |
119 | | - for (BaseComponent component : comp) { |
120 | | - component.setClickEvent(clickEvent); |
121 | | - } |
122 | | - } |
123 | | - for (Player recipient : recipients) { |
124 | | - recipient.spigot().sendMessage(comp); |
| 63 | + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) |
| 64 | + public void onChatMonitor(AsyncPlayerChatEvent e) { |
| 65 | + if (pluginConfig.getChatPriority() != EventPriority.MONITOR) { |
| 66 | + return; |
125 | 67 | } |
126 | | - // Костыли... костыли вечны. |
127 | | - Bukkit.getConsoleSender().sendMessage(formatWithMessage); |
| 68 | + process(e); |
128 | 69 | } |
129 | 70 |
|
130 | | - private Collection<? extends Player> getRadius(Player p, ChatChannel chatChannel) { |
131 | | - if (chatChannel.radius() < 0) { |
132 | | - return Bukkit.getOnlinePlayers(); |
133 | | - } |
134 | | - ObjectList<Player> plist = new ObjectArrayList<>(); |
135 | | - double maxDist = Math.pow(chatChannel.radius(), 2.0D); |
136 | | - Location loc = p.getLocation(); |
137 | | - for (Player player : Bukkit.getOnlinePlayers()) { |
138 | | - if (player.getWorld() != p.getWorld()) { |
139 | | - continue; |
140 | | - } |
141 | | - if (loc.distanceSquared(player.getLocation()) <= maxDist) { |
142 | | - plist.add(player); |
143 | | - } |
| 71 | + private void process(AsyncPlayerChatEvent e) { |
| 72 | + Player p = e.getPlayer(); |
| 73 | + |
| 74 | + if (chatManager.checkNewbie(p, e)) { |
| 75 | + return; |
144 | 76 | } |
145 | | - return plist; |
146 | | - } |
147 | 77 |
|
148 | | - private String getFormatWithMessage(String format, String chatMessage) { |
149 | | - return format |
150 | | - .replace("%message%", chatMessage) |
151 | | - .replace("%", "%%"); // Это надо чтобы PAPI не выёбывался |
152 | | - } |
| 78 | + String rawMessage = e.getMessage(); |
153 | 79 |
|
154 | | - private String getDonatePlaceholder(Player p, ChatChannel chatChannel) { |
155 | | - String primaryGroup = plugin.getPerms().getPrimaryGroup(p); |
156 | | - return chatChannel.donatePlaceholders().getOrDefault(primaryGroup, ""); |
| 80 | + chatManager.processChat(p, rawMessage, e); |
157 | 81 | } |
158 | 82 |
|
159 | 83 | } |
0 commit comments