Tracking particular blocks in the loaded world

Josharias

Conjurer of Grimoires
Contributor
World
SpecOps
I am attempting to find a combination of events that allow tracking each valid block with particular components in the world. This will enable integrating with the BlockNetwork module and allow adding blocks to the in memory network.

Currently I have hooked up to these events:

OnActivatedBlocks which notifies of blocks entering the world through chunk loading.
Code:
@ReceiveEvent
public void createNetworkNodesOnWorldLoad(OnActivatedBlocks event, EntityRef blockType, BlockNetworkNodeComponent blockNetworkNode) {
    // add all these blocks to the list of valid network nodes
}
 
@ReceiveEvent
public void removeNetworkNodesOnWorldUnload(BeforeDeactivateBlocks event, EntityRef blockType, BlockNetworkNodeComponent blockNetworkNode) {
    // remove all these blocks from the list of valid network nodes
}
OnActivatedComponent which should notify when blocks are placed in the world
Code:
@ReceiveEvent
public void createNetworkNode(OnActivatedComponent event, EntityRef entity, BlockNetworkNodeComponent blockNetworkNode, BlockComponent block) {
    // add all this block to the list of valid network nodes
}
 
@ReceiveEvent
public void removeNetworkNode(BeforeDeactivateComponent event, EntityRef entity, BlockNetworkNodeComponent blockNetworkNode, BlockComponent block) {
    // remove this block from the list of valid network nodes
}

There is odd behavior in that after I destroy a block, the BeforeDeactivateComponent fires, and then right afterwards an OnActivatedComponent fires. Upon closer inspection, the second OnActivatedComponent is triggered in EntityAwareWorldProvider.cleanUpTemporaryEntity(...).

This behavior causes the locally cached set of network nodes to get confused as it does not know how to distinguish between a real entity that is valid in the world, and a temporary one.

Is this set of events the correct way to track valid blocks in the world?
 

Immortius

Lead Software Architect
Contributor
Architecture
GUI
Can you please be a bit more specific about exactly what components are involved, and what is being removed/restored? What do you mean when you destroy a block? (Replace with air?)

Anyhow, the expected behavior is that temporary block entities are transparent - they act like they existed before they were created, and continue to exist after they are destroyed. Part of this illusion is that they are restored to the default state of their block before destruction, since that is the state they will have when next inspected. Perhaps there is a bug in this logic?

I would generally expect to see this behavior when a component is removed from a non-keep active block.
 

Josharias

Conjurer of Grimoires
Contributor
World
SpecOps
"destroy a block", meaning I hit it with a fist.

The BlockNetworkNodeComponent is annotated with @ForceBlockActive. It is added to the block in the prefab.

When the second OnActivatedComponent is triggered, the callstack goes back to EntityAwareWorldProvider.java:447 where it is iterating through my prefab that has 2 components: NetworkComponent, and BlockNetworkNodeComponent.
 

Immortius

Lead Software Architect
Contributor
Architecture
GUI
Found it, the prefab for the block entity was not being set to null when changing to a block with no prefab. Added a unit test for this and corrected the error.
 
Top