diff --git a/src/main/java/com/minecolonies/api/IMinecoloniesAPI.java b/src/main/java/com/minecolonies/api/IMinecoloniesAPI.java index 6aaaa8d714a..744c2b9075b 100755 --- a/src/main/java/com/minecolonies/api/IMinecoloniesAPI.java +++ b/src/main/java/com/minecolonies/api/IMinecoloniesAPI.java @@ -1,6 +1,7 @@ package com.minecolonies.api; import com.ldtteam.common.config.Configurations; + import com.minecolonies.api.client.render.modeltype.registry.IModelTypeRegistry; import com.minecolonies.api.colony.ICitizenDataManager; import com.minecolonies.api.colony.IColonyManager; @@ -25,6 +26,7 @@ import com.minecolonies.api.entity.citizen.happiness.HappinessRegistry; import com.minecolonies.api.entity.pathfinding.registry.IPathNavigateRegistry; import com.minecolonies.api.equipment.registry.EquipmentTypeEntry; +import com.minecolonies.api.eventbus.EventBus; import com.minecolonies.api.quests.registries.QuestRegistries; import com.minecolonies.api.research.IGlobalResearchTree; import com.minecolonies.api.research.effects.registry.ResearchEffectEntry; @@ -101,4 +103,6 @@ static IMinecoloniesAPI getInstance() void onRegistryNewRegistry(NewRegistryEvent event); Registry getEquipmentTypeRegistry(); + + EventBus getEventBus(); } diff --git a/src/main/java/com/minecolonies/api/MinecoloniesAPIProxy.java b/src/main/java/com/minecolonies/api/MinecoloniesAPIProxy.java index 742518b8761..83c141a9072 100755 --- a/src/main/java/com/minecolonies/api/MinecoloniesAPIProxy.java +++ b/src/main/java/com/minecolonies/api/MinecoloniesAPIProxy.java @@ -25,6 +25,7 @@ import com.minecolonies.api.entity.citizen.happiness.HappinessRegistry; import com.minecolonies.api.entity.pathfinding.registry.IPathNavigateRegistry; import com.minecolonies.api.equipment.registry.EquipmentTypeEntry; +import com.minecolonies.api.eventbus.EventBus; import com.minecolonies.api.quests.registries.QuestRegistries; import com.minecolonies.api.research.IGlobalResearchTree; import com.minecolonies.api.research.effects.registry.ResearchEffectEntry; @@ -34,7 +35,7 @@ public final class MinecoloniesAPIProxy implements IMinecoloniesAPI { - private static MinecoloniesAPIProxy ourInstance = new MinecoloniesAPIProxy(); + private static final MinecoloniesAPIProxy ourInstance = new MinecoloniesAPIProxy(); private IMinecoloniesAPI apiInstance; @@ -231,4 +232,10 @@ public Registry getEquipmentTypeRegistry() { return apiInstance.getEquipmentTypeRegistry(); } + + @Override + public EventBus getEventBus() + { + return apiInstance.getEventBus(); + } } diff --git a/src/main/java/com/minecolonies/api/colony/buildings/event/BuildingConstructionEvent.java b/src/main/java/com/minecolonies/api/colony/buildings/event/BuildingConstructionEvent.java deleted file mode 100644 index f470839e587..00000000000 --- a/src/main/java/com/minecolonies/api/colony/buildings/event/BuildingConstructionEvent.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.minecolonies.api.colony.buildings.event; - -import com.minecolonies.api.colony.buildings.IBuilding; -import com.minecolonies.api.colony.workorders.WorkOrderType; - -/** - * Event for when a building was built/repaired/removed. - */ -public final class BuildingConstructionEvent extends AbstractBuildingEvent -{ - /** - * What happened to the building. - */ - private final EventType eventType; - - /** - * Building construction event. - * - * @param building the building the event was for. - * @param eventType what happened to the building. - */ - public BuildingConstructionEvent(final IBuilding building, final EventType eventType) - { - super(building); - this.eventType = eventType; - } - - /** - * Get what happened to the building. - * - * @return the event type. - */ - public EventType getEventType() - { - return eventType; - } - - /** - * What happened to the building. - */ - public enum EventType - { - BUILT, - UPGRADED, - REPAIRED, - REMOVED; - - /** - * Obtain the construction event type from the work order type. - * - * @param workOrderType the work order type. - * @return the construction event type. - */ - public static EventType fromWorkOrderType(final WorkOrderType workOrderType) - { - return switch (workOrderType) - { - case BUILD -> BUILT; - case UPGRADE -> UPGRADED; - case REPAIR -> REPAIRED; - case REMOVE -> REMOVED; - }; - } - } -} diff --git a/src/main/java/com/minecolonies/api/colony/event/ColonyInformationChangedEvent.java b/src/main/java/com/minecolonies/api/colony/event/ColonyInformationChangedEvent.java deleted file mode 100644 index 69e62fdb966..00000000000 --- a/src/main/java/com/minecolonies/api/colony/event/ColonyInformationChangedEvent.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.minecolonies.api.colony.event; - -import com.minecolonies.api.colony.IColony; -import org.jetbrains.annotations.NotNull; - -/** - * Colony information changed event. - */ -public class ColonyInformationChangedEvent extends AbstractColonyEvent -{ - /** - * What type of information changed on the colony. - */ - private final Type type; - - /** - * Constructs a colony information changed event. - * - * @param colony the colony related to the event. - * @param type what type of information changed on the colony. - */ - public ColonyInformationChangedEvent(final @NotNull IColony colony, final Type type) - { - super(colony); - this.type = type; - } - - /** - * Get what type of information changed on the colony. - * - * @return the enum value. - */ - public Type getType() - { - return type; - } - - /** - * What information of the colony changed. - */ - public enum Type - { - NAME, - TEAM_COLOR, - FLAG - } -} diff --git a/src/main/java/com/minecolonies/api/eventbus/DefaultEventBus.java b/src/main/java/com/minecolonies/api/eventbus/DefaultEventBus.java new file mode 100644 index 00000000000..997e39247af --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/DefaultEventBus.java @@ -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, List>> eventHandlersPerType = new HashMap<>(); + + @Override + public void subscribe(final @NotNull Class eventType, final @NotNull EventHandler 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> 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 handler : eventHandlers) + { + try + { + ((EventHandler) handler).apply(event); + } + catch (Exception ex) + { + Log.getLogger().warn("Sending event '{}' for type '{}'. Error occurred in handler:", event.getEventId(), event.getClass().getSimpleName(), ex); + } + } + } +} diff --git a/src/main/java/com/minecolonies/api/eventbus/EventBus.java b/src/main/java/com/minecolonies/api/eventbus/EventBus.java new file mode 100644 index 00000000000..6ab350aadcc --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/EventBus.java @@ -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 the generic type of the event class. + */ + void subscribe(final @NotNull Class eventType, final @NotNull EventHandler 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 the generic type of the event class. + */ + @FunctionalInterface + interface EventHandler + { + void apply(final @NotNull T event); + } +} diff --git a/src/main/java/com/minecolonies/api/eventbus/IModEvent.java b/src/main/java/com/minecolonies/api/eventbus/IModEvent.java new file mode 100644 index 00000000000..619d7afa4a7 --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/IModEvent.java @@ -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(); +} diff --git a/src/main/java/com/minecolonies/api/eventbus/events/AbstractModEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/AbstractModEvent.java new file mode 100644 index 00000000000..d4f88210bf4 --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/events/AbstractModEvent.java @@ -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; + } +} diff --git a/src/main/java/com/minecolonies/api/colony/managers/events/ColonyManagerLoadedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/ColonyManagerLoadedModEvent.java similarity index 64% rename from src/main/java/com/minecolonies/api/colony/managers/events/ColonyManagerLoadedEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/ColonyManagerLoadedModEvent.java index bfc7b4d6316..6a45bfce5e4 100644 --- a/src/main/java/com/minecolonies/api/colony/managers/events/ColonyManagerLoadedEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/ColonyManagerLoadedModEvent.java @@ -1,13 +1,12 @@ -package com.minecolonies.api.colony.managers.events; +package com.minecolonies.api.eventbus.events; import com.minecolonies.api.colony.IColonyManager; -import net.neoforged.bus.api.Event; import org.jetbrains.annotations.NotNull; /** * Colony manager loaded event. */ -public final class ColonyManagerLoadedEvent extends Event +public final class ColonyManagerLoadedModEvent extends AbstractModEvent { /** * The colony manager instance. @@ -18,7 +17,7 @@ public final class ColonyManagerLoadedEvent extends Event /** * Event for colony manager loaded. */ - public ColonyManagerLoadedEvent(final @NotNull IColonyManager colonyManager) + public ColonyManagerLoadedModEvent(final @NotNull IColonyManager colonyManager) { this.colonyManager = colonyManager; } @@ -28,7 +27,8 @@ public ColonyManagerLoadedEvent(final @NotNull IColonyManager colonyManager) * * @return the colony manager. */ - public @NotNull IColonyManager getColonyManager() + @NotNull + public IColonyManager getColonyManager() { return colonyManager; } diff --git a/src/main/java/com/minecolonies/api/colony/managers/events/ColonyManagerUnloadedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/ColonyManagerUnloadedModEvent.java similarity index 64% rename from src/main/java/com/minecolonies/api/colony/managers/events/ColonyManagerUnloadedEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/ColonyManagerUnloadedModEvent.java index 827fe9fe27d..7443b63d1fd 100644 --- a/src/main/java/com/minecolonies/api/colony/managers/events/ColonyManagerUnloadedEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/ColonyManagerUnloadedModEvent.java @@ -1,13 +1,12 @@ -package com.minecolonies.api.colony.managers.events; +package com.minecolonies.api.eventbus.events; import com.minecolonies.api.colony.IColonyManager; -import net.neoforged.bus.api.Event; import org.jetbrains.annotations.NotNull; /** * Colony manager unloaded event. */ -public class ColonyManagerUnloadedEvent extends Event +public final class ColonyManagerUnloadedModEvent extends AbstractModEvent { /** * The colony manager instance. @@ -18,7 +17,7 @@ public class ColonyManagerUnloadedEvent extends Event /** * Event for colony manager loaded. */ - public ColonyManagerUnloadedEvent(final @NotNull IColonyManager colonyManager) + public ColonyManagerUnloadedModEvent(final @NotNull IColonyManager colonyManager) { this.colonyManager = colonyManager; } @@ -28,7 +27,8 @@ public ColonyManagerUnloadedEvent(final @NotNull IColonyManager colonyManager) * * @return the colony manager. */ - public @NotNull IColonyManager getColonyManager() + @NotNull + public IColonyManager getColonyManager() { return colonyManager; } diff --git a/src/main/java/com/minecolonies/api/eventbus/events/CustomRecipesReloadedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/CustomRecipesReloadedEvent.java new file mode 100644 index 00000000000..20302a88cff --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/events/CustomRecipesReloadedEvent.java @@ -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() + { + } +} diff --git a/src/main/java/com/minecolonies/api/colony/event/AbstractColonyEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/AbstractColonyModEvent.java similarity index 57% rename from src/main/java/com/minecolonies/api/colony/event/AbstractColonyEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/AbstractColonyModEvent.java index 2a57bad2625..3e60277cb26 100644 --- a/src/main/java/com/minecolonies/api/colony/event/AbstractColonyEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/AbstractColonyModEvent.java @@ -1,37 +1,36 @@ -package com.minecolonies.api.colony.event; - -import com.minecolonies.api.colony.IColony; -import net.neoforged.bus.api.Event; -import org.jetbrains.annotations.NotNull; - -/** - * This is a colony-related event in the Forge sense, not in the - * {@link com.minecolonies.api.colony.colonyEvents.IColonyEvent} sense. - */ -public abstract class AbstractColonyEvent extends Event -{ - /** - * The colony this event was called in. - */ - @NotNull - private final IColony colony; - - /** - * Constructs a colony-based event. - * - * @param colony The colony related to the event. - */ - protected AbstractColonyEvent(@NotNull final IColony colony) - { - this.colony = colony; - } - - /** - * Gets the colony related to the event. - */ - @NotNull - public IColony getColony() - { - return colony; - } -} +package com.minecolonies.api.eventbus.events.colony; + +import com.minecolonies.api.colony.IColony; +import com.minecolonies.api.eventbus.events.AbstractModEvent; +import org.jetbrains.annotations.NotNull; + +/** + * Any colony related event, provides the target colony the event occurred in. + */ +public abstract class AbstractColonyModEvent extends AbstractModEvent +{ + /** + * The colony this event was called in. + */ + @NotNull + private final IColony colony; + + /** + * Constructs a colony-based event. + * + * @param colony The colony related to the event. + */ + protected AbstractColonyModEvent(@NotNull final IColony colony) + { + this.colony = colony; + } + + /** + * Gets the colony related to the event. + */ + @NotNull + public IColony getColony() + { + return colony; + } +} diff --git a/src/main/java/com/minecolonies/api/colony/event/ColonyCreatedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyCreatedModEvent.java similarity index 59% rename from src/main/java/com/minecolonies/api/colony/event/ColonyCreatedEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyCreatedModEvent.java index 8b0e1d66fef..8fb54bdc6e7 100644 --- a/src/main/java/com/minecolonies/api/colony/event/ColonyCreatedEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyCreatedModEvent.java @@ -1,4 +1,4 @@ -package com.minecolonies.api.colony.event; +package com.minecolonies.api.eventbus.events.colony; import com.minecolonies.api.colony.IColony; import org.jetbrains.annotations.NotNull; @@ -6,14 +6,14 @@ /** * Colony created event. */ -public class ColonyCreatedEvent extends AbstractColonyEvent +public final class ColonyCreatedModEvent extends AbstractColonyModEvent { /** * Constructs a colony created event. * * @param colony The colony related to the event. */ - public ColonyCreatedEvent(final @NotNull IColony colony) + public ColonyCreatedModEvent(final @NotNull IColony colony) { super(colony); } diff --git a/src/main/java/com/minecolonies/api/colony/event/ColonyDeletedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyDeletedModEvent.java similarity index 59% rename from src/main/java/com/minecolonies/api/colony/event/ColonyDeletedEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyDeletedModEvent.java index cb08a6daecc..ef0bb7e0089 100644 --- a/src/main/java/com/minecolonies/api/colony/event/ColonyDeletedEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyDeletedModEvent.java @@ -1,4 +1,4 @@ -package com.minecolonies.api.colony.event; +package com.minecolonies.api.eventbus.events.colony; import com.minecolonies.api.colony.IColony; import org.jetbrains.annotations.NotNull; @@ -6,14 +6,14 @@ /** * Colony deleted event. */ -public class ColonyDeletedEvent extends AbstractColonyEvent +public final class ColonyDeletedModEvent extends AbstractColonyModEvent { /** * Constructs a colony deleted event. * * @param colony The colony related to the event. */ - public ColonyDeletedEvent(final @NotNull IColony colony) + public ColonyDeletedModEvent(final @NotNull IColony colony) { super(colony); } diff --git a/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyFlagChangedModEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyFlagChangedModEvent.java new file mode 100644 index 00000000000..a6a6ec1a67d --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyFlagChangedModEvent.java @@ -0,0 +1,20 @@ +package com.minecolonies.api.eventbus.events.colony; + +import com.minecolonies.api.colony.IColony; +import org.jetbrains.annotations.NotNull; + +/** + * Colony flag changed event. + */ +public final class ColonyFlagChangedModEvent extends AbstractColonyModEvent +{ + /** + * Constructs a colony flag changed event. + * + * @param colony the colony related to the event. + */ + public ColonyFlagChangedModEvent(final @NotNull IColony colony) + { + super(colony); + } +} diff --git a/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyNameChangedModEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyNameChangedModEvent.java new file mode 100644 index 00000000000..b457042535b --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyNameChangedModEvent.java @@ -0,0 +1,20 @@ +package com.minecolonies.api.eventbus.events.colony; + +import com.minecolonies.api.colony.IColony; +import org.jetbrains.annotations.NotNull; + +/** + * Colony name changed event. + */ +public final class ColonyNameChangedModEvent extends AbstractColonyModEvent +{ + /** + * Constructs a colony name changed event. + * + * @param colony the colony related to the event. + */ + public ColonyNameChangedModEvent(final @NotNull IColony colony) + { + super(colony); + } +} diff --git a/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyTeamColorChangedModEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyTeamColorChangedModEvent.java new file mode 100644 index 00000000000..00fb7ac90f7 --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyTeamColorChangedModEvent.java @@ -0,0 +1,20 @@ +package com.minecolonies.api.eventbus.events.colony; + +import com.minecolonies.api.colony.IColony; +import org.jetbrains.annotations.NotNull; + +/** + * Colony team changed event. + */ +public final class ColonyTeamColorChangedModEvent extends AbstractColonyModEvent +{ + /** + * Constructs a colony team changed event. + * + * @param colony the colony related to the event. + */ + public ColonyTeamColorChangedModEvent(final @NotNull IColony colony) + { + super(colony); + } +} diff --git a/src/main/java/com/minecolonies/api/colony/event/ColonyViewUpdatedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyViewUpdatedModEvent.java similarity index 69% rename from src/main/java/com/minecolonies/api/colony/event/ColonyViewUpdatedEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyViewUpdatedModEvent.java index 23e7a3f0151..37d6a4a30a6 100644 --- a/src/main/java/com/minecolonies/api/colony/event/ColonyViewUpdatedEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/ColonyViewUpdatedModEvent.java @@ -1,26 +1,26 @@ -package com.minecolonies.api.colony.event; - -import com.minecolonies.api.colony.IColonyView; -import org.jetbrains.annotations.NotNull; - -/** - * This event is raised client-side whenever a particular colony's data is refreshed. - */ -public class ColonyViewUpdatedEvent extends AbstractColonyEvent -{ - /** - * Constructs a new event. - * - * @param colony The colony (view) that was just updated. - */ - public ColonyViewUpdatedEvent(final @NotNull IColonyView colony) - { - super(colony); - } - - @Override - public @NotNull IColonyView getColony() - { - return (IColonyView) super.getColony(); - } -} +package com.minecolonies.api.eventbus.events.colony; + +import com.minecolonies.api.colony.IColonyView; +import org.jetbrains.annotations.NotNull; + +/** + * This event is raised client-side whenever a particular colony's data is refreshed. + */ +public final class ColonyViewUpdatedModEvent extends AbstractColonyModEvent +{ + /** + * Constructs a new event. + * + * @param colony The colony (view) that was just updated. + */ + public ColonyViewUpdatedModEvent(final @NotNull IColonyView colony) + { + super(colony); + } + + @Override + public @NotNull IColonyView getColony() + { + return (IColonyView) super.getColony(); + } +} \ No newline at end of file diff --git a/src/main/java/com/minecolonies/api/colony/buildings/event/AbstractBuildingEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/buildings/AbstractBuildingModEvent.java similarity index 68% rename from src/main/java/com/minecolonies/api/colony/buildings/event/AbstractBuildingEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/buildings/AbstractBuildingModEvent.java index f5759a3399f..dd0a737e6df 100644 --- a/src/main/java/com/minecolonies/api/colony/buildings/event/AbstractBuildingEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/buildings/AbstractBuildingModEvent.java @@ -1,12 +1,12 @@ -package com.minecolonies.api.colony.buildings.event; +package com.minecolonies.api.eventbus.events.colony.buildings; import com.minecolonies.api.colony.buildings.IBuilding; -import com.minecolonies.api.colony.event.AbstractColonyEvent; +import com.minecolonies.api.eventbus.events.colony.AbstractColonyModEvent; /** * Abstract event for building related things. */ -public abstract class AbstractBuildingEvent extends AbstractColonyEvent +public abstract class AbstractBuildingModEvent extends AbstractColonyModEvent { /** * The building related to the event. @@ -18,7 +18,7 @@ public abstract class AbstractBuildingEvent extends AbstractColonyEvent * * @param building the building related to the event. */ - protected AbstractBuildingEvent(final IBuilding building) + protected AbstractBuildingModEvent(final IBuilding building) { super(building.getColony()); this.building = building; diff --git a/src/main/java/com/minecolonies/api/eventbus/events/colony/buildings/BuildingConstructionModEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/buildings/BuildingConstructionModEvent.java new file mode 100644 index 00000000000..87e133bdf15 --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/buildings/BuildingConstructionModEvent.java @@ -0,0 +1,37 @@ +package com.minecolonies.api.eventbus.events.colony.buildings; + +import com.minecolonies.api.colony.buildings.IBuilding; +import com.minecolonies.core.colony.workorders.WorkOrderBuilding; + +/** + * Event for when a building was built, upgraded, repaired or removed. + */ +public final class BuildingConstructionModEvent extends AbstractBuildingModEvent +{ + /** + * The work order which was completed. + */ + private final WorkOrderBuilding workOrder; + + /** + * Building construction event. + * + * @param building the building the event was for. + * @param workOrder the work order which was completed. + */ + public BuildingConstructionModEvent(final IBuilding building, final WorkOrderBuilding workOrder) + { + super(building); + this.workOrder = workOrder; + } + + /** + * Get the work order which was completed. + * + * @return the event type. + */ + public WorkOrderBuilding getWorkOrder() + { + return workOrder; + } +} diff --git a/src/main/java/com/minecolonies/api/colony/citizens/event/AbstractCitizenEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/AbstractCitizenModEvent.java similarity index 71% rename from src/main/java/com/minecolonies/api/colony/citizens/event/AbstractCitizenEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/AbstractCitizenModEvent.java index 05942def618..eca9aa3123d 100644 --- a/src/main/java/com/minecolonies/api/colony/citizens/event/AbstractCitizenEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/AbstractCitizenModEvent.java @@ -1,14 +1,14 @@ -package com.minecolonies.api.colony.citizens.event; +package com.minecolonies.api.eventbus.events.colony.citizens; import com.minecolonies.api.colony.ICitizen; import com.minecolonies.api.colony.ICitizenData; -import com.minecolonies.api.colony.event.AbstractColonyEvent; +import com.minecolonies.api.eventbus.events.colony.AbstractColonyModEvent; import org.jetbrains.annotations.NotNull; /** * Abstract event for citizen related things. */ -public class AbstractCitizenEvent extends AbstractColonyEvent +public class AbstractCitizenModEvent extends AbstractColonyModEvent { /** * The citizen related to the event. @@ -20,7 +20,7 @@ public class AbstractCitizenEvent extends AbstractColonyEvent * * @param citizen the citizen related to the event. */ - protected AbstractCitizenEvent(final @NotNull ICitizenData citizen) + protected AbstractCitizenModEvent(final @NotNull ICitizenData citizen) { super(citizen.getColony()); this.citizen = citizen; diff --git a/src/main/java/com/minecolonies/api/colony/citizens/event/CitizenAddedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenAddedModEvent.java similarity index 77% rename from src/main/java/com/minecolonies/api/colony/citizens/event/CitizenAddedEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenAddedModEvent.java index c2baf35e0d0..559e5ec8f27 100644 --- a/src/main/java/com/minecolonies/api/colony/citizens/event/CitizenAddedEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenAddedModEvent.java @@ -1,16 +1,16 @@ -package com.minecolonies.api.colony.citizens.event; +package com.minecolonies.api.eventbus.events.colony.citizens; import com.minecolonies.api.colony.ICitizenData; /** * Event for when a citizen was added to the colony. */ -public class CitizenAddedEvent extends AbstractCitizenEvent +public final class CitizenAddedModEvent extends AbstractCitizenModEvent { /** * The way the citizen came into the colony. */ - private final Source source; + private final CitizenAddedSource source; /** * Citizen added event. @@ -18,7 +18,7 @@ public class CitizenAddedEvent extends AbstractCitizenEvent * @param citizen the citizen related to the event. * @param source the way the citizen came into the colony. */ - public CitizenAddedEvent(final ICitizenData citizen, final Source source) + public CitizenAddedModEvent(final ICitizenData citizen, final CitizenAddedSource source) { super(citizen); this.source = source; @@ -29,7 +29,7 @@ public CitizenAddedEvent(final ICitizenData citizen, final Source source) * * @return the enum value. */ - public Source getSource() + public CitizenAddedSource getSource() { return source; } @@ -37,7 +37,7 @@ public Source getSource() /** * How the citizen came into the colony. */ - public enum Source + public enum CitizenAddedSource { /** * The citizen spawned as part of the {@link com.minecolonies.api.configuration.ServerConfiguration#initialCitizenAmount}. diff --git a/src/main/java/com/minecolonies/api/colony/citizens/event/CitizenRemovedEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenDiedModEvent.java similarity index 66% rename from src/main/java/com/minecolonies/api/colony/citizens/event/CitizenRemovedEvent.java rename to src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenDiedModEvent.java index 779372a5be6..c5b9366a573 100644 --- a/src/main/java/com/minecolonies/api/colony/citizens/event/CitizenRemovedEvent.java +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenDiedModEvent.java @@ -1,13 +1,13 @@ -package com.minecolonies.api.colony.citizens.event; +package com.minecolonies.api.eventbus.events.colony.citizens; import com.minecolonies.api.colony.ICitizenData; import net.minecraft.world.damagesource.DamageSource; import org.jetbrains.annotations.NotNull; /** - * Event for when a citizen was removed from the colony. + * Event for when a citizen died in any colony. */ -public class CitizenRemovedEvent extends AbstractCitizenEvent +public final class CitizenDiedModEvent extends AbstractCitizenModEvent { /** * The damage source that caused a citizen to die. @@ -15,12 +15,12 @@ public class CitizenRemovedEvent extends AbstractCitizenEvent private final @NotNull DamageSource source; /** - * Citizen removed event. + * Citizen died event. * * @param citizen the citizen related to the event. - * @param source the way the citizen went out of the colony. + * @param source the damage source the citizen died from. */ - public CitizenRemovedEvent(final @NotNull ICitizenData citizen, final @NotNull DamageSource source) + public CitizenDiedModEvent(final @NotNull ICitizenData citizen, final @NotNull DamageSource source) { super(citizen); this.source = source; diff --git a/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenRemovedModEvent.java b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenRemovedModEvent.java new file mode 100644 index 00000000000..859bd3a0b34 --- /dev/null +++ b/src/main/java/com/minecolonies/api/eventbus/events/colony/citizens/CitizenRemovedModEvent.java @@ -0,0 +1,39 @@ +package com.minecolonies.api.eventbus.events.colony.citizens; + +import com.minecolonies.api.colony.ICitizenData; +import net.minecraft.world.entity.Entity; +import org.jetbrains.annotations.NotNull; + +/** + * Event for when a citizen was removed from the colony. + */ +public final class CitizenRemovedModEvent extends AbstractCitizenModEvent +{ + /** + * The damage source that caused a citizen to die. + */ + private final @NotNull Entity.RemovalReason reason; + + /** + * Citizen removed event. + * + * @param citizen the citizen related to the event. + * @param reason the reason the citizen was removed. + */ + public CitizenRemovedModEvent(final @NotNull ICitizenData citizen, final @NotNull Entity.RemovalReason reason) + { + super(citizen); + this.reason = reason; + } + + /** + * The damage source that caused the citizen to die. + * + * @return the damage source. + */ + @NotNull + public Entity.RemovalReason getRemovalReason() + { + return reason; + } +} diff --git a/src/main/java/com/minecolonies/api/events/ColonyEvents.java b/src/main/java/com/minecolonies/api/events/ColonyEvents.java deleted file mode 100644 index 122da1f3689..00000000000 --- a/src/main/java/com/minecolonies/api/events/ColonyEvents.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.minecolonies.api.events; - -import com.minecolonies.api.colony.IColony; -import com.minecolonies.api.colony.event.ColonyDeletedEvent; -import com.minecolonies.api.util.Log; -import net.neoforged.bus.api.Event; -import net.neoforged.neoforge.common.NeoForge; - -/** - * Event manager for all forge events. - */ -public class ColonyEvents -{ - /** - * Event triggered when a colony is being deleted. - * - * @param colony the colony in question. - */ - public static void deleteColony(final IColony colony) - { - sendEventSafe(new ColonyDeletedEvent(colony)); - } - - /** - * Underlying logic for transmitting an event. - */ - private static void sendEventSafe(final Event event) - { - try - { - NeoForge.EVENT_BUS.post(event); - } - catch (final Exception e) - { - Log.getLogger().atError().withThrowable(e).log("Exception occurred during {} event", event.getClass().getName()); - } - } -} diff --git a/src/main/java/com/minecolonies/apiimp/CommonMinecoloniesAPIImpl.java b/src/main/java/com/minecolonies/apiimp/CommonMinecoloniesAPIImpl.java index 36c79b41db1..66246caccd9 100755 --- a/src/main/java/com/minecolonies/apiimp/CommonMinecoloniesAPIImpl.java +++ b/src/main/java/com/minecolonies/apiimp/CommonMinecoloniesAPIImpl.java @@ -27,6 +27,8 @@ import com.minecolonies.api.entity.citizen.happiness.HappinessRegistry; import com.minecolonies.api.entity.pathfinding.registry.IPathNavigateRegistry; import com.minecolonies.api.equipment.registry.EquipmentTypeEntry; +import com.minecolonies.api.eventbus.DefaultEventBus; +import com.minecolonies.api.eventbus.EventBus; import com.minecolonies.api.quests.registries.QuestRegistries; import com.minecolonies.api.research.IGlobalResearchTree; import com.minecolonies.api.research.effects.registry.ResearchEffectEntry; @@ -103,6 +105,8 @@ public class CommonMinecoloniesAPIImpl implements IMinecoloniesAPI private Registry happinessFunctionRegistry; private Registry equipmentTypeRegistry; + private EventBus eventBus = new DefaultEventBus(); + @Override @NotNull public IColonyManager getColonyManager() @@ -322,5 +326,11 @@ public Registry getEquipmentTypeRegistry() { return equipmentTypeRegistry; } + + @Override + public EventBus getEventBus() + { + return eventBus; + } } diff --git a/src/main/java/com/minecolonies/core/colony/ColonyManager.java b/src/main/java/com/minecolonies/core/colony/ColonyManager.java index d05414a894c..12e731ab7d0 100755 --- a/src/main/java/com/minecolonies/core/colony/ColonyManager.java +++ b/src/main/java/com/minecolonies/core/colony/ColonyManager.java @@ -1,6 +1,7 @@ package com.minecolonies.core.colony; import com.google.common.collect.Maps; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.blocks.AbstractBlockHut; import com.minecolonies.api.colony.*; import com.minecolonies.api.colony.buildings.IBuilding; @@ -8,14 +9,14 @@ import com.minecolonies.api.colony.claim.ChunkClaimData; import com.minecolonies.api.colony.claim.IChunkClaimData; import com.minecolonies.api.colony.savedata.IServerColonySaveData; -import com.minecolonies.api.colony.event.ColonyViewUpdatedEvent; -import com.minecolonies.api.colony.managers.events.ColonyManagerLoadedEvent; -import com.minecolonies.api.colony.managers.events.ColonyManagerUnloadedEvent; import com.minecolonies.api.colony.permissions.ColonyPlayer; import com.minecolonies.api.compatibility.CompatibilityManager; import com.minecolonies.api.compatibility.ICompatibilityManager; import com.minecolonies.api.crafting.IRecipeManager; -import com.minecolonies.api.events.ColonyEvents; +import com.minecolonies.api.eventbus.events.ColonyManagerLoadedModEvent; +import com.minecolonies.api.eventbus.events.ColonyManagerUnloadedModEvent; +import com.minecolonies.api.eventbus.events.colony.ColonyDeletedModEvent; +import com.minecolonies.api.eventbus.events.colony.ColonyViewUpdatedModEvent; import com.minecolonies.api.sounds.SoundManager; import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.ColonyUtils; @@ -217,7 +218,7 @@ private void deleteColony(@Nullable final IColony iColony, final boolean canDest return; } - ColonyEvents.deleteColony(colony); + IMinecoloniesAPI.getInstance().getEventBus().post(new ColonyDeletedModEvent(colony)); cap.deleteColony(id); BackUpHelper.markColonyDeleted(colony.getID(), colony.getDimension()); colony.getImportantMessageEntityPlayers() @@ -662,14 +663,7 @@ public void onWorldLoad(@NotNull final Level w) c.onWorldLoad(world); } - try - { - NeoForge.EVENT_BUS.post(new ColonyManagerLoadedEvent(this)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during ColonyManagerLoadedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new ColonyManagerLoadedModEvent(this)); } } @@ -690,14 +684,7 @@ public void onWorldUnload(@NotNull final Level world) BackUpHelper.backupColonyData(world.registryAccess()); } - try - { - NeoForge.EVENT_BUS.post(new ColonyManagerUnloadedEvent(this)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during ColonyManagerUnloadedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new ColonyManagerUnloadedModEvent(this)); } } @@ -725,14 +712,7 @@ public void handleColonyViewMessage( } view.handleColonyViewMessage(colonyData, isNewSubscription); - try - { - NeoForge.EVENT_BUS.post(new ColonyViewUpdatedEvent(view)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during ColonyViewUpdatedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new ColonyViewUpdatedModEvent(view)); } @Override diff --git a/src/main/java/com/minecolonies/core/colony/interactionhandling/RecruitmentInteraction.java b/src/main/java/com/minecolonies/core/colony/interactionhandling/RecruitmentInteraction.java index 09a6ddf9f1a..64567c09dab 100644 --- a/src/main/java/com/minecolonies/core/colony/interactionhandling/RecruitmentInteraction.java +++ b/src/main/java/com/minecolonies/core/colony/interactionhandling/RecruitmentInteraction.java @@ -6,13 +6,13 @@ import com.ldtteam.blockui.controls.Text; import com.ldtteam.blockui.views.BOWindow; import com.ldtteam.blockui.views.Box; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.colony.*; -import com.minecolonies.api.colony.citizens.event.CitizenAddedEvent; import com.minecolonies.api.colony.interactionhandling.IChatPriority; import com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler; import com.minecolonies.api.colony.interactionhandling.ModInteractionResponseHandlers; +import com.minecolonies.api.eventbus.events.colony.citizens.CitizenAddedModEvent; import com.minecolonies.api.util.InventoryUtils; -import com.minecolonies.api.util.Log; import com.minecolonies.api.util.MessageUtils; import com.minecolonies.api.util.Tuple; import com.minecolonies.api.util.constant.Constants; @@ -23,7 +23,6 @@ import net.minecraft.world.item.ItemStack; import net.neoforged.api.distmarker.Dist; import net.neoforged.api.distmarker.OnlyIn; -import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.items.wrapper.InvWrapper; import java.util.Collections; import java.util.List; @@ -188,14 +187,7 @@ public void onServerResponseTriggered(final int responseId, final Player player, MessageUtils.format(MESSAGE_RECRUITMENT_SUCCESS, data.getName()).sendTo(colony).forAllPlayers(); } - try - { - NeoForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.HIRED)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during CitizenAddedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new CitizenAddedModEvent(newCitizen, CitizenAddedModEvent.CitizenAddedSource.HIRED)); } } else diff --git a/src/main/java/com/minecolonies/core/colony/managers/CitizenManager.java b/src/main/java/com/minecolonies/core/colony/managers/CitizenManager.java index 1c3ef264e71..d5f3f218ee9 100755 --- a/src/main/java/com/minecolonies/core/colony/managers/CitizenManager.java +++ b/src/main/java/com/minecolonies/core/colony/managers/CitizenManager.java @@ -1,5 +1,6 @@ package com.minecolonies.core.colony.managers; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.MinecoloniesAPIProxy; import com.minecolonies.api.colony.ICitizenData; import com.minecolonies.api.colony.ICitizenDataManager; @@ -7,12 +8,12 @@ import com.minecolonies.api.colony.IColony; import com.minecolonies.api.colony.buildings.HiringMode; import com.minecolonies.api.colony.buildings.IBuilding; -import com.minecolonies.api.colony.citizens.event.CitizenAddedEvent; import com.minecolonies.api.colony.managers.interfaces.ICitizenManager; import com.minecolonies.api.entity.ModEntities; import com.minecolonies.api.entity.citizen.AbstractCivilianEntity; import com.minecolonies.api.entity.citizen.AbstractEntityCitizen; import com.minecolonies.api.entity.citizen.happiness.IHappinessModifier; +import com.minecolonies.api.eventbus.events.colony.citizens.CitizenAddedModEvent; import com.minecolonies.api.util.*; import com.minecolonies.api.util.constant.CitizenConstants; import com.minecolonies.core.MineColonies; @@ -40,7 +41,6 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.Level; -import net.neoforged.neoforge.common.NeoForge; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -361,14 +361,7 @@ public ICitizenData resurrectCivilianData(@NotNull final CompoundTag compoundNBT citizens.put(citizenData.getId(), citizenData); spawnOrCreateCitizen(citizenData, world, spawnPos); - try - { - NeoForge.EVENT_BUS.post(new CitizenAddedEvent(citizenData, CitizenAddedEvent.Source.RESURRECTED)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during CitizenAddedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new CitizenAddedModEvent(citizenData, CitizenAddedModEvent.CitizenAddedSource.RESURRECTED)); return citizenData; } @@ -623,14 +616,7 @@ else if (femaleCount < (getCitizens().size() - 1) / 2.0) spawnOrCreateCivilian(newCitizen, colony.getWorld(), null, true); - try - { - NeoForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.INITIAL)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during CitizenAddedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new CitizenAddedModEvent(newCitizen, CitizenAddedModEvent.CitizenAddedSource.INITIAL)); colony.getEventDescriptionManager().addEventDescription(new CitizenSpawnedEvent(colony.getBuildingManager().getTownHall().getPosition(), newCitizen.getName())); } diff --git a/src/main/java/com/minecolonies/core/commands/citizencommands/CommandCitizenSpawnNew.java b/src/main/java/com/minecolonies/core/commands/citizencommands/CommandCitizenSpawnNew.java index ad334cf3568..711a1e13e60 100755 --- a/src/main/java/com/minecolonies/core/commands/citizencommands/CommandCitizenSpawnNew.java +++ b/src/main/java/com/minecolonies/core/commands/citizencommands/CommandCitizenSpawnNew.java @@ -1,10 +1,10 @@ package com.minecolonies.core.commands.citizencommands; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.colony.ICitizenData; import com.minecolonies.api.colony.IColony; import com.minecolonies.api.colony.IColonyManager; -import com.minecolonies.api.colony.citizens.event.CitizenAddedEvent; -import com.minecolonies.api.util.Log; +import com.minecolonies.api.eventbus.events.colony.citizens.CitizenAddedModEvent; import com.minecolonies.core.commands.commandTypes.IMCCommand; import com.minecolonies.core.commands.commandTypes.IMCOPCommand; import com.mojang.brigadier.arguments.IntegerArgumentType; @@ -12,7 +12,6 @@ import com.mojang.brigadier.context.CommandContext; import net.minecraft.commands.CommandSourceStack; import net.minecraft.network.chat.Component; -import net.neoforged.neoforge.common.NeoForge; import static com.minecolonies.api.util.constant.translation.CommandTranslationConstants.COMMAND_CITIZEN_SPAWN_SUCCESS; import static com.minecolonies.api.util.constant.translation.CommandTranslationConstants.COMMAND_COLONY_ID_NOT_FOUND; @@ -43,14 +42,7 @@ public int onExecute(final CommandContext context) final ICitizenData newCitizen = colony.getCitizenManager().spawnOrCreateCivilian(null, colony.getWorld(), null, true); context.getSource().sendSuccess(() -> Component.translatableEscape(COMMAND_CITIZEN_SPAWN_SUCCESS, newCitizen.getName()), true); - try - { - NeoForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.COMMANDS)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during CitizenAddedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new CitizenAddedModEvent(newCitizen, CitizenAddedModEvent.CitizenAddedSource.COMMANDS)); return 1; } diff --git a/src/main/java/com/minecolonies/core/compatibility/journeymap/EventListener.java b/src/main/java/com/minecolonies/core/compatibility/journeymap/EventListener.java index 5689cee5c78..d6f25efa110 100644 --- a/src/main/java/com/minecolonies/core/compatibility/journeymap/EventListener.java +++ b/src/main/java/com/minecolonies/core/compatibility/journeymap/EventListener.java @@ -1,7 +1,8 @@ package com.minecolonies.core.compatibility.journeymap; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.colony.IColonyView; -import com.minecolonies.api.colony.event.ColonyViewUpdatedEvent; +import com.minecolonies.api.eventbus.events.colony.ColonyViewUpdatedModEvent; import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; import net.minecraft.resources.ResourceKey; @@ -29,6 +30,7 @@ public EventListener(@NotNull final Journeymap jmap) this.jmap = jmap; NeoForge.EVENT_BUS.register(this); + IMinecoloniesAPI.getInstance().getEventBus().subscribe(ColonyViewUpdatedModEvent.class, this::onColonyViewUpdated); } @SubscribeEvent @@ -53,7 +55,7 @@ public void onChunkLoaded(@NotNull final ChunkEvent.Load event) } @SubscribeEvent - public void onColonyViewUpdated(@NotNull final ColonyViewUpdatedEvent event) + public void onColonyViewUpdated(@NotNull final ColonyViewUpdatedModEvent event) { final IColonyView colony = event.getColony(); final Set graves = colony.getGraveManager().getGraves().keySet(); diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructureWithWorkOrder.java b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructureWithWorkOrder.java index 5687c975c81..6e54d6309eb 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructureWithWorkOrder.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/AbstractEntityAIStructureWithWorkOrder.java @@ -6,13 +6,14 @@ import com.ldtteam.structurize.placement.StructurePhasePlacementResult; import com.ldtteam.structurize.placement.StructurePlacer; import com.ldtteam.structurize.util.BlockUtils; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.colony.IColony; import com.minecolonies.api.colony.buildings.IBuilding; -import com.minecolonies.api.colony.buildings.event.BuildingConstructionEvent; import com.minecolonies.api.colony.workorders.IWorkOrder; import com.minecolonies.api.colony.workorders.WorkOrderType; import com.minecolonies.api.crafting.ItemStorage; import com.minecolonies.api.entity.ai.statemachine.states.IAIState; +import com.minecolonies.api.eventbus.events.colony.buildings.BuildingConstructionModEvent; import com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding; import com.minecolonies.api.util.*; import com.minecolonies.api.util.constant.Constants; @@ -31,7 +32,6 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.BlockEntity; -import net.neoforged.neoforge.common.NeoForge; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -420,31 +420,23 @@ public void executeSpecificCompleteActions() job.complete(); - if (wo instanceof WorkOrderBuilding) + if (wo instanceof WorkOrderBuilding workOrderBuilding) { final IBuilding building = colony.getBuildingManager().getBuilding(wo.getLocation()); - try - { - NeoForge.EVENT_BUS.post(new BuildingConstructionEvent(building, BuildingConstructionEvent.EventType.fromWorkOrderType(wo.getWorkOrderType()))); - } - catch (final Exception e) + if (building == null) { - Log.getLogger().error("Error during BuildingConstructionEvent", e); + Log.getLogger().error(String.format("Builder (%d:%d) ERROR - Finished, but missing building(%s)", + worker.getCitizenColonyHandler().getColonyOrRegister().getID(), + worker.getCitizenData().getId(), + wo.getLocation())); } - switch (wo.getWorkOrderType()) + else { - case BUILD: - case UPGRADE: - case REPAIR: - if (building == null) - { - Log.getLogger().error(String.format("Builder (%d:%d) ERROR - Finished, but missing building(%s)", - worker.getCitizenColonyHandler().getColonyOrRegister().getID(), - worker.getCitizenData().getId(), - wo.getLocation())); - } - else - { + switch (wo.getWorkOrderType()) + { + case BUILD: + case UPGRADE: + case REPAIR: // Normally levels are done through the schematic data, but in case it is missing we do it manually here. final BlockEntity te = worker.level().getBlockEntity(building.getID()); if (te instanceof AbstractTileEntityColonyBuilding && ((IBlueprintDataProviderBE) te).getSchematicName().isEmpty()) @@ -452,21 +444,12 @@ public void executeSpecificCompleteActions() building.onUpgradeComplete(wo.getTargetLevel()); building.setBuildingLevel(wo.getTargetLevel()); } - } - break; - case REMOVE: - if (building == null) - { - Log.getLogger().error(String.format("Builder (%d:%d) ERROR - Finished, but missing building(%s)", - worker.getCitizenColonyHandler().getColonyOrRegister().getID(), - worker.getCitizenData().getId(), - wo.getLocation())); - } - else - { + break; + case REMOVE: building.setDeconstructed(); - } - break; + break; + } + IMinecoloniesAPI.getInstance().getEventBus().post(new BuildingConstructionModEvent(building, workOrderBuilding)); } } } diff --git a/src/main/java/com/minecolonies/core/network/messages/server/CreateColonyMessage.java b/src/main/java/com/minecolonies/core/network/messages/server/CreateColonyMessage.java index 9221649457c..cb00efe9a78 100755 --- a/src/main/java/com/minecolonies/core/network/messages/server/CreateColonyMessage.java +++ b/src/main/java/com/minecolonies/core/network/messages/server/CreateColonyMessage.java @@ -3,11 +3,11 @@ import com.ldtteam.common.network.AbstractServerPlayMessage; import com.ldtteam.common.network.PlayMessageType; import com.ldtteam.structurize.storage.StructurePacks; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.colony.IColony; import com.minecolonies.api.colony.IColonyManager; import com.minecolonies.api.colony.buildings.IBuilding; -import com.minecolonies.api.colony.event.ColonyCreatedEvent; -import com.minecolonies.api.util.Log; +import com.minecolonies.api.eventbus.events.colony.ColonyCreatedModEvent; import com.minecolonies.core.network.messages.client.colony.OpenBuildingUIMessage; import com.minecolonies.core.tileentities.TileEntityColonyBuilding; import com.minecolonies.api.util.BlockPosUtil; @@ -20,7 +20,6 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.block.entity.BlockEntity; -import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.network.handling.IPayloadContext; import java.util.ArrayList; @@ -170,16 +169,9 @@ else if (spawnDistance > MineColonies.getConfig().getServer().maxDistanceFromWor .sendTo(sender); } + IMinecoloniesAPI.getInstance().getEventBus().post(new ColonyCreatedModEvent(createdColony)); new OpenBuildingUIMessage(building).sendToPlayer(sender);; - try - { - NeoForge.EVENT_BUS.post(new ColonyCreatedEvent(createdColony)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during ColonyCreatedEvent", e); - } return; } diff --git a/src/main/java/com/minecolonies/core/network/messages/server/colony/ColonyFlagChangeMessage.java b/src/main/java/com/minecolonies/core/network/messages/server/colony/ColonyFlagChangeMessage.java index 327fe356308..8b0f4e495e9 100644 --- a/src/main/java/com/minecolonies/core/network/messages/server/colony/ColonyFlagChangeMessage.java +++ b/src/main/java/com/minecolonies/core/network/messages/server/colony/ColonyFlagChangeMessage.java @@ -1,16 +1,15 @@ package com.minecolonies.core.network.messages.server.colony; import com.ldtteam.common.network.PlayMessageType; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.colony.IColony; -import com.minecolonies.api.colony.event.ColonyInformationChangedEvent; -import com.minecolonies.api.util.Log; +import com.minecolonies.api.eventbus.events.colony.ColonyFlagChangedModEvent; import com.minecolonies.api.util.Utils; import com.minecolonies.api.util.constant.Constants; import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.block.entity.BannerPatternLayers; -import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.network.handling.IPayloadContext; /** @@ -20,12 +19,15 @@ public class ColonyFlagChangeMessage extends AbstractColonyServerMessage { public static final PlayMessageType TYPE = PlayMessageType.forServer(Constants.MOD_ID, "colony_flag_change", ColonyFlagChangeMessage::new); - /** The chosen list of patterns from the window */ + /** + * The chosen list of patterns from the window + */ private final BannerPatternLayers patterns; /** * Spawn a new change message - * @param colony the colony the player changed the banner in + * + * @param colony the colony the player changed the banner in * @param patternList the list of patterns they set in the banner picker */ public ColonyFlagChangeMessage(final IColony colony, final BannerPatternLayers patternList) @@ -38,14 +40,7 @@ public ColonyFlagChangeMessage(final IColony colony, final BannerPatternLayers p protected void onExecute(final IPayloadContext ctxIn, final ServerPlayer player, final IColony colony) { colony.setColonyFlag(patterns); - try - { - NeoForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.FLAG)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during ColonyInformationChangedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new ColonyFlagChangedModEvent(colony)); } @Override diff --git a/src/main/java/com/minecolonies/core/network/messages/server/colony/TownHallRenameMessage.java b/src/main/java/com/minecolonies/core/network/messages/server/colony/TownHallRenameMessage.java index 2c577c90150..3bc40b0beb9 100755 --- a/src/main/java/com/minecolonies/core/network/messages/server/colony/TownHallRenameMessage.java +++ b/src/main/java/com/minecolonies/core/network/messages/server/colony/TownHallRenameMessage.java @@ -1,20 +1,19 @@ package com.minecolonies.core.network.messages.server.colony; import com.ldtteam.common.network.PlayMessageType; +import com.minecolonies.api.IMinecoloniesAPI; import com.minecolonies.api.colony.IColony; import com.minecolonies.api.colony.IColonyView; -import com.minecolonies.api.colony.event.ColonyInformationChangedEvent; -import com.minecolonies.api.util.Log; +import com.minecolonies.api.eventbus.events.colony.ColonyNameChangedModEvent; import com.minecolonies.api.util.constant.Constants; import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.server.level.ServerPlayer; -import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.network.handling.IPayloadContext; import org.jetbrains.annotations.NotNull; /** - * Message to execute the renaiming of the townHall. + * Message to execute the renaming of the townHall. */ public class TownHallRenameMessage extends AbstractColonyServerMessage { @@ -54,13 +53,6 @@ protected void toBytes(@NotNull final RegistryFriendlyByteBuf buf) protected void onExecute(final IPayloadContext ctxIn, final ServerPlayer player, final IColony colony) { colony.setName(name); - try - { - NeoForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.NAME)); - } - catch (final Exception e) - { - Log.getLogger().error("Error during ColonyInformationChangedEvent", e); - } + IMinecoloniesAPI.getInstance().getEventBus().post(new ColonyNameChangedModEvent(colony)); } }