-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathNotificationCentre.java
More file actions
125 lines (114 loc) · 4.39 KB
/
NotificationCentre.java
File metadata and controls
125 lines (114 loc) · 4.39 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
package com.froobworld.farmcontrol.controller.tracker;
import com.froobworld.farmcontrol.FarmControl;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class NotificationCentre {
private static final UUID CONSOLE_UUID = new UUID(0, 0);
private final FarmControl farmControl;
private final ExecutorService executorService;
private final Set<UUID> notifiableUsers = new HashSet<>();
public NotificationCentre(FarmControl farmControl) {
this.farmControl = farmControl;
this.executorService = Executors.newSingleThreadExecutor();
load();
}
public void notify(CycleStats cycleStats) {
ComponentBuilder notificationBuilder = new ComponentBuilder()
.append("FarmControl - ").color(ChatColor.GOLD)
.append("" + cycleStats.getAffectedEntityCount()).color(ChatColor.RED)
.append(" entities affected").color(ChatColor.GRAY);
if (cycleStats.getEntitiesRemoved() > 0) {
notificationBuilder
.append(", including ").color(ChatColor.GRAY)
.append("" + cycleStats.getEntitiesRemoved()).color(ChatColor.RED)
.append(" removed.").color(ChatColor.GRAY);
} else {
notificationBuilder.append(".").color(ChatColor.GRAY);
}
BaseComponent[] breakdown = cycleStats.getBreakdown();
notificationBuilder.getParts().forEach(part -> part.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, breakdown)));
BaseComponent[] message = notificationBuilder.create();
for (Player player : Bukkit.getOnlinePlayers()) {
if (!player.hasPermission("farmcontrol.command.notify")) {
continue;
}
if (notifiableUsers.contains(player.getUniqueId())) {
player.spigot().sendMessage(message);
}
}
if (notifiableUsers.contains(CONSOLE_UUID)) {
Bukkit.getConsoleSender().spigot().sendMessage(message);
}
}
public void setNotifiable(CommandSender sender, boolean notifiable) {
UUID uuid;
if (sender instanceof Player) {
uuid = ((Player) sender).getUniqueId();
} else {
uuid = CONSOLE_UUID;
}
if (notifiable) {
notifiableUsers.add(uuid);
} else {
notifiableUsers.remove(uuid);
}
save();
}
public boolean isNotifiable(CommandSender sender) {
UUID uuid;
if (sender instanceof Player) {
uuid = ((Player) sender).getUniqueId();
} else {
uuid = CONSOLE_UUID;
}
return notifiableUsers.contains(uuid);
}
public void load() {
notifiableUsers.clear();
File userFile = new File(farmControl.getDataFolder(), "notifiable-users.txt");
if (!userFile.exists()) {
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(userFile))) {
for (String line; (line = reader.readLine()) != null; ) {
if (!line.isEmpty()) {
notifiableUsers.add(UUID.fromString(line));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void save() {
final Set<UUID> users = new HashSet<>(notifiableUsers);
final File userFile = new File(farmControl.getDataFolder(), "notifiable-users.txt");
executorService.submit(() -> {
if (!userFile.exists()) {
try {
userFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
try (PrintWriter writer = new PrintWriter(new FileWriter(userFile, false))) {
for (UUID uuid : users) {
writer.println(uuid.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
}