*/
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
Prefab oldPrefab = Assets.getPrefab(oldType.getPrefab());
EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab);
oldEntityBuilder.addComponent(new BlockComponent(oldType, new Vector3i(blockComponent.getPosition())));
BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab, oldEntityBuilder.iterateComponents());
blockEntity.send(oldEntityEvent);
for (Component comp : oldEntityEvent.getResultComponents()) {
oldEntityBuilder.addComponent(comp);
}
Prefab newPrefab = Assets.getPrefab(type.getPrefab());
EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab);
newEntityBuilder.addComponent(new BlockComponent(type, new Vector3i(blockComponent.getPosition())));
BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab, newEntityBuilder.iterateComponents());
blockEntity.send(newEntityEvent);
for (Component comp : newEntityEvent.getResultComponents()) {
newEntityBuilder.addComponent(comp);
}
for (Component component : blockEntity.iterateComponents()) {
if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass())
&& !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange()
&& !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
blockEntity.removeComponent(component.getClass());
}
}
blockComponent.setBlock(type);
blockEntity.saveComponent(blockComponent);
HealthComponent health = blockEntity.getComponent(HealthComponent.class);
if (health == null && type.isDestructible()) {
blockEntity.addComponent(new HealthComponent(type.getHardness(), type.getHardness() / BLOCK_REGEN_SECONDS, 1.0f));
} else if (health != null && !type.isDestructible()) {
blockEntity.removeComponent(HealthComponent.class);
} else if (health != null && type.isDestructible()) {
health.maxHealth = type.getHardness();
health.currentHealth = Math.min(health.currentHealth, health.maxHealth);
blockEntity.saveComponent(health);
}
for (Component comp : newEntityBuilder.iterateComponents()) {
copyIntoPrefab(blockEntity, comp, retainComponents);
}
}