Class CommonAdaptingEventBusWire<P,C>

java.lang.Object
com.blamejared.crafttweaker.api.event.bus.CommonAdaptingEventBusWire<P,C>
Type Parameters:
P - The type of the platform-specific event.
C - The type of the common event.
All Implemented Interfaces:
IEventBusWire

public final class CommonAdaptingEventBusWire<P,C> extends Object implements IEventBusWire
Wire that provides a way to adapt a platform-specific event to a common event, allowing it to fire on both platform-specific buses and common buses as needed.

In other words, an instance of this class will wire its associated IEventBus by first cascading to another specified wiring implementation (usually a platform-specific one), and then setting up a series of listeners that allow redirection of events from the targeted bus to another one, specified when constructing the wire.

To allow for the double-posting to happen, a converting Function and a BiConsumer have to be provided, as further explained in the of(IEventBusWire, IEventBus, Function) and of(IEventBusWire, IEventBus, Function, BiConsumer) factory methods, refer to those for more information.

The general usage pattern of this class sees it being wired accordingly to a scheme similar to the following snippet of code:

     
     class MyCommonEvent {
         final IEventBus<MyCommonEvent> BUS = makeCommonBus();
     }

     class MyPlatformEvent {
         final IEventBus<MyPlatformEvent> BUS = makeBusWithWire(
             CommonAdaptingEventBusWire.of(platformSpecificWire(), MyCommonEvent.BUS, MyCommonEvent::new)
         );
     }
     
 
Since:
11.0.0
  • Method Details

    • of

      public static <C, P> IEventBusWire of(IEventBusWire delegate, IEventBus<C> commonBus, Function<P,C> platformToCommon)
      Constructs a new CommonAdaptingEventBusWire with the given parameters.

      The newly built wire will first delegate to the given delegate for wiring, and then set up a listener that will redirect an event of type C to one of type P on the given commonBus. The conversion will happen accordingly to the specified platformToCommon Function.

      Usage of this method means that no further processing is required once the event dispatch is done to move back information from the C event to the P event, either because objects are shared between the two or because the events are both immutable. If additional behavior is required, please refer to of(IEventBusWire, IEventBus, Function, BiConsumer) instead.

      Type Parameters:
      C - The type of the common event.
      P - The type of the platform event.
      Parameters:
      delegate - The IEventBusWire that will manage the initial wiring of the target event bus.
      commonBus - The IEventBus that will be used to dispatch the event, once converted from its platform-specific form to the common one.
      platformToCommon - A Function that, given as argument an instance of the platform-specific event, will handle the transformation to a common event instance.
      Returns:
      An IEventBusWire that will perform the requested operations.
      Since:
      11.0.0
    • of

      public static <C, P> IEventBusWire of(IEventBusWire delegate, IEventBus<C> commonBus, Function<P,C> platformToCommon, BiConsumer<C,P> commonToPlatform)
      Constructs a new CommonAdaptingEventBusWire with the given parameters.

      The newly built wire will first delegate to the given delegate for wiring, and then set up a listener that will redirect an event of type C to one of type P on the given commonBus. The conversion will happen accordingly to the specified platformToCommon Function. Once the dispatch has been completed, the commonToPlatform BiConsumer will be invoked to allow copying of data from the instance of C to the one of P.

      If no further processing is required, refer to of(IEventBusWire, IEventBus, Function) instead.

      Type Parameters:
      C - The type of the common event.
      P - The type of the platform event.
      Parameters:
      delegate - The IEventBusWire that will manage the initial wiring of the target event bus.
      commonBus - The IEventBus that will be used to dispatch the event, once converted from its platform-specific form to the common one.
      platformToCommon - A Function that, given as argument an instance of the platform-specific event, will handle the transformation to a common event instance.
      commonToPlatform - A BiConsumer that, given as arguments an instance of the common event and one of the platform-specific event, will handle copying of data between the first and the second as necessary.
      Returns:
      An IEventBusWire that will perform the requested operations.
      Since:
      11.0.0
    • registerBusForDispatch

      public <T> void registerBusForDispatch(com.google.common.reflect.TypeToken<T> eventType, IEventBus<T> bus)
      Description copied from interface: IEventBusWire
      Performs the necessary work to wire the given IEventBus to its platform-specific counterpart.
      Specified by:
      registerBusForDispatch in interface IEventBusWire
      Type Parameters:
      T - The type of the events fired in the bus.
      Parameters:
      eventType - The type of the events fired in the bus; this corresponds to invoking IEventBus.eventType() on bus.
      bus - The IEventBus that should be wired.