Skip to content

Commit 376bc74

Browse files
committed
lots of changes
- make listener priority configurable - add proxy support - channel permission is now also used for seing ppl writing to it
1 parent 697bf80 commit 376bc74

5 files changed

Lines changed: 281 additions & 117 deletions

File tree

Lines changed: 40 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,83 @@
11
package ru.overwrite.chat;
22

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;
133
import org.bukkit.entity.Player;
14-
import org.bukkit.event.Cancellable;
154
import org.bukkit.event.EventHandler;
165
import org.bukkit.event.EventPriority;
176
import org.bukkit.event.Listener;
187
import org.bukkit.event.player.AsyncPlayerChatEvent;
198
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;
259

2610
public class ChatListener implements Listener {
2711

28-
private final PromisedChat plugin;
12+
private final ChatManager chatManager;
2913
private final Config pluginConfig;
3014

3115
public ChatListener(PromisedChat plugin) {
32-
this.plugin = plugin;
16+
this.chatManager = plugin.getChatManager();
3317
this.pluginConfig = plugin.getPluginConfig();
3418
}
3519

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+
// Не ну а хуле делать
4122

42-
if (checkNewbie(p, e)) {
23+
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
24+
public void onChatLowest(AsyncPlayerChatEvent e) {
25+
if (pluginConfig.getChatPriority() != EventPriority.LOWEST) {
4326
return;
4427
}
28+
process(e);
29+
}
4530

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) {
5934
return;
6035
}
36+
process(e);
37+
}
6138

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) {
6442
return;
6543
}
44+
process(e);
45+
}
6646

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) {
8750
return;
8851
}
89-
e.getRecipients().addAll(playersInRadius);
90-
e.setFormat(formatWithMessage);
52+
process(e);
9153
}
9254

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;
10659
}
107-
return false;
60+
process(e);
10861
}
10962

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;
12567
}
126-
// Костыли... костыли вечны.
127-
Bukkit.getConsoleSender().sendMessage(formatWithMessage);
68+
process(e);
12869
}
12970

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;
14476
}
145-
return plist;
146-
}
14777

148-
private String getFormatWithMessage(String format, String chatMessage) {
149-
return format
150-
.replace("%message%", chatMessage)
151-
.replace("%", "%%"); // Это надо чтобы PAPI не выёбывался
152-
}
78+
String rawMessage = e.getMessage();
15379

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);
15781
}
15882

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

0 commit comments

Comments
 (0)