Class GridHelper

java.lang.Object
appeng.api.networking.GridHelper

public final class GridHelper extends Object
A helper responsible for creating new IGridNode, connecting existing nodes, and related features.
  • Method Details

    • onFirstTick

      public static <T extends net.minecraft.world.level.block.entity.BlockEntity> void onFirstTick(T blockEntity, Consumer<? super T> callback)
      On the server side, schedule a call to the passed callback once the block entity is in a ticking chunk. Any action which might cause other chunks to be initialized, such as creating a grid node, should not be done immediately when the block entity is added to the world, but should rather be deferred using this function.

      This function should usually be called from BlockEntity.clearRemoved(), for example:

       
       atOverride
       public void clearRemoved() {
           super.clearRemoved();
           GridHelper.onFirstTick(this, MyBlockEntity::onFirstTick);
       }
      
       private void onFirstTick() {
           // First tick logic here, for example:
           this.managedGridNode.create(getLevel(), getBlockPos());
       }
       
       

      Client side this can be safely called, it will do nothing.

    • addEventHandler

      public static <T extends GridEvent> void addEventHandler(Class<T> eventClass, BiConsumer<IGrid,T> handler)
      Listens to events that are emitted per IGrid.
    • addGridServiceEventHandler

      public static <T extends GridEvent, C extends IGridService> void addGridServiceEventHandler(Class<T> eventClass, Class<C> cacheClass, BiConsumer<C,T> eventHandler)
      Forwards grid-wide events to the IGridService attached to that particular IGrid.
    • addNodeOwnerEventHandler

      public static <T extends GridEvent, C> void addNodeOwnerEventHandler(Class<T> eventClass, Class<C> nodeOwnerClass, BiConsumer<C,T> eventHandler)
      Forwards grid-wide events to any node owner of a given type currently connected to that particular IGrid.
      Parameters:
      nodeOwnerClass - The class of node owner to forward the event to. Please note that subclasses are not included.
    • addNodeOwnerEventHandler

      public static <T extends GridEvent, C> void addNodeOwnerEventHandler(Class<T> eventClass, Class<C> nodeOwnerClass, Consumer<C> eventHandler)
      Convenience variant of addNodeOwnerEventHandler(Class, Class, BiConsumer) where the event handler doesn't care about the actual event object.
    • getNodeHost

      @Nullable public static @Nullable IInWorldGridNodeHost getNodeHost(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos)
      Finds an IInWorldGridNodeHost at the given world location, or returns null if there isn't one.
    • getExposedNode

      @Nullable public static @Nullable IGridNode getExposedNode(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos, net.minecraft.core.Direction side)
      Given a known IInWorldGridNodeHost, find an adjacent grid node (i.e. for the purposes of making connections) on another host in the world.

      Nodes that have been destroyed or have not completed initialization will not be returned.

      See Also:
    • createManagedNode

      public static <T> IManagedGridNode createManagedNode(T owner, IGridNodeListener<T> listener)
      Creates a managed grid node that makes managing the lifecycle of an IGridNode easier.

      This method can be called on both server and client.

      Type Parameters:
      T - The type of the owner.
      Parameters:
      owner - The game object that owns the node, such as a block entity or IPart.
      listener - A listener that will adapt events sent by the grid node to the owner.
      Returns:
      The managed grid node.
    • createConnection

      public static IGridConnection createConnection(IGridNode a, IGridNode b)
      Create a direct connection between two IGridNode.

      This will be considered as having a distance of 1, regardless of the location of both nodes.

      Parameters:
      a - to be connected gridnode
      b - to be connected gridnode
      Throws:
      IllegalStateException - If the nodes are already connected.