Skip to content

Commit

Permalink
feat(JOML): migrate BlockComponent
Browse files Browse the repository at this point in the history
Contributes to #3832
Resolves #4430
  • Loading branch information
skaldarnar committed Feb 6, 2021
1 parent 67cb9bd commit 6e525ac
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.terasology.world.block.BlockComponent;
import org.terasology.world.block.family.BlockFamily;
import org.terasology.world.chunks.Chunk;
import org.terasology.world.chunks.Chunks;

import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -335,9 +336,9 @@ public void send(Event event, EntityRef target) {
try {
BlockComponent blockComp = target.getComponent(BlockComponent.class);
if (blockComp != null) {
if (relevantChunks.contains(ChunkMath.calcChunkPos(JomlUtil.from(blockComp.position), new Vector3i()))) {
if (relevantChunks.contains(Chunks.toChunkPos(blockComp.getPosition(), new Vector3i()))) {
queuedOutgoingEvents.add(NetData.EventMessage.newBuilder()
.setTargetBlockPos(NetMessageUtil.convert(blockComp.position))
.setTargetBlockPos(NetMessageUtil.convert(blockComp.getPosition()))
.setEvent(eventSerializer.serialize(event)).build());
}
} else {
Expand Down Expand Up @@ -493,7 +494,7 @@ private void sendInitialEntities(NetData.NetMessage.Builder message) {
NetData.CreateEntityMessage.Builder createMessage = NetData.CreateEntityMessage.newBuilder().setEntity(entityData);
BlockComponent blockComponent = entity.getComponent(BlockComponent.class);
if (blockComponent != null) {
createMessage.setBlockPos(NetMessageUtil.convert(blockComponent.position));
createMessage.setBlockPos(NetMessageUtil.convert(blockComponent.getPosition()));
}
message.addCreateEntity(createMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private void updateEntity(NetData.UpdateEntityMessage updateEntity) {
entitySerializer.deserializeOnto(currentEntity, updateEntity.getEntity());
BlockComponent blockComponent = currentEntity.getComponent(BlockComponent.class);
if (blockComponent != null && !blockEntityBefore) {
if (!blockEntityRegistry.getExistingBlockEntityAt(blockComponent.getPosition(new Vector3i())).equals(currentEntity)) {
if (!blockEntityRegistry.getExistingBlockEntityAt(blockComponent.getPosition()).equals(currentEntity)) {
logger.error("Failed to associated new block entity");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.terasology.network.serialization;

import gnu.trove.list.TIntList;
import org.joml.Vector3ic;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3i;
Expand Down Expand Up @@ -49,8 +50,8 @@ public NetEntityRefTypeHandler(NetworkSystemImpl networkSystem, BlockEntityRegis
public PersistedData serializeNonNull(EntityRef value, PersistedDataSerializer serializer) {
BlockComponent blockComponent = value.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3i pos = blockComponent.position;
return serializer.serialize(pos.x, pos.y, pos.z);
Vector3ic pos = blockComponent.getPosition();
return serializer.serialize(pos.x(), pos.y(), pos.z());
}
NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
if (netComponent != null) {
Expand Down
62 changes: 20 additions & 42 deletions engine/src/main/java/org/terasology/world/block/BlockComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,90 +15,68 @@
*/
package org.terasology.world.block;

import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.terasology.entitySystem.Component;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector3i;
import org.terasology.network.Replicate;

/**
* Used for entities representing a block in the world
*/
public final class BlockComponent implements Component {
@Replicate
public Vector3i position = new Vector3i();
Vector3i position = new Vector3i();
@Replicate
public Block block;

public BlockComponent() {
}
/**
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #BlockComponent(Block, org.joml.Vector3ic)}.
* {@link #BlockComponent(Block, Vector3ic)}.
*/
@Deprecated
public BlockComponent(Block block, Vector3i pos) {
this.block = block;
this.position.set(pos);
public BlockComponent(Block block, org.terasology.math.geom.Vector3i pos) {
this(block, JomlUtil.from(pos));
}

public BlockComponent(Block block, org.joml.Vector3ic pos) {
public BlockComponent(Block block, Vector3ic pos) {
this.block = block;
this.position.set(JomlUtil.from(pos));
this.position.set(pos);
}

/**
* @deprecated Deprecated on 21/Sep/2018, because it is error prone (no defensive copy) and needlessly verbose.
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getPosition(org.joml.Vector3i)}.
* Get an immutable view on the current position.
*
* Note: the vector may change when the position on this component is updated. If the position information is to be
* stored you should use {@link #getPosition(Vector3i)} instead.
*/
@Deprecated
public Vector3i getPosition() {
public Vector3ic getPosition() {
return position;
}

/**
* @deprecated Deprecated on 21/Sep/2018, because it is needlessly verbose.
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #setPosition(org.joml.Vector3i)}.
*/
@Deprecated
public void setPosition(Vector3i pos) {
position.set(pos);
}

/**
* @deprecated Deprecated on 21/Sep/2018, because it is error prone (no defensive copy) and needlessly verbose.
*/
@Deprecated
public void setBlock(Block block) {
this.block = block;
}

/**
* @deprecated Deprecated on 21/Sep/2018, because it is error prone (no defensive copy) and needlessly verbose.
*/
@Deprecated
public Block getBlock() {
return block;
}

/**
* get the position
*
* @param dest will hold the result
* @return dest
*/
public org.joml.Vector3i getPosition(org.joml.Vector3i dest) {
dest.set(JomlUtil.from(position));
public Vector3i getPosition(Vector3i dest) {
dest.set(position);
return dest;
}

public void setPosition(Vector3ic pos) {
position.set(pos);
}

/**
* set the position of the {@link BlockComponent}
*
* @param pos position to set
*/
public void setPosition(org.joml.Vector3i pos) {
public void setPosition(org.terasology.math.geom.Vector3i pos) {
position.set(JomlUtil.from(pos));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public void replaceBlock(
if (def.isPresent()) {
BlockFamily blockFamily = blockManager.getBlockFamily(uri);
Block block = blockManager.getBlock(blockFamily.getURI());
world.setBlock(targetLocation.position, block);
world.setBlock(targetLocation.getPosition(), block);
} else if (matchingUris.size() > 1) {
StringBuilder builder = new StringBuilder();
builder.append("Non-unique shape name, possible matches: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.common.collect.Sets;
import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.entitySystem.entity.EntityRef;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void onBlockPlaced(OnBlockItemPlaced event, EntityRef entity) {
return;
}

processUpdateForBlockLocation(JomlUtil.from(blockComponent.position));
processUpdateForBlockLocation(blockComponent.getPosition());
}

private void notifyNeighboursOfChangedBlocks() {
Expand All @@ -108,10 +109,9 @@ public void blockUpdate(OnChangedBlock event, EntityRef blockEntity) {
}
}

private void processUpdateForBlockLocation(Vector3i blockLocation) {
private void processUpdateForBlockLocation(Vector3ic blockLocation) {
for (Side side : Side.getAllSides()) {
Vector3i neighborLocation = new Vector3i(blockLocation);
neighborLocation.add(side.direction());
Vector3i neighborLocation = blockLocation.add(side.direction(), new Vector3i());
if (worldProvider.isBlockRelevant(neighborLocation)) {
Block neighborBlock = worldProvider.getBlock(neighborLocation);
final BlockFamily blockFamily = neighborBlock.getBlockFamily();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean shouldBeRemovedDueToChange(Vector3i location, Side sideChanged) {
@ReceiveEvent
public void checkForSupport(DelayedActionTriggeredEvent event, EntityRef entity, BlockComponent block, SideBlockSupportRequiredComponent supportRequired) {
if (event.getActionId().equals(SUPPORT_CHECK_ACTION_ID)) {
if (!isSufficientlySupported(JomlUtil.from(block.position), null, Collections.emptyMap(), supportRequired)) {
if (!isSufficientlySupported(block.getPosition(), null, Collections.emptyMap(), supportRequired)) {
PrefabManager prefabManager = CoreRegistry.get(PrefabManager.class);
entity.send(new DestroyEvent(entity, EntityRef.NULL, prefabManager.getPrefab("engine:supportRemovedDamage")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, B

Optional<Prefab> oldPrefab = oldType.getPrefab();
EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
oldEntityBuilder.addComponent(new BlockComponent(oldType, blockComponent.position));
oldEntityBuilder.addComponent(new BlockComponent(oldType, blockComponent.getPosition()));
BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
blockEntity.send(oldEntityEvent);
for (Component comp : oldEntityEvent.getResultComponents()) {
Expand All @@ -295,7 +295,7 @@ private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, B

Optional<Prefab> newPrefab = type.getPrefab();
EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
newEntityBuilder.addComponent(new BlockComponent(type, blockComponent.position));
newEntityBuilder.addComponent(new BlockComponent(type, blockComponent.getPosition()));
BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
blockEntity.send(newEntityEvent);
for (Component comp : newEntityEvent.getResultComponents()) {
Expand Down Expand Up @@ -417,8 +417,8 @@ public void onActivateBlock(OnActivatedComponent event, EntityRef entity) {
@ReceiveEvent(components = {BlockComponent.class})
public void onDeactivateBlock(BeforeDeactivateComponent event, EntityRef entity) {
BlockComponent block = entity.getComponent(BlockComponent.class);
if (blockEntityLookup.get(block.getPosition(new Vector3i())) == entity) {
blockEntityLookup.remove(block.getPosition(new Vector3i()));
if (blockEntityLookup.get(block.getPosition()) == entity) {
blockEntityLookup.remove(block.getPosition());
}
}

Expand Down Expand Up @@ -517,7 +517,8 @@ public void onEntityComponentRemoved(EntityRef entity, Class<? extends Component
if (entityManager.getComponentLibrary().getMetadata(component).isForceBlockActive()) {
BlockComponent blockComp = entity.getComponent(BlockComponent.class);
if (blockComp != null) {
Block block = getBlock(blockComp.position.x, blockComp.position.y, blockComp.position.z);
Vector3ic blockPosition = blockComp.getPosition();
Block block = getBlock(blockPosition.x(), blockPosition.y(), blockPosition.z());
if (isTemporaryBlock(entity, block, component)) {
temporaryBlockEntities.add(entity);
}
Expand Down

0 comments on commit 6e525ac

Please sign in to comment.