-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
454 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
src/main/java/com/minecolonies/api/colony/buildings/event/BuildingConstructionEvent.java
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
src/main/java/com/minecolonies/api/colony/event/ColonyInformationChangedEvent.java
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/main/java/com/minecolonies/api/eventbus/DefaultEventBus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.minecolonies.api.eventbus; | ||
|
||
import com.minecolonies.api.util.Log; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Default implementation of the mod event bus. | ||
*/ | ||
public class DefaultEventBus implements EventBus | ||
{ | ||
/** | ||
* The map of event handlers. | ||
*/ | ||
private final Map<Class<? extends IModEvent>, List<EventHandler<? extends IModEvent>>> eventHandlersPerType = new HashMap<>(); | ||
|
||
@Override | ||
public <T extends IModEvent> void subscribe(final @NotNull Class<T> eventType, final @NotNull EventHandler<T> handler) | ||
{ | ||
Log.getLogger().debug("Registering event handler for id {}.", eventType.getSimpleName()); | ||
eventHandlersPerType.computeIfAbsent(eventType, (f) -> new ArrayList<>()).add(handler); | ||
} | ||
|
||
@Override | ||
public void post(final @NotNull IModEvent event) | ||
{ | ||
final List<EventHandler<? extends IModEvent>> eventHandlers = eventHandlersPerType.get(event.getClass()); | ||
if (eventHandlers == null) | ||
{ | ||
return; | ||
} | ||
Log.getLogger().debug("Sending event '{}' for type '{}'. Sending to {} handlers.", event.getEventId(), event.getClass().getSimpleName(), eventHandlers.size()); | ||
for (final EventHandler<? extends IModEvent> handler : eventHandlers) | ||
{ | ||
try | ||
{ | ||
((EventHandler<IModEvent>) handler).apply(event); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.getLogger().warn("Sending event '{}' for type '{}'. Error occurred in handler:", event.getEventId(), event.getClass().getSimpleName(), ex); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.minecolonies.api.eventbus; | ||
import org.jetbrains.annotations.NotNull; | ||
/** | ||
* Interface for the mod event bus. | ||
*/ | ||
public interface EventBus | ||
{ | ||
/** | ||
* Subscribe to the given event type, providing a handler function. | ||
* | ||
* @param eventType the event class type. | ||
* @param handler the handler function handling the event logic. | ||
* @param <T> the generic type of the event class. | ||
*/ | ||
<T extends IModEvent> void subscribe(final @NotNull Class<T> eventType, final @NotNull EventHandler<T> handler); | ||
/** | ||
* Posts a new event on the event bus for the given type. | ||
* | ||
* @param event the event to send. | ||
*/ | ||
void post(final @NotNull IModEvent event); | ||
/** | ||
* The event handler lambda definition. | ||
* | ||
* @param <T> the generic type of the event class. | ||
*/ | ||
@FunctionalInterface | ||
interface EventHandler<T extends IModEvent> | ||
{ | ||
void apply(final @NotNull T event); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/minecolonies/api/eventbus/IModEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.minecolonies.api.eventbus; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Default event interface. | ||
*/ | ||
public interface IModEvent | ||
{ | ||
/** | ||
* The unique id for this event. | ||
* | ||
* @return the event id. | ||
*/ | ||
UUID getEventId(); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/minecolonies/api/eventbus/events/AbstractModEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.minecolonies.api.eventbus.events; | ||
|
||
import com.minecolonies.api.eventbus.IModEvent; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Abstract implementation for this mod bus events. | ||
*/ | ||
public class AbstractModEvent implements IModEvent | ||
{ | ||
/** | ||
* The unique id for this event. | ||
*/ | ||
private final UUID eventId; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
protected AbstractModEvent() | ||
{ | ||
this.eventId = UUID.randomUUID(); | ||
} | ||
|
||
@Override | ||
public UUID getEventId() | ||
{ | ||
return eventId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/minecolonies/api/eventbus/events/CustomRecipesReloadedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.minecolonies.api.eventbus.events; | ||
|
||
import net.neoforged.bus.api.Event; | ||
|
||
/** | ||
* This event is fired on the client side whenever the CustomRecipeManager has been | ||
* populated. This occurs once on world load/connect and again whenever data-packs are reloaded. | ||
*/ | ||
public class CustomRecipesReloadedEvent extends Event | ||
{ | ||
public CustomRecipesReloadedEvent() | ||
{ | ||
} | ||
} |
Oops, something went wrong.