use of org.terasology.world.block.entity.placement.PlaceBlocks in project Terasology by MovingBlocks.
the class DoorSystem method placeDoor.
@ReceiveEvent(components = { DoorComponent.class, ItemComponent.class })
public void placeDoor(ActivateEvent event, EntityRef entity) {
DoorComponent door = entity.getComponent(DoorComponent.class);
BlockComponent targetBlockComp = event.getTarget().getComponent(BlockComponent.class);
if (targetBlockComp == null) {
event.consume();
return;
}
Vector3f horizDir = new Vector3f(event.getDirection());
horizDir.y = 0;
Side facingDir = Side.inDirection(horizDir);
if (!facingDir.isHorizontal()) {
event.consume();
return;
}
Vector3f offset = new Vector3f(event.getHitPosition());
offset.sub(targetBlockComp.getPosition().toVector3f());
Side offsetDir = Side.inDirection(offset);
Vector3i primePos = new Vector3i(targetBlockComp.getPosition());
primePos.add(offsetDir.getVector3i());
Block primeBlock = worldProvider.getBlock(primePos);
if (!primeBlock.isReplacementAllowed()) {
event.consume();
return;
}
Block belowBlock = worldProvider.getBlock(primePos.x, primePos.y - 1, primePos.z);
Block aboveBlock = worldProvider.getBlock(primePos.x, primePos.y + 1, primePos.z);
// Determine top and bottom blocks
Vector3i bottomBlockPos;
Vector3i topBlockPos;
if (belowBlock.isReplacementAllowed()) {
bottomBlockPos = new Vector3i(primePos.x, primePos.y - 1, primePos.z);
topBlockPos = primePos;
} else if (aboveBlock.isReplacementAllowed()) {
bottomBlockPos = primePos;
topBlockPos = new Vector3i(primePos.x, primePos.y + 1, primePos.z);
} else {
event.consume();
return;
}
Side attachSide = determineAttachSide(facingDir, offsetDir, bottomBlockPos, topBlockPos);
if (attachSide == null) {
event.consume();
return;
}
Side closedSide = facingDir.reverse();
if (closedSide == attachSide || closedSide.reverse() == attachSide) {
closedSide = attachSide.yawClockwise(1);
}
Block newBottomBlock = door.bottomBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
Block newTopBlock = door.topBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, bottomBlockPos, closedSide, Side.TOP);
Map<Vector3i, Block> blockMap = new HashMap<>();
blockMap.put(bottomBlockPos, newBottomBlock);
blockMap.put(topBlockPos, newTopBlock);
PlaceBlocks blockEvent = new PlaceBlocks(blockMap, event.getInstigator());
worldProvider.getWorldEntity().send(blockEvent);
if (!blockEvent.isConsumed()) {
EntityRef newDoor = entityManager.create(door.doorRegionPrefab);
entity.removeComponent(MeshComponent.class);
newDoor.addComponent(new BlockRegionComponent(Region3i.createBounded(bottomBlockPos, topBlockPos)));
Vector3f doorCenter = bottomBlockPos.toVector3f();
doorCenter.y += 0.5f;
newDoor.addComponent(new LocationComponent(doorCenter));
DoorComponent newDoorComp = newDoor.getComponent(DoorComponent.class);
newDoorComp.closedSide = closedSide;
newDoorComp.openSide = attachSide.reverse();
newDoorComp.isOpen = false;
newDoor.saveComponent(newDoorComp);
newDoor.send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
logger.info("Closed Side: {}", newDoorComp.closedSide);
logger.info("Open Side: {}", newDoorComp.openSide);
newDoor.send(new DoorPlacedEvent(event.getInstigator()));
}
}
use of org.terasology.world.block.entity.placement.PlaceBlocks 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 type = blockItem.blockFamily;
Side surfaceSide = Side.inDirection(event.getHitNormal());
Side secondaryDirection = ChunkMath.getSecondaryPlacementDirection(event.getDirection(), 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 = blockComponent.getPosition();
Vector3i placementPos = new Vector3i(targetBlock);
placementPos.add(surfaceSide.getVector3i());
Block block = type.getBlockForPlacement(worldProvider, blockEntityRegistry, placementPos, surfaceSide, secondaryDirection);
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)));
} else {
event.consume();
}
}
recordBlockPlaced(event, type);
event.getInstigator().send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
} else {
event.consume();
}
}
Aggregations