Package appeng.api.networking
Class GridHelper
java.lang.Object
appeng.api.networking.GridHelper
A helper responsible for creating new
IGridNode
, connecting existing nodes, and related features.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends GridEvent>
voidaddEventHandler
(Class<T> eventClass, BiConsumer<IGrid, T> handler) Listens to events that are emitted perIGrid
.static <T extends GridEvent,
C extends IGridService>
voidaddGridServiceEventHandler
(Class<T> eventClass, Class<C> cacheClass, BiConsumer<C, T> eventHandler) Forwards grid-wide events to theIGridService
attached to that particularIGrid
.static <T extends GridEvent,
C>
voidaddNodeOwnerEventHandler
(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 particularIGrid
.static <T extends GridEvent,
C>
voidaddNodeOwnerEventHandler
(Class<T> eventClass, Class<C> nodeOwnerClass, Consumer<C> eventHandler) Convenience variant ofaddNodeOwnerEventHandler(Class, Class, BiConsumer)
where the event handler doesn't care about the actual event object.static IGridConnection
Create a direct connection between twoIGridNode
.static <T> IManagedGridNode
createManagedNode
(T owner, IGridNodeListener<T> listener) Creates a managed grid node that makes managing the lifecycle of anIGridNode
easier.static @Nullable IGridNode
getExposedNode
(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos, net.minecraft.core.Direction side) Given a knownIInWorldGridNodeHost
, find an adjacent grid node (i.e.static @Nullable IInWorldGridNodeHost
getNodeHost
(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos) Finds anIInWorldGridNodeHost
at the given world location, or returns null if there isn't one.static <T extends net.minecraft.world.level.block.entity.BlockEntity>
voidonFirstTick
(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.
-
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 perIGrid
. -
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 theIGridService
attached to that particularIGrid
. -
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 particularIGrid
.- 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 ofaddNodeOwnerEventHandler(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 anIInWorldGridNodeHost
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 knownIInWorldGridNodeHost
, 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
Creates a managed grid node that makes managing the lifecycle of anIGridNode
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 orIPart
.listener
- A listener that will adapt events sent by the grid node to the owner.- Returns:
- The managed grid node.
-
createConnection
Create a direct connection between twoIGridNode
.This will be considered as having a distance of 1, regardless of the location of both nodes.
- Parameters:
a
- to be connected gridnodeb
- to be connected gridnode- Throws:
IllegalStateException
- If the nodes are already connected.
-