Update bus API to fix an error

This commit is contained in:
zontreck 2023-11-19 03:14:38 -07:00
parent 803370c35c
commit 1d5b113308
2 changed files with 38 additions and 45 deletions

View file

@ -19,6 +19,8 @@ archivesBaseName = "EventsBus"
java {
toolchain.languageVersion = JavaLanguageVersion.of(17)
withSourcesJar()
withJavadocJar()
}
def determinePatchVersion = {
@ -52,25 +54,13 @@ dependencies {
def MAVEN_PASSWORD_PROPERTY = "AriasCreationsMavenPassword"
task sourcesJar (type: Jar) {
archiveClassifier = "sources"
from sourceSets.main.allJava
}
jar {
archiveClassifier = "final"
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact (sourcesJar) {
classifier = "sources"
}
artifact (jar) {
classifier = "final"
}
artifact jar
//artifact sourcesJar
//artifact javadocJar
}
}
repositories {
@ -89,7 +79,9 @@ publishing {
}
compileJava.finalizedBy("sourcesJar")
compileJava.finalizedBy("javadocJar")
artifacts {
archives jar
archives sourcesJar
archives javadocJar
}

View file

@ -13,7 +13,14 @@ public class Bus {
/**
* The main event bus!
*/
private static Bus Main = new Bus("Main Event Bus", false);
private static Bus Main;
static {
if(Main == null)
{
Main = new Bus("Main Event Bus", false);
}
}
public static boolean debug = false;
public final String BusName;
@ -23,13 +30,7 @@ public class Bus {
BusName = name;
UsesInstances = useInstances;
try {
Post(new EventBusReadyEvent());
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
Post(new EventBusReadyEvent());
}
public Map<Class<?>, List<EventContainer>> static_events = new HashMap<Class<?>, List<EventContainer>>();
@ -95,31 +96,35 @@ public class Bus {
* @param event The event you wish to post
* @return True if the event was cancelled.
*/
public static boolean Post(Event event) throws InvocationTargetException, IllegalAccessException {
public static boolean Post(Event event) {
for (PriorityLevel level :
PriorityLevel.values()) {
// Call each priority level in order of declaration
// Static first, then instanced
// At the end, this method will return the cancellation result.
try {
if (Main.static_events.containsKey(event.getClass())) {
EventContainer[] tempArray = (EventContainer[]) Main.static_events.get(event.getClass()).toArray();
if (Main.static_events.containsKey(event.getClass())) {
EventContainer[] tempArray = (EventContainer[]) Main.static_events.get(event.getClass()).toArray();
for (EventContainer container : tempArray) {
if (container.invoke(event, level)) {
Main.static_events.get(event.getClass()).remove(container);
for (EventContainer container : tempArray) {
if (container.invoke(event, level)) {
Main.static_events.get(event.getClass()).remove(container);
}
}
}
}
if (Main.instanced_events.containsKey(event.getClass())) {
EventContainer[] tempArray = (EventContainer[]) Main.instanced_events.get(event.getClass()).toArray();
if (Main.instanced_events.containsKey(event.getClass())) {
EventContainer[] tempArray = (EventContainer[]) Main.instanced_events.get(event.getClass()).toArray();
for (EventContainer container : tempArray) {
if (container.invoke(event, level)) {
Main.instanced_events.get(event.getClass()).remove(container);
for (EventContainer container : tempArray) {
if (container.invoke(event, level)) {
Main.instanced_events.get(event.getClass()).remove(container);
}
}
}
} catch (Exception e) {
}
}
@ -133,18 +138,14 @@ public class Bus {
* @see dev.zontreck.eventsbus.events.ResetEventBusEvent
*/
public static boolean Reset() {
try {
if (!Post(new ResetEventBusEvent())) {
if (!Post(new ResetEventBusEvent())) {
Main.static_events = new HashMap<>();
Main.instanced_events = new HashMap<>();
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);
Post(new EventBusReadyEvent());
return true;
}
return false;