Adds a reset event

This commit is contained in:
Zontreck 2023-10-12 18:25:43 -07:00
parent 96b096478c
commit fca7a9ce61
3 changed files with 63 additions and 0 deletions

View file

@ -1,5 +1,7 @@
package dev.zontreck.eventsbus;
import dev.zontreck.eventsbus.events.EventBusReadyEvent;
import dev.zontreck.eventsbus.events.ResetEventBusEvent;
import org.checkerframework.common.reflection.qual.GetClass;
import java.lang.reflect.InvocationTargetException;
@ -20,6 +22,14 @@ public class Bus {
public Bus(String name, boolean useInstances) {
BusName = name;
UsesInstances = useInstances;
try {
Post(new EventBusReadyEvent());
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public Map<Class<?>, List<EventContainer>> static_events = new HashMap<Class<?>, List<EventContainer>>();
@ -115,4 +125,29 @@ public class Bus {
return event.isCancelled();
}
/**
* Attempts to reset the Event Bus
*
* @return True if the bus was successfully reset (If not interrupts!)
* @see dev.zontreck.eventsbus.events.ResetEventBusEvent
*/
public static boolean Reset() {
try {
if (!Post(new ResetEventBusEvent())) {
Main.static_events = new HashMap<>();
Main.instanced_events = new HashMap<>();
Post(new EventBusReadyEvent());
}
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return false;
}
}

View file

@ -0,0 +1,11 @@
package dev.zontreck.eventsbus.events;
import dev.zontreck.eventsbus.Event;
/**
* This event is sent out when the Event Bus is ready.
* <p>
* This is also dispatched when a reset occurs, prior to clearing the lists.
*/
public class EventBusReadyEvent extends Event {
}

View file

@ -0,0 +1,17 @@
package dev.zontreck.eventsbus.events;
import dev.zontreck.eventsbus.Cancellable;
import dev.zontreck.eventsbus.Event;
/**
* Posted when the event bus is about to be reset.
* <p>
* This event can be cancelled to prevent the reset.
* <p>
* The default behavior is: Allow Reset.
* <p>
* Recommended that handlers be implemented to re-register instances, and classes when a reset occurs.
*/
@Cancellable
public class ResetEventBusEvent extends Event {
}