-
-
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.
Update all forge event code, attempt to become loader unaware (#10468)
Introduce a new event handler in the IMinecoloniesAPI, which is backed by an interface containing basic methods for the events we want to send Event handler is implemented in such a way it could be made loader unaware in the future if needed. All logic for sending Forge specific events is ripped out in favor of the API event handler
- Loading branch information
Showing
41 changed files
with
512 additions
and
459 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.
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
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,36 @@ | ||
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
4 changes: 2 additions & 2 deletions
4
.../crafting/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
Oops, something went wrong.