chore!: limit API for BlockComponent (1/2) (#4528)

- limit visibility of `BlockComponent.position` (not directly accessed in Omega)
- add getter for `BlockComponent.block` and do automatic refactoring to use it
- adjust `EntityAwareWorldProvider` to not access the `block` directly anymore (create a new component instance instead)

Co-authored-by: Tobias Nett <skaldarnar@googlemail.com>
develop
Michael Pollind 2021-03-05 13:13:07 -08:00 committed by GitHub
parent 872f9a5317
commit 0a6cee9553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 11 deletions

View File

@ -44,7 +44,7 @@ public class EntityDestructionAuthoritySystem extends BaseComponentSystem {
if (instigator != null) {
if (entityRef.hasComponent(BlockComponent.class)) {
BlockComponent blockComponent = entityRef.getComponent(BlockComponent.class);
String blockName = blockComponent.block.getDisplayName();
String blockName = blockComponent.getBlock().getDisplayName();
if (instigator.hasComponent(GamePlayStatsComponent.class)) {
GamePlayStatsComponent gamePlayStatsComponent = instigator.getComponent(GamePlayStatsComponent.class);
Map<String, Integer> blockDestroyedMap = gamePlayStatsComponent.blockDestroyedMap;

View File

@ -12,7 +12,7 @@ import org.terasology.network.Replicate;
*/
public final class BlockComponent implements Component {
@Replicate
Vector3i position = new Vector3i();
protected Vector3i position = new Vector3i();
@Replicate
public Block block;
@ -24,6 +24,14 @@ public final class BlockComponent implements Component {
this.position.set(pos);
}
/**
* the block associated with this component
* @return block tied to this entity
*/
public Block getBlock() {
return block;
}
/**
* Get an immutable view on the current position.
*
@ -39,13 +47,11 @@ public final class BlockComponent implements Component {
*
* @param dest will hold the result
* @return dest
* @deprecated use {@link #getPosition()}
*/
@Deprecated
public Vector3i getPosition(Vector3i dest) {
dest.set(position);
return dest;
}
public void setPosition(Vector3ic pos) {
position.set(pos);
}
}

View File

@ -86,14 +86,14 @@ public class BlockEntitySystem extends BaseComponentSystem {
@ReceiveEvent(priority = EventPriority.PRIORITY_LOW)
public void doDestroy(DoDestroyEvent event, EntityRef entity, BlockComponent blockComponent) {
commonDestroyed(event, entity, blockComponent.block);
commonDestroyed(event, entity, blockComponent.getBlock());
worldProvider.setBlock(blockComponent.getPosition(), blockManager.getBlock(BlockManager.AIR_ID));
}
@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
public void defaultDropsHandling(CreateBlockDropsEvent event, EntityRef entity, BlockComponent blockComponent) {
Vector3ic location = blockComponent.getPosition();
commonDefaultDropsHandling(event, entity, location, blockComponent.block);
commonDefaultDropsHandling(event, entity, location, blockComponent.getBlock());
}
@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)

View File

@ -310,9 +310,8 @@ public class EntityAwareWorldProvider extends AbstractWorldProviderDecorator imp
}
}
blockComponent.block = type;
blockEntity.saveComponent(blockComponent);
BlockComponent newBlockComponent = new BlockComponent(type, blockComponent.getPosition());
blockEntity.saveComponent(newBlockComponent);
for (Component comp : newEntityBuilder.iterateComponents()) {
copyIntoPrefab(blockEntity, comp, retainComponents);