use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class BlockItemSystem method onPlaceBlock.
@ReceiveEvent(components = { BlockItemComponent.class, ItemComponent.class })
public void onPlaceBlock(ActivateEvent event, EntityRef item) {
if (!event.getTarget().exists()) {
event.consume();
return;
}
BlockItemComponent blockItem = item.getComponent(BlockItemComponent.class);
BlockFamily blockFamily = blockItem.blockFamily;
Side surfaceSide = Side.inDirection(event.getHitNormal());
BlockComponent blockComponent = event.getTarget().getComponent(BlockComponent.class);
if (blockComponent == null) {
// If there is no block there (i.e. it's a BlockGroup, we don't allow placing block, try somewhere else)
event.consume();
return;
}
Vector3i targetBlock = new Vector3i(blockComponent.getPosition());
Vector3i placementPos = new Vector3i(targetBlock);
placementPos.add(surfaceSide.direction());
Vector2f relativeAttachmentPosition = getRelativeAttachmentPosition(event);
Block block = blockFamily.getBlockForPlacement(new BlockPlacementData(placementPos, surfaceSide, event.getDirection(), relativeAttachmentPosition));
if (canPlaceBlock(block, targetBlock, placementPos)) {
// TODO: Fix this for changes.
if (networkSystem.getMode().isAuthority()) {
PlaceBlocks placeBlocks = new PlaceBlocks(placementPos, block, event.getInstigator());
worldProvider.getWorldEntity().send(placeBlocks);
if (!placeBlocks.isConsumed()) {
item.send(new OnBlockItemPlaced(placementPos, blockEntityRegistry.getBlockEntityAt(placementPos), event.getInstigator()));
} else {
event.consume();
}
}
recordBlockPlaced(event, blockFamily);
event.getInstigator().send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
} else {
event.consume();
}
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class ServerImpl method updateEntity.
private void updateEntity(NetData.UpdateEntityMessage updateEntity) {
EntityRef currentEntity = networkSystem.getEntity(updateEntity.getNetId());
if (currentEntity.exists()) {
NetworkComponent netComp = currentEntity.getComponent(NetworkComponent.class);
if (netComp == null) {
logger.error("Updating entity with no network component: {}, expected netId {}", currentEntity, updateEntity.getNetId());
return;
}
if (netComp.getNetworkId() != updateEntity.getNetId()) {
logger.error("Network ID wrong before update");
}
boolean blockEntityBefore = currentEntity.hasComponent(BlockComponent.class);
entitySerializer.deserializeOnto(currentEntity, updateEntity.getEntity());
BlockComponent blockComponent = currentEntity.getComponent(BlockComponent.class);
if (blockComponent != null && !blockEntityBefore) {
if (!blockEntityRegistry.getExistingBlockEntityAt(blockComponent.getPosition()).equals(currentEntity)) {
logger.error("Failed to associated new block entity");
}
}
if (netComp.getNetworkId() != updateEntity.getNetId()) {
logger.error("Network ID lost in update: {}, {} -> {}", currentEntity, updateEntity.getNetId(), netComp.getNetworkId());
}
} else {
logger.warn("Received update for non-existent entity {}", updateEntity.getNetId());
}
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class NetworkEventSystemDecorator method broadcastEvent.
private <T extends Event> void broadcastEvent(EntityRef entity, T event, EventMetadata<T> metadata) {
if (networkSystem.getMode().isServer()) {
NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
BlockComponent blockComp = entity.getComponent(BlockComponent.class);
if (netComp != null || blockComp != null) {
Client instigatorClient = null;
if (metadata.isSkipInstigator() && event instanceof NetworkEvent) {
instigatorClient = networkSystem.getOwner(((NetworkEvent) event).getInstigator());
}
for (Client client : networkSystem.getPlayers()) {
if (!client.equals(instigatorClient)) {
client.send(event, entity);
}
}
}
}
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class EventSystemReplayImpl method broadcastEvent.
private void broadcastEvent(EntityRef entity, Event event, EventMetadata metadata) {
if (networkSystem.getMode().isServer()) {
NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
BlockComponent blockComp = entity.getComponent(BlockComponent.class);
if (netComp != null || blockComp != null) {
Client instigatorClient = null;
if (metadata.isSkipInstigator() && event instanceof NetworkEvent) {
instigatorClient = networkSystem.getOwner(((NetworkEvent) event).getInstigator());
}
for (Client client : networkSystem.getPlayers()) {
if (!client.equals(instigatorClient)) {
client.send(event, entity);
}
}
}
}
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class LocalPlayerSystem method onTargetChanged.
@ReceiveEvent
public void onTargetChanged(PlayerTargetChangedEvent event, EntityRef entity) {
EntityRef target = event.getNewTarget();
hasTarget = target.exists();
if (hasTarget) {
LocationComponent location = target.getComponent(LocationComponent.class);
if (location != null) {
BlockComponent blockComp = target.getComponent(BlockComponent.class);
BlockRegionComponent blockRegion = target.getComponent(BlockRegionComponent.class);
if (blockComp != null || blockRegion != null) {
Vector3f blockPos = location.getWorldPosition(new Vector3f());
Block block = worldProvider.getBlock(blockPos);
aabb.set(block.getBounds(blockPos));
} else {
MeshComponent mesh = target.getComponent(MeshComponent.class);
if (mesh != null && mesh.mesh != null) {
aabb.set(mesh.mesh.getAABB());
aabb.transform(new Matrix4f().translationRotateScale(location.getWorldPosition(new Vector3f()), location.getWorldRotation(new Quaternionf()), location.getWorldScale()));
}
}
}
}
}
Aggregations