-
-
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.
Merge branch 'version/main' into aipathing
- Loading branch information
Showing
63 changed files
with
803 additions
and
493 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
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.
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
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
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(); | ||
} |
Oops, something went wrong.