Skip to content

Commit

Permalink
Convert to our own event handler instead of mod specific event busses
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodor12 committed Nov 17, 2024
1 parent 57fce97 commit 37a5ed1
Show file tree
Hide file tree
Showing 44 changed files with 388 additions and 405 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/minecolonies/api/IMinecoloniesAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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.events.IModEventHandler;
import com.minecolonies.api.eventbus.EventBus;
import com.minecolonies.api.quests.registries.QuestRegistries;
import com.minecolonies.api.research.IGlobalResearchTree;
import com.minecolonies.api.research.ModResearchCostTypes.ResearchCostType;
Expand Down Expand Up @@ -103,5 +103,5 @@ static IMinecoloniesAPI getInstance()

IForgeRegistry<EquipmentTypeEntry> getEquipmentTypeRegistry();

IModEventHandler getEventHandler();
EventBus getEventBus();
}
6 changes: 3 additions & 3 deletions src/main/java/com/minecolonies/api/MinecoloniesAPIProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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.events.IModEventHandler;
import com.minecolonies.api.eventbus.EventBus;
import com.minecolonies.api.quests.registries.QuestRegistries;
import com.minecolonies.api.research.IGlobalResearchTree;
import com.minecolonies.api.research.ModResearchCostTypes.ResearchCostType;
Expand Down Expand Up @@ -238,8 +238,8 @@ public IForgeRegistry<EquipmentTypeEntry> getEquipmentTypeRegistry()
}

@Override
public IModEventHandler getEventHandler()
public EventBus getEventBus()
{
return apiInstance.getEventHandler();
return apiInstance.getEventBus();
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/minecolonies/api/eventbus/DefaultEventBus.java
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);
}
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/minecolonies/api/eventbus/EventBus.java
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 src/main/java/com/minecolonies/api/eventbus/IModEvent.java
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 src/main/java/com/minecolonies/api/eventbus/IModEventType.java
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();
}
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);
}
}
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
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.minecolonies.api.colony.managers.events;
package com.minecolonies.api.eventbus.events;

import com.minecolonies.api.colony.IColonyManager;
import net.minecraftforge.eventbus.api.Event;
import com.minecolonies.api.eventbus.IModEvent;
import com.minecolonies.api.eventbus.IModEventType;
import org.jetbrains.annotations.NotNull;

/**
* Colony manager loaded event.
*/
public final class ColonyManagerLoadedEvent extends Event
public final class ColonyManagerLoadedEvent extends AbstractEvent
{
/**
* The colony manager instance.
Expand All @@ -28,7 +29,8 @@ public ColonyManagerLoadedEvent(final @NotNull IColonyManager colonyManager)
*
* @return the colony manager.
*/
public @NotNull IColonyManager getColonyManager()
@NotNull
public IColonyManager getColonyManager()
{
return colonyManager;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.minecraftforge.eventbus.api.Event;
import org.jetbrains.annotations.NotNull;

/**
* Colony manager unloaded event.
*/
public class ColonyManagerUnloadedEvent extends Event
public final class ColonyManagerUnloadedEvent extends AbstractEvent
{
/**
* The colony manager instance.
Expand All @@ -28,7 +27,8 @@ public ColonyManagerUnloadedEvent(final @NotNull IColonyManager colonyManager)
*
* @return the colony manager.
*/
public @NotNull IColonyManager getColonyManager()
@NotNull
public IColonyManager getColonyManager()
{
return colonyManager;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events;
package com.minecolonies.api.eventbus.events;

import net.minecraftforge.eventbus.api.Event;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import com.minecolonies.api.colony.IColony;
import net.minecraftforge.eventbus.api.Event;
import com.minecolonies.api.eventbus.events.AbstractEvent;
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.
* Any colony related event, provides the target colony the event occurred in.
*/
public abstract class AbstractColonyEvent extends Event
public abstract class AbstractColonyEvent extends AbstractEvent
{
/**
* The colony this event was called in.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.eventbus.api.Event;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import com.minecolonies.api.colony.IColony;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import com.minecolonies.api.colony.IColony;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import com.minecolonies.api.colony.IColony;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import com.minecolonies.api.colony.IColony;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import com.minecolonies.api.colony.IColony;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony;
package com.minecolonies.api.eventbus.events.colony;

import com.minecolonies.api.colony.IColonyView;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.minecolonies.api.events.colony.buildings;
package com.minecolonies.api.eventbus.events.colony.buildings;

import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.api.events.colony.AbstractColonyEvent;
import com.minecolonies.api.eventbus.events.colony.AbstractColonyEvent;

/**
* Abstract event for building related things.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecolonies.api.events.colony.buildings;
package com.minecolonies.api.eventbus.events.colony.buildings;

import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.core.colony.workorders.WorkOrderBuilding;
Expand Down
Loading

0 comments on commit 37a5ed1

Please sign in to comment.