Skip to content

Commit

Permalink
Port of mod event logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodor12 committed Dec 23, 2024
1 parent e7bef64 commit 1197273
Show file tree
Hide file tree
Showing 35 changed files with 454 additions and 393 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/minecolonies/api/IMinecoloniesAPI.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -101,4 +103,6 @@ static IMinecoloniesAPI getInstance()
void onRegistryNewRegistry(NewRegistryEvent event);

Registry<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 @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -231,4 +232,10 @@ public Registry<EquipmentTypeEntry> getEquipmentTypeRegistry()
{
return apiInstance.getEquipmentTypeRegistry();
}

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

This file was deleted.

This file was deleted.

49 changes: 49 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,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<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);
}
}
}
}
32 changes: 32 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,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 <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.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.
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.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.
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
@@ -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()
{
}
}
Loading

0 comments on commit 1197273

Please sign in to comment.