-
-
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.
Convert to our own event handler instead of mod specific event busses
- Loading branch information
Showing
44 changed files
with
388 additions
and
405 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
package com.minecolonies.api.eventbus; | ||
|
||
import com.google.gson.reflect.TypeToken; | ||
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<TypeToken<?>, List<EventHandler<?>>> eventHandlersPerType = new HashMap<>(); | ||
|
||
@Override | ||
public <T extends IModEvent> void subscribe(final @NotNull IModEventType<T> eventType, final @NotNull EventHandler<T> handler) | ||
{ | ||
Log.getLogger().debug("Registering event handler for id {}.", eventType.getIdentifier()); | ||
|
||
eventHandlersPerType.putIfAbsent(eventType.getIdentifier(), new ArrayList<>()); | ||
eventHandlersPerType.get(eventType.getIdentifier()).add(handler); | ||
} | ||
|
||
@Override | ||
public <T extends IModEvent> void post(final @NotNull IModEventType<T> eventType, final @NotNull T event) | ||
{ | ||
final List<EventHandler<?>> eventHandlers = eventHandlersPerType.get(eventType.getIdentifier()); | ||
if (eventHandlers == null) | ||
{ | ||
return; | ||
} | ||
|
||
Log.getLogger().debug("Event id: {}; Sending event for type '{}' to {} handlers.", event.getEventId(), eventType.getIdentifier(), eventHandlers.size()); | ||
|
||
for (final EventHandler<?> handler : eventHandlers) | ||
{ | ||
try | ||
{ | ||
((EventHandler<T>) handler).apply(event); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.getLogger().warn("Event id: {};'Error occurred during processing event handler:", event.getEventId(), 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,38 @@ | ||
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 type to subscribe to. | ||
* @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 IModEventType<T> eventType, final @NotNull EventHandler<T> handler); | ||
|
||
/** | ||
* Posts a new event on the event bus for the given type. | ||
* | ||
* @param eventType the event type to subscribe to. | ||
* @param event the event to send. | ||
* @param <T> the generic type of the event class. | ||
*/ | ||
<T extends IModEvent> void post(final @NotNull IModEventType<T> eventType, final @NotNull T 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(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/minecolonies/api/eventbus/IModEventType.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,13 @@ | ||
package com.minecolonies.api.eventbus; | ||
|
||
import com.google.gson.reflect.TypeToken; | ||
|
||
/** | ||
* Interface for event types, must define an identifier. | ||
* | ||
* @param <T> the generic type of the event class. | ||
*/ | ||
public interface IModEventType<T extends IModEvent> | ||
{ | ||
TypeToken<T> getIdentifier(); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/minecolonies/api/eventbus/MinecoloniesEventType.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,27 @@ | ||
package com.minecolonies.api.eventbus; | ||
|
||
import com.google.gson.reflect.TypeToken; | ||
|
||
/** | ||
* Simple implementation for an event type. | ||
* | ||
* @param <T> the generic type of the event class. | ||
*/ | ||
public final class MinecoloniesEventType<T extends IModEvent> implements IModEventType<T> | ||
{ | ||
/** | ||
* The class type of the event. | ||
*/ | ||
private final Class<T> eventClass; | ||
|
||
MinecoloniesEventType(final Class<T> eventClass) | ||
{ | ||
this.eventClass = eventClass; | ||
} | ||
|
||
@Override | ||
public TypeToken<T> getIdentifier() | ||
{ | ||
return TypeToken.get(this.eventClass); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/minecolonies/api/eventbus/MinecoloniesEventTypes.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,37 @@ | ||
package com.minecolonies.api.eventbus; | ||
|
||
import com.minecolonies.api.eventbus.events.ColonyManagerLoadedEvent; | ||
import com.minecolonies.api.eventbus.events.ColonyManagerUnloadedEvent; | ||
import com.minecolonies.api.eventbus.events.colony.*; | ||
import com.minecolonies.api.eventbus.events.colony.buildings.BuildingConstructionEvent; | ||
import com.minecolonies.api.eventbus.events.colony.citizens.CitizenAddedEvent; | ||
import com.minecolonies.api.eventbus.events.colony.citizens.CitizenDiedEvent; | ||
import com.minecolonies.api.eventbus.events.colony.citizens.CitizenRemovedEvent; | ||
|
||
/** | ||
* All possible minecolonies mod bus events. | ||
*/ | ||
public final class MinecoloniesEventTypes | ||
{ | ||
// Colony manager events | ||
public static final IModEventType<ColonyManagerLoadedEvent> COLONY_MANAGER_LOADED = new MinecoloniesEventType<>(ColonyManagerLoadedEvent.class); | ||
public static final IModEventType<ColonyManagerUnloadedEvent> COLONY_MANAGER_UNLOADED = new MinecoloniesEventType<>(ColonyManagerUnloadedEvent.class); | ||
|
||
// Colony events | ||
public static final IModEventType<ColonyCreatedEvent> COLONY_CREATED = new MinecoloniesEventType<>(ColonyCreatedEvent.class); | ||
public static final IModEventType<ColonyDeletedEvent> COLONY_DELETED = new MinecoloniesEventType<>(ColonyDeletedEvent.class); | ||
public static final IModEventType<ColonyNameChangedEvent> COLONY_NAME_CHANGED = new MinecoloniesEventType<>(ColonyNameChangedEvent.class); | ||
public static final IModEventType<ColonyTeamColorChangedEvent> COLONY_TEAM_COLOR_CHANGED = new MinecoloniesEventType<>(ColonyTeamColorChangedEvent.class); | ||
public static final IModEventType<ColonyFlagChangedEvent> COLONY_FLAG_CHANGED = new MinecoloniesEventType<>(ColonyFlagChangedEvent.class); | ||
public static final IModEventType<ColonyViewUpdatedEvent> COLONY_VIEW_UPDATED = new MinecoloniesEventType<>(ColonyViewUpdatedEvent.class); | ||
|
||
// Colony building events | ||
public static final IModEventType<BuildingConstructionEvent> BUILDING_COMPLETED = new MinecoloniesEventType<>(BuildingConstructionEvent.class); | ||
|
||
// Colony citizen events | ||
public static final IModEventType<CitizenAddedEvent> CITIZEN_ADDED = new MinecoloniesEventType<>(CitizenAddedEvent.class); | ||
public static final IModEventType<CitizenDiedEvent> CITIZEN_DIED = new MinecoloniesEventType<>(CitizenDiedEvent.class); | ||
public static final IModEventType<CitizenRemovedEvent> CITIZEN_REMOVED = new MinecoloniesEventType<>(CitizenRemovedEvent.class); | ||
|
||
// Other events | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/minecolonies/api/eventbus/events/AbstractEvent.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 AbstractEvent implements IModEvent | ||
{ | ||
/** | ||
* The unique id for this event. | ||
*/ | ||
private final UUID eventId; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
protected AbstractEvent() | ||
{ | ||
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
2 changes: 1 addition & 1 deletion
2
...pi/events/CustomRecipesReloadedEvent.java → ...us/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
9 changes: 4 additions & 5 deletions
9
...pi/events/colony/AbstractColonyEvent.java → ...us/events/colony/AbstractColonyEvent.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
2 changes: 1 addition & 1 deletion
2
...vents/colony/ClientChunkUpdatedEvent.java → ...vents/colony/ClientChunkUpdatedEvent.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
2 changes: 1 addition & 1 deletion
2
...api/events/colony/ColonyCreatedEvent.java → ...bus/events/colony/ColonyCreatedEvent.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
2 changes: 1 addition & 1 deletion
2
...api/events/colony/ColonyDeletedEvent.java → ...bus/events/colony/ColonyDeletedEvent.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
2 changes: 1 addition & 1 deletion
2
...events/colony/ColonyFlagChangedEvent.java → ...events/colony/ColonyFlagChangedEvent.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
2 changes: 1 addition & 1 deletion
2
...events/colony/ColonyNameChangedEvent.java → ...events/colony/ColonyNameChangedEvent.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
2 changes: 1 addition & 1 deletion
2
...s/colony/ColonyTeamColorChangedEvent.java → ...s/colony/ColonyTeamColorChangedEvent.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
2 changes: 1 addition & 1 deletion
2
...events/colony/ColonyViewUpdatedEvent.java → ...events/colony/ColonyViewUpdatedEvent.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
4 changes: 2 additions & 2 deletions
4
...lony/buildings/AbstractBuildingEvent.java → ...lony/buildings/AbstractBuildingEvent.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
2 changes: 1 addition & 1 deletion
2
.../buildings/BuildingConstructionEvent.java → .../buildings/BuildingConstructionEvent.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
Oops, something went wrong.