Skip to content

Commit a7f6a2b

Browse files
committed
Add ActionBar functionality for sending messages to players
This commit introduces the ActionBar class along with supporting classes (ActionBarEntry, ActionBarManager, ActionBarMessage, ActionBarPriority) to manage and send action bar messages to players in the game. The ActionBar allows for customizable message duration, repeat intervals, and priority levels, enhancing the user experience with dynamic messaging.
1 parent b124a7f commit a7f6a2b

5 files changed

Lines changed: 692 additions & 0 deletions

File tree

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package com.bencodez.simpleapi.messages.actionbar;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.entity.Player;
5+
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
/**
10+
* Compatibility wrapper for sending action bars.
11+
*/
12+
@Getter
13+
@Setter
14+
public class ActionBar {
15+
16+
/**
17+
* Action bar manager.
18+
*/
19+
private static ActionBarManager manager;
20+
21+
/**
22+
* Message.
23+
*/
24+
private String msg;
25+
26+
/**
27+
* Duration in ticks.
28+
*/
29+
private long duration;
30+
31+
/**
32+
* Repeat interval in ticks.
33+
*/
34+
private long repeatInterval;
35+
36+
/**
37+
* Priority.
38+
*/
39+
private ActionBarPriority priority;
40+
41+
/**
42+
* Creates a new action bar.
43+
*
44+
* @param msg the message
45+
*/
46+
public ActionBar(String msg) {
47+
this(msg, -1L, -1L, ActionBarPriority.NORMAL);
48+
}
49+
50+
/**
51+
* Creates a new action bar.
52+
*
53+
* @param msg the message
54+
* @param duration the duration in ticks
55+
*/
56+
public ActionBar(String msg, long duration) {
57+
this(msg, duration, 20L, ActionBarPriority.NORMAL);
58+
}
59+
60+
/**
61+
* Creates a new action bar.
62+
*
63+
* @param msg the message
64+
* @param duration the duration in ticks
65+
* @param repeatInterval the repeat interval in ticks
66+
*/
67+
public ActionBar(String msg, long duration, long repeatInterval) {
68+
this(msg, duration, repeatInterval, ActionBarPriority.NORMAL);
69+
}
70+
71+
/**
72+
* Creates a new action bar.
73+
*
74+
* @param msg the message
75+
* @param duration the duration in ticks
76+
* @param repeatInterval the repeat interval in ticks
77+
* @param priority the priority
78+
*/
79+
public ActionBar(String msg, long duration, long repeatInterval, ActionBarPriority priority) {
80+
this.msg = msg;
81+
this.duration = duration;
82+
this.repeatInterval = repeatInterval;
83+
this.priority = priority;
84+
}
85+
86+
/**
87+
* Sets the manager used by this API.
88+
*
89+
* @param manager the manager
90+
*/
91+
public static void setManager(ActionBarManager manager) {
92+
ActionBar.manager = manager;
93+
}
94+
95+
/**
96+
* Gets the current manager.
97+
*
98+
* @return the manager
99+
*/
100+
public static ActionBarManager getManager() {
101+
return manager;
102+
}
103+
104+
/**
105+
* Sends this action bar to players.
106+
*
107+
* @param players the players
108+
*/
109+
public void send(Player... players) {
110+
if (manager == null || players == null) {
111+
return;
112+
}
113+
114+
ActionBarMessage message = new ActionBarMessage(msg, duration, repeatInterval, true, priority);
115+
for (Player player : players) {
116+
manager.send(player, message);
117+
}
118+
}
119+
120+
/**
121+
* Sends an action bar immediately.
122+
*
123+
* @param player the player
124+
* @param message the message
125+
*/
126+
public void sendActionBar(Player player, String message) {
127+
if (manager == null) {
128+
return;
129+
}
130+
131+
manager.send(player, new ActionBarMessage(message));
132+
}
133+
134+
/**
135+
* Sends an action bar with duration.
136+
*
137+
* @param player the player
138+
* @param message the message
139+
* @param duration the duration in ticks
140+
*/
141+
public void sendActionBar(Player player, String message, int duration) {
142+
if (manager == null) {
143+
return;
144+
}
145+
146+
manager.send(player, new ActionBarMessage(message, duration, 20L, true, ActionBarPriority.NORMAL));
147+
}
148+
149+
/**
150+
* Sends an action bar to all online players.
151+
*
152+
* @param message the message
153+
*/
154+
public void sendActionBarToAllPlayers(String message) {
155+
sendActionBarToAllPlayers(message, -1);
156+
}
157+
158+
/**
159+
* Sends an action bar to all online players.
160+
*
161+
* @param message the message
162+
* @param duration the duration in ticks
163+
*/
164+
public void sendActionBarToAllPlayers(String message, int duration) {
165+
if (manager == null) {
166+
return;
167+
}
168+
169+
ActionBarMessage actionBarMessage = new ActionBarMessage(message, duration, 20L, true,
170+
ActionBarPriority.NORMAL);
171+
for (Player player : Bukkit.getOnlinePlayers()) {
172+
manager.send(player, actionBarMessage);
173+
}
174+
}
175+
176+
/**
177+
* Clears an action bar from a player.
178+
*
179+
* @param player the player
180+
*/
181+
public static void clear(Player player) {
182+
if (manager == null) {
183+
return;
184+
}
185+
186+
manager.clear(player);
187+
}
188+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.bencodez.simpleapi.messages.actionbar;
2+
3+
import java.util.UUID;
4+
5+
import org.bukkit.scheduler.BukkitTask;
6+
7+
import lombok.Getter;
8+
import lombok.Setter;
9+
10+
/**
11+
* Stores the active action bar state for a player.
12+
*/
13+
@Getter
14+
@Setter
15+
public class ActionBarEntry {
16+
17+
/**
18+
* Player unique id.
19+
*/
20+
private UUID playerUuid;
21+
22+
/**
23+
* Sequence used to prevent older messages from overriding newer ones.
24+
*/
25+
private long sequence;
26+
27+
/**
28+
* Active message.
29+
*/
30+
private ActionBarMessage message;
31+
32+
/**
33+
* Repeating resend task.
34+
*/
35+
private BukkitTask repeatingTask;
36+
37+
/**
38+
* Expiration task.
39+
*/
40+
private BukkitTask expireTask;
41+
42+
/**
43+
* Cancels all scheduled tasks.
44+
*/
45+
public void cancel() {
46+
if (repeatingTask != null) {
47+
repeatingTask.cancel();
48+
repeatingTask = null;
49+
}
50+
if (expireTask != null) {
51+
expireTask.cancel();
52+
expireTask = null;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)