Skip to content

Commit

Permalink
Update all forge event code, attempt to become loader unaware (#10468)
Browse files Browse the repository at this point in the history
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
Thodor12 authored Dec 22, 2024
1 parent 03f58b5 commit 3c8a387
Show file tree
Hide file tree
Showing 41 changed files with 512 additions and 459 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/minecolonies/api/IMinecoloniesAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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.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 @@ -101,4 +102,6 @@ static IMinecoloniesAPI getInstance()
void onRegistryNewRegistry(NewRegistryEvent event);

IForgeRegistry<EquipmentTypeEntry> getEquipmentTypeRegistry();

EventBus getEventBus();
}
9 changes: 8 additions & 1 deletion src/main/java/com/minecolonies/api/MinecoloniesAPIProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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.eventbus.EventBus;
import com.minecolonies.api.quests.registries.QuestRegistries;
import com.minecolonies.api.research.IGlobalResearchTree;
import com.minecolonies.api.research.ModResearchCostTypes.ResearchCostType;
Expand All @@ -32,7 +33,7 @@

public final class MinecoloniesAPIProxy implements IMinecoloniesAPI
{
private static MinecoloniesAPIProxy ourInstance = new MinecoloniesAPIProxy();
private static final MinecoloniesAPIProxy ourInstance = new MinecoloniesAPIProxy();

private IMinecoloniesAPI apiInstance;

Expand Down Expand Up @@ -235,4 +236,10 @@ public IForgeRegistry<EquipmentTypeEntry> getEquipmentTypeRegistry()
{
return apiInstance.getEquipmentTypeRegistry();
}

@Override
public EventBus getEventBus()
{
return apiInstance.getEventBus();
}
}

This file was deleted.

This file was deleted.

52 changes: 52 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,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);
}
}
}
}
36 changes: 36 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,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 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();
}
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;
}
}
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 loaded event.
*/
public final class ColonyManagerLoadedEvent extends Event
public final class ColonyManagerLoadedModEvent extends AbstractModEvent
{
/**
* The colony manager instance.
Expand All @@ -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;
}
Expand All @@ -28,7 +27,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 ColonyManagerUnloadedModEvent extends AbstractModEvent
{
/**
* The colony manager instance.
Expand All @@ -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;
}
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,10 +1,10 @@
package com.minecolonies.core.colony.crafting;
package com.minecolonies.api.eventbus.events;

import net.minecraftforge.eventbus.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 datapacks are reloaded.
* populated. This occurs once on world load/connect and again whenever data-packs are reloaded.
*/
public class CustomRecipesReloadedEvent extends Event
{
Expand Down
Loading

0 comments on commit 3c8a387

Please sign in to comment.