Fix a bug where events would not post

This commit is contained in:
Aria 2023-03-13 03:33:03 -07:00
parent 5db113cbd3
commit a70425d418
3 changed files with 42 additions and 21 deletions

View file

@ -72,20 +72,17 @@ public class EventBus
} }
List<EventContainer> containers = entry.getValue(); List<EventContainer> containers = entry.getValue();
if(eventClazz.isInstance(Event.class)) if(eventClazz.equals(event.getClass()))
{ {
for (EventContainer eventContainer : containers) { for (EventContainer eventContainer : containers) {
try { try {
eventContainer.function.invoke(event); //Event t = (Event)eventClazz.getConstructor().newInstance();
} catch (IllegalAccessException e) { eventContainer.function.invoke(eventContainer.containingClass, event);
e.printStackTrace(); } catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException | SecurityException e) {
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
if(event.isCancelled()) cancelled=true; cancelled=event.isCancelled();
} }
} }
} }

View file

@ -0,0 +1,17 @@
package dev.zontreck.ariaslib.events;
public class NBTLoadFailedEvent extends Event
{
public String fileName;
@Override
public boolean isCancellable() {
return false;
}
public NBTLoadFailedEvent(String fileName)
{
this.fileName=fileName;
}
}

View file

@ -9,26 +9,33 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import dev.zontreck.ariaslib.events.EventBus; import dev.zontreck.ariaslib.events.EventBus;
import dev.zontreck.ariaslib.events.NBTLoadFailedEvent;
import dev.zontreck.ariaslib.events.NBTLoadedEvent; import dev.zontreck.ariaslib.events.NBTLoadedEvent;
import dev.zontreck.ariaslib.events.NBTSavingEvent; import dev.zontreck.ariaslib.events.NBTSavingEvent;
public class NBTIO { public class NBTIO {
public static CompoundTag read(File file) throws FileNotFoundException, IOException public static CompoundTag read(File file) throws FileNotFoundException, IOException
{ {
FileInputStream fis = new FileInputStream(file); try{
CompoundTag tag;
DataInputStream dis = new DataInputStream(fis); FileInputStream fis = new FileInputStream(file);
// Start loading CompoundTag tag;
// We can only be a compound tag DataInputStream dis = new DataInputStream(fis);
dis.skipBytes(1); // Skip the compound tag type bit // Start loading
tag = (CompoundTag) TagTypes.Compound.type.load(dis);
// We can only be a compound tag
EventBus.BUS.post(new NBTLoadedEvent(tag, file.getName())); dis.skipBytes(1); // Skip the compound tag type bit
tag = (CompoundTag) TagTypes.Compound.type.load(dis);
// Complete
return tag; EventBus.BUS.post(new NBTLoadedEvent(tag, file.getName()));
// Complete
return tag;
}catch(Exception e) {
EventBus.BUS.post(new NBTLoadFailedEvent(file.getName()));
return new CompoundTag();
}
} }