Skip to content

Commit 8658670

Browse files
committed
Improve activity rotation
1 parent 0a4e50e commit 8658670

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

src/main/java/xyz/srnyx/lazylibrary/LazyLibrary.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.dv8tion.jda.api.entities.Activity;
1111

1212
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
1314

1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
@@ -22,6 +23,8 @@
2223

2324
import java.sql.SQLException;
2425
import java.util.Scanner;
26+
import java.util.concurrent.Executors;
27+
import java.util.concurrent.ScheduledExecutorService;
2528
import java.util.concurrent.TimeUnit;
2629
import java.util.function.Supplier;
2730

@@ -44,6 +47,10 @@ public class LazyLibrary extends Stringable {
4447
* The {@link JDA} instance
4548
*/
4649
public JDA jda;
50+
/**
51+
* @see #updateActivityRotation()
52+
*/
53+
@Nullable private ScheduledExecutorService activityScheduler;
4754

4855
/**
4956
* Starts the bot
@@ -115,11 +122,8 @@ public LazyLibrary() {
115122
// All necessary tasks are done
116123
onNecessaryTasksDone();
117124

118-
// Rotating status
119-
if (!settings.activities.isEmpty()) {
120-
final Activity[] array = settings.activities.toArray(new Activity[0]);
121-
LazyUtilities.CPU_SCHEDULER.scheduleAtFixedRate(() -> jda.getPresence().setActivity(array[LazyUtilities.RANDOM.nextInt(array.length)]), 0, 1, TimeUnit.MINUTES);
122-
}
125+
// Rotating activity
126+
updateActivityRotation();
123127
}
124128

125129
/**
@@ -167,6 +171,15 @@ public void onStop() {
167171
// Should be overridden
168172
}
169173

174+
/**
175+
* Called when a command is sent in the console
176+
*
177+
* @param command the {@link ConsoleCommand} that was sent
178+
*/
179+
public void onConsoleCommand(@NotNull ConsoleCommand command) {
180+
// Should be overridden
181+
}
182+
170183
/**
171184
* Stops the bot (calls {@link #onStop()} and exits the program)
172185
*/
@@ -176,12 +189,39 @@ public void stopBot() {
176189
}
177190

178191
/**
179-
* Called when a command is sent in the console
192+
* Updates the activity rotation (stops the old scheduler and starts a new one)
180193
*
181-
* @param command the {@link ConsoleCommand} that was sent
194+
* @see LazySettings#activities(Activity...)
195+
* @see LazySettings#activities(java.util.Collection)
182196
*/
183-
public void onConsoleCommand(@NotNull ConsoleCommand command) {
184-
// Should be overridden
197+
public void updateActivityRotation() {
198+
// Stop old scheduler
199+
if (activityScheduler != null) activityScheduler.shutdown();
200+
201+
// Stop activity rotation
202+
if (settings.activities == null) {
203+
activityScheduler = null;
204+
return;
205+
}
206+
207+
// Check if JDA is ready
208+
if (jda == null || jda.getStatus() != JDA.Status.CONNECTED) return;
209+
210+
// Start new scheduler
211+
activityScheduler = Executors.newSingleThreadScheduledExecutor();
212+
activityScheduler.scheduleAtFixedRate(() -> {
213+
// Stop if activities is null
214+
if (settings.activities == null) {
215+
if (activityScheduler != null) {
216+
activityScheduler.shutdown();
217+
activityScheduler = null;
218+
}
219+
return;
220+
}
221+
222+
// Set random activity
223+
if (!settings.activities.isEmpty()) jda.getPresence().setActivity(settings.activities.get(LazyUtilities.RANDOM.nextInt(settings.activities.size())));
224+
}, 0, 1, TimeUnit.MINUTES);
185225
}
186226

187227
/**

src/main/java/xyz/srnyx/lazylibrary/settings/LazySettings.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
*/
2525
@SuppressWarnings("EmptyMethod")
2626
public class LazySettings extends Stringable {
27+
/**
28+
* The {@link LazyLibrary} this {@link LazySettings} belongs to
29+
*/
30+
@NotNull private final LazyLibrary library;
2731
/**
2832
* The {@link FileSettings file settings} for the bot
2933
*/
@@ -63,16 +67,17 @@ public class LazySettings extends Stringable {
6367
@NotNull public Map<LazyEmbed.Key, Object> embedDefaults = new EnumMap<>(LazyEmbed.Key.class);
6468
/**
6569
* A list of {@link Activity activities} to rotate between every minute
66-
* <br><i>Leave empty to disable</i>
70+
* <br><i>Set to null to disable</i>
6771
*/
68-
@NotNull public Set<Activity> activities = new HashSet<>();
72+
@Nullable public List<Activity> activities = null;
6973

7074
/**
7175
* Creates a new {@link LazySettings} for the given {@link LazyLibrary}
7276
*
73-
* @param library the {@link LazyLibrary} to create the {@link LazySettings} for
77+
* @param library {@link #library}
7478
*/
7579
public LazySettings(@NotNull LazyLibrary library) {
80+
this.library = library;
7681
fileSettings = new FileSettings(library);
7782
loggerName = library.getClass().getSimpleName();
7883
}
@@ -204,7 +209,9 @@ public LazySettings embedDefault(@NotNull LazyEmbed.Key key, @NotNull Object val
204209
*/
205210
@NotNull
206211
public LazySettings activities(@NotNull Collection<Activity> activities) {
207-
this.activities.addAll(activities);
212+
if (this.activities == null) this.activities = new ArrayList<>();
213+
Objects.requireNonNull(this.activities).addAll(activities);
214+
library.updateActivityRotation();
208215
return this;
209216
}
210217

@@ -217,7 +224,9 @@ public LazySettings activities(@NotNull Collection<Activity> activities) {
217224
*/
218225
@NotNull
219226
public LazySettings activities(@NotNull Activity... activities) {
220-
Collections.addAll(this.activities, activities);
227+
if (this.activities == null) this.activities = new ArrayList<>();
228+
Collections.addAll(Objects.requireNonNull(this.activities), activities);
229+
library.updateActivityRotation();
221230
return this;
222231
}
223232
}

0 commit comments

Comments
 (0)