From e1188e7d336694fd35fbab8bb14563809ebbc799 Mon Sep 17 00:00:00 2001 From: raul1ro Date: Sat, 5 Oct 2024 12:20:57 +0300 Subject: [PATCH] Fix ScheduledEventManagerImpl::setStatus (#2743) --- .../managers/ScheduledEventManagerImpl.java | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/internal/managers/ScheduledEventManagerImpl.java b/src/main/java/net/dv8tion/jda/internal/managers/ScheduledEventManagerImpl.java index e66356d9fe..26826f035d 100644 --- a/src/main/java/net/dv8tion/jda/internal/managers/ScheduledEventManagerImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/managers/ScheduledEventManagerImpl.java @@ -162,12 +162,44 @@ public ScheduledEventManager setEndTime(@Nonnull TemporalAccessor endTime) @Nonnull @Override - public ScheduledEventManager setStatus(@Nonnull ScheduledEvent.Status status) + public ScheduledEventManager setStatus(@Nonnull ScheduledEvent.Status newStatus) { - Checks.notNull(status, "Status"); - Checks.check(status != ScheduledEvent.Status.UNKNOWN, "Cannot set the event status to an unknown status!"); - Checks.check(status != ScheduledEvent.Status.SCHEDULED && getScheduledEvent().getStatus() != ScheduledEvent.Status.ACTIVE, "Cannot perform status update!"); - this.status = status; + Checks.notNull(newStatus, "Status"); + switch (newStatus) + { + case SCHEDULED: + case UNKNOWN: + throw new IllegalArgumentException("Cannot change scheduled event status to " + newStatus); + } + + //get the current status of the event. multiple-usage + ScheduledEvent.Status currentStatus = getScheduledEvent().getStatus(); + + switch (currentStatus) + { + case SCHEDULED: + //event is scheduled -> new status can be only active or cancel + Checks.check( + newStatus == ScheduledEvent.Status.ACTIVE || newStatus == ScheduledEvent.Status.CANCELED, + "Cannot perform status update! A scheduled event with status SCHEDULED can only be set to ACTIVE or CANCELED status." + ); + break; + + case ACTIVE: + //event is active -> new status can be only completed + Checks.check( + newStatus == ScheduledEvent.Status.COMPLETED, + "Cannot perform status updated! A scheduled event with status ACTIVE can only be set to COMPLETED status." + ); + break; + + case COMPLETED: + case CANCELED: + //event is completed or canceled -> can't update status + throw new IllegalArgumentException("Cannot perform status update! Event is " + currentStatus.name().toLowerCase() + "."); + } + + this.status = newStatus; set |= STATUS; return this; }