Skip to content

Commit e809108

Browse files
committed
Add action bar alert manager
1 parent 9dc29c2 commit e809108

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020, 2021, 2022, 2023, 2024 Clément "carlodrift" Raynaud, Lucas "Lucas_Cdry" Cadiry and contributors
3+
*
4+
* This file is part of Skoice.
5+
*
6+
* Skoice is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Skoice is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with Skoice. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package net.clementraynaud.skoice.system;
21+
22+
import java.util.Comparator;
23+
import java.util.Set;
24+
25+
public enum ActionBarAlert {
26+
27+
CONNECTING(1),
28+
DISCONNECTING(1);
29+
30+
private final int priority;
31+
private final String lowerCaseName;
32+
33+
ActionBarAlert(int priority) {
34+
this.priority = priority;
35+
this.lowerCaseName = this.name().toLowerCase() + "-alert";
36+
}
37+
38+
private int getPriority() {
39+
return this.priority;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return this.lowerCaseName;
45+
}
46+
47+
public static ActionBarAlert getPriorityAlert(Set<ActionBarAlert> alerts) {
48+
return alerts.stream()
49+
.max(Comparator.comparingInt(ActionBarAlert::getPriority))
50+
.orElse(null);
51+
}
52+
}

src/main/java/net/clementraynaud/skoice/system/LinkedPlayer.java

+19-12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.bukkit.scoreboard.Scoreboard;
3232
import org.bukkit.scoreboard.Team;
3333

34+
import java.util.EnumSet;
3435
import java.util.Set;
3536
import java.util.concurrent.ConcurrentHashMap;
3637
import java.util.stream.Collectors;
@@ -44,6 +45,7 @@ public class LinkedPlayer {
4445
private final Skoice plugin;
4546
private final Player player;
4647
private final String discordId;
48+
private final EnumSet<ActionBarAlert> alerts = EnumSet.noneOf(ActionBarAlert.class);
4749

4850
public LinkedPlayer(Skoice plugin, Player player, String discordId) {
4951
this.plugin = plugin;
@@ -57,6 +59,10 @@ public static Set<LinkedPlayer> getOnlineLinkedPlayers() {
5759
return LinkedPlayer.onlineLinkedPlayers;
5860
}
5961

62+
public static void sendActionBarAlerts() {
63+
LinkedPlayer.onlineLinkedPlayers.forEach(LinkedPlayer::sendActionBarAlert);
64+
}
65+
6066
public static LinkedPlayer fromMemberId(String memberId) {
6167
ThreadUtil.ensureNotMainThread();
6268
return LinkedPlayer.onlineLinkedPlayers.stream()
@@ -70,20 +76,21 @@ public boolean isStateEligible() {
7076
&& !this.plugin.getConfigYamlFile().getStringList(ConfigField.DISABLED_WORLDS.toString()).contains(this.player.getWorld().getName());
7177
}
7278

73-
public void sendConnectingAlert() {
74-
this.plugin.adventure().player(this.player).sendActionBar(
75-
Component.text(ChatColor.translateAlternateColorCodes('&',
76-
this.plugin.getLang().getMessage("action-bar.connecting-alert")
77-
))
78-
);
79+
public void addActionBarAlert(ActionBarAlert alert) {
80+
this.alerts.add(alert);
7981
}
8082

81-
public void sendDisconnectingAlert() {
82-
this.plugin.adventure().player(this.player).sendActionBar(
83-
Component.text(ChatColor.translateAlternateColorCodes('&',
84-
this.plugin.getLang().getMessage("action-bar.disconnecting-alert")
85-
))
86-
);
83+
public void sendActionBarAlert() {
84+
ActionBarAlert priorityAlert = ActionBarAlert.getPriorityAlert(this.alerts);
85+
if (priorityAlert != null) {
86+
this.plugin.adventure().player(this.player).sendActionBar(
87+
Component.text(ChatColor.translateAlternateColorCodes('&',
88+
this.plugin.getLang().getMessage("action-bar." + priorityAlert)
89+
))
90+
);
91+
}
92+
93+
this.alerts.clear();
8794
}
8895

8996
public Set<LinkedPlayer> getPlayersWithinRange() {

src/main/java/net/clementraynaud/skoice/tasks/UpdateNetworksTask.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.bugsnag.Severity;
2323
import net.clementraynaud.skoice.Skoice;
2424
import net.clementraynaud.skoice.storage.config.ConfigField;
25+
import net.clementraynaud.skoice.system.ActionBarAlert;
2526
import net.clementraynaud.skoice.system.LinkedPlayer;
2627
import net.clementraynaud.skoice.system.Network;
2728
import net.clementraynaud.skoice.system.Networks;
@@ -89,6 +90,8 @@ private void run() {
8990
this.mergeNetworks();
9091
this.manageMoves();
9192

93+
LinkedPlayer.sendActionBarAlerts();
94+
9295
Set<Member> connectedMembers = new HashSet<>(mainVoiceChannel.getMembers());
9396
connectedMembers.addAll(ProximityChannels.getInitialized().stream()
9497
.map(ProximityChannel::getChannel)
@@ -155,7 +158,7 @@ private void manageConnectedPlayers() {
155158

156159
} else if (this.plugin.getConfigYamlFile().getBoolean(ConfigField.DISCONNECTING_ALERT.toString())
157160
&& !network.canPlayerConnect(p)) {
158-
p.sendDisconnectingAlert();
161+
p.addActionBarAlert(ActionBarAlert.DISCONNECTING);
159162
}
160163
});
161164
}
@@ -180,7 +183,7 @@ private void manageIsolatedPlayers() {
180183
playerInNearNetwork.getNetwork().add(p);
181184

182185
if (this.plugin.getConfigYamlFile().getBoolean(ConfigField.CONNECTING_ALERT.toString())) {
183-
p.sendConnectingAlert();
186+
p.addActionBarAlert(ActionBarAlert.CONNECTING);
184187
}
185188
});
186189

@@ -190,7 +193,7 @@ private void manageIsolatedPlayers() {
190193
new Network(this.plugin, playersWithinRange).build();
191194

192195
if (this.plugin.getConfigYamlFile().getBoolean(ConfigField.CONNECTING_ALERT.toString())) {
193-
playersWithinRange.forEach(LinkedPlayer::sendConnectingAlert);
196+
playersWithinRange.forEach(playerWithinRange -> playerWithinRange.addActionBarAlert(ActionBarAlert.CONNECTING));
194197
}
195198
}
196199
}

0 commit comments

Comments
 (0)