-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBroadcastManager.java
More file actions
219 lines (174 loc) · 8.62 KB
/
BroadcastManager.java
File metadata and controls
219 lines (174 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2022-2025 Noah Ross
*
* This file is part of PerPlayerKit.
*
* PerPlayerKit is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* PerPlayerKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PerPlayerKit. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.noah.perplayerkit.util;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.List;
public class BroadcastManager {
private static final int LINE_LENGTH = 60; // Length of the strikethrough line
private static final String FIGURE_SPACE = "\u2007"; // A whitespace character of consistent width
private static BroadcastManager instance;
private final int broadcastDistance = 200;
private final Plugin plugin;
private final CooldownManager repairBroadcastCooldown = new CooldownManager(5);
private final CooldownManager kitroomBroadcastCooldown = new CooldownManager(15);
private final BukkitAudiences audience;
private final Component prefix;
public BroadcastManager(Plugin plugin) {
this.plugin = plugin;
audience = BukkitAudiences.create(plugin);
prefix = MiniMessage.miniMessage().deserialize(plugin.getConfig().getString("prefix", "<gray>[<aqua>Kits</aqua>]</gray> "));
instance = this;
}
public static BroadcastManager get() {
if (instance == null) {
throw new IllegalStateException("BroadcastManager has not been initialized yet!");
}
return instance;
}
public static Component generateBroadcastComponent(String message) {
String strikeThroughLine = "<gray>" + " ".repeat(3) + "<st>" + FIGURE_SPACE.repeat(LINE_LENGTH) + "</st>";
int messageLength = MiniMessage.miniMessage().stripTags(message).length();
int padding = (LINE_LENGTH - messageLength) / 2;
String formattedMessage = strikeThroughLine + "\n\n" + " ".repeat(3) + FIGURE_SPACE.repeat(Math.max(padding, 0)) + message + "\n\n" + strikeThroughLine;
return MiniMessage.miniMessage().deserialize(formattedMessage);
}
private boolean isKitLoadingMessage(MessageKey key) {
return key == MessageKey.PLAYER_LOADED_PRIVATE_KIT ||
key == MessageKey.PLAYER_LOADED_PUBLIC_KIT ||
key == MessageKey.PLAYER_LOADED_ENDER_CHEST;
}
private void broadcastMessage(Player player, String message, String permission) {
World world = player.getWorld();
if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI")!=null) {
message = PlaceholderAPI.setPlaceholders(player, message);
}
for (Player broadcastPlayer : world.getPlayers()) {
if (broadcastPlayer.getLocation().distance(player.getLocation()) < broadcastDistance) {
// Check if the broadcast player has permission to see this message
if (broadcastPlayer.hasPermission(permission)) {
audience.player(broadcastPlayer).sendMessage(prefix.append(MiniMessage.miniMessage().deserialize(message)));
}
}
}
}
private void broadcastMessage(Player player, BroadcastManager.MessageKey key, CooldownManager cooldownManager) {
broadcastMessage(player, key, cooldownManager, null);
}
private void broadcastMessage(Player player, BroadcastManager.MessageKey key, CooldownManager cooldownManager, String kitName) {
if(!plugin.getConfig().getBoolean("feature.broadcast-on-player-action",true)){
return;
}
if(plugin.getConfig().getBoolean("messages.disable-kit-messages", false)){
return;
}
// Check if this is a kit loading message and if kit message broadcasts are disabled
if (isKitLoadingMessage(key) && !plugin.getConfig().getBoolean("feature.broadcast-kit-messages", true)) {
return;
}
if (cooldownManager != null && cooldownManager.isOnCooldown(player)) {
return;
}
String messageConfigPath = key.getKey();
String enabledPath = messageConfigPath + ".enabled";
String messagePath = messageConfigPath + ".message";
String permissionPath = messageConfigPath + ".permission";
// Check if message is enabled
if (!plugin.getConfig().getBoolean(enabledPath, true)) {
return;
}
String message = plugin.getConfig().getString(messagePath, "<gray><aqua>%player%</aqua> " +
"performed an action.</gray>");
String permission = plugin.getConfig().getString(permissionPath, "perplayerkit.kitnotify");
String playerName;
if (plugin.getConfig().getBoolean("use-display-name", false)) {
playerName = player.getDisplayName();
} else {
playerName = player.getName();
}
message = message.replace("%player%", playerName);
if (kitName != null) {
message = message.replace("%kitname%", kitName);
}
broadcastMessage(player, message, permission);
if (cooldownManager != null) {
cooldownManager.setCooldown(player);
}
}
public void broadcastPlayerRepaired(Player player) {
broadcastMessage(player, MessageKey.PLAYER_REPAIRED, repairBroadcastCooldown);
}
public void broadcastPlayerHealed(Player player) {
broadcastMessage(player, MessageKey.PLAYER_HEALED, null);
}
public void broadcastPlayerOpenedKitRoom(Player player) {
broadcastMessage(player, MessageKey.PLAYER_OPENED_KIT_ROOM, kitroomBroadcastCooldown);
}
public void broadcastPlayerLoadedPrivateKit(Player player, String kitName) {
broadcastMessage(player, MessageKey.PLAYER_LOADED_PRIVATE_KIT, null, kitName);
}
public void broadcastPlayerLoadedPublicKit(Player player, String kitName) {
broadcastMessage(player, MessageKey.PLAYER_LOADED_PUBLIC_KIT, null, kitName);
}
public void broadcastPlayerLoadedEnderChest(Player player) {
broadcastMessage(player, MessageKey.PLAYER_LOADED_ENDER_CHEST, null);
}
public void broadcastPlayerCopiedKit(Player player) {
broadcastMessage(player, MessageKey.PLAYER_COPIED_KIT, null);
}
public void broadcastPlayerCopiedEC(Player player) {
broadcastMessage(player, MessageKey.PLAYER_COPIED_EC, null);
}
public void broadcastPlayerRegeared(Player player) {
broadcastMessage(player, MessageKey.PLAYER_REGEARED, null);
}
public void startScheduledBroadcast() {
List<Component> messages = new ArrayList<>();
plugin.getConfig().getStringList("scheduled-broadcast.messages").forEach(message -> messages.add(generateBroadcastComponent(message)));
int[] index = {0};
if (plugin.getConfig().getBoolean("scheduled-broadcast.enabled")) {
Bukkit.getScheduler().runTaskTimer(plugin, () -> {
for (Player player : Bukkit.getOnlinePlayers()) {
audience.player(player).sendMessage(messages.get(index[0]));
}
index[0] = (index[0] + 1) % messages.size();
}, 0, plugin.getConfig().getInt("scheduled-broadcast.period") * 20L);
}
}
public void sendComponentMessage(Player player, Component message) {
audience.player(player).sendMessage(message);
}
public enum MessageKey {
PLAYER_REPAIRED("messages.player-repaired"), PLAYER_HEALED("messages.player-healed"), PLAYER_OPENED_KIT_ROOM("messages.player-opened-kit-room"), PLAYER_LOADED_PRIVATE_KIT("messages.player-loaded-private-kit"), PLAYER_LOADED_PUBLIC_KIT("messages.player-loaded-public-kit"), PLAYER_LOADED_ENDER_CHEST("messages.player-loaded-enderchest"), PLAYER_COPIED_KIT("messages.player-copied-kit"),PLAYER_COPIED_EC("messages.player-copied-ec"), PLAYER_REGEARED("messages.player-regeared");
private final String key;
MessageKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
}