use of org.terasology.engine.world.OnChangedBlock in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method setBlocks.
// SetBlocks, not SetBlock, is currently triggered by the engine whenever a player places a block.
// This allows for several useful features, such as quickly synchronizing placement across networks.
// However, this means that even if only one block is placed, this is the method being called.
// It must be overridden here to allow an OnChangedBlock event to be properly sent for placed blocks.
@Override
public Map<Vector3ic, Block> setBlocks(Map<? extends Vector3ic, Block> blocks) {
if (GameThread.isCurrentThread()) {
Map<Vector3ic, Block> oldBlocks = super.setBlocks(blocks);
for (Vector3ic vec : oldBlocks.keySet()) {
if (oldBlocks.get(vec) != null) {
EntityRef blockEntity = getBlockEntityAt(vec);
// check for components to be retained when updating the block entity
final Set<Class<? extends Component>> retainComponents = Optional.ofNullable(blockEntity.getComponent(RetainComponentsComponent.class)).map(retainComponentsComponent -> retainComponentsComponent.components).orElse(Collections.emptySet());
updateBlockEntity(blockEntity, vec, oldBlocks.get(vec), blocks.get(vec), false, retainComponents);
}
}
return oldBlocks;
}
return null;
}
use of org.terasology.engine.world.OnChangedBlock in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method updateBlockEntity.
private void updateBlockEntity(EntityRef blockEntity, Vector3ic pos, Block oldType, Block type, boolean forceEntityUpdate, Set<Class<? extends Component>> retainComponents) {
if (type.isKeepActive()) {
temporaryBlockEntities.remove(blockEntity);
} else if (oldType.isKeepActive() && isTemporaryBlock(blockEntity, type)) {
temporaryBlockEntities.add(blockEntity);
}
if (forceEntityUpdate || !(Objects.equal(oldType.getBlockFamily(), type.getBlockFamily()) && Objects.equal(oldType.getPrefab(), type.getPrefab()))) {
updateBlockEntityComponents(blockEntity, oldType, type, retainComponents);
}
OnChangedBlock changedEvent = new OnChangedBlock(pos, type, oldType);
EntityRef regionEntity = blockRegionLookup.get(pos);
if (regionEntity != null) {
regionEntity.send(changedEvent);
}
blockEntity.send(changedEvent);
}
Aggregations