use of org.terasology.engine.world.block.Block 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.Block in project Terasology by MovingBlocks.
the class AttachSupportRequired method getEntity.
private EntityRef getEntity(Vector3ic location, Map<? extends Vector3ic, Block> blockOverrides) {
final Block overwrittenBlock = blockOverrides.get(location);
if (overwrittenBlock != null) {
return overwrittenBlock.getEntity();
}
EntityRef blockEntity = getBlockEntityRegistry().getExistingBlockEntityAt(location);
if (blockEntity.exists()) {
return blockEntity;
} else {
return getWorldProvider().getBlock(location).getEntity();
}
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class MapWorldProvider method getBlock.
@Override
public Block getBlock(int x, int y, int z) {
Vector3i pos = new Vector3i(x, y, z);
Block block = blocks.get(pos);
if (block != null) {
return block;
}
// TODO block manager
Vector3i chunkPos = Chunks.toChunkPos(pos, new Vector3i());
Chunk chunk = chunks.get(chunkPos);
if (chunk == null && worldGenerator != null) {
chunk = new ChunkImpl(chunkPos, blockManager, extraDataManager);
worldGenerator.createChunk(chunk, entityBuffer);
chunks.put(chunkPos, chunk);
}
if (chunk != null) {
return chunk.getBlock(Chunks.toRelative(pos, pos));
}
return null;
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class NetClient method sendRegisteredBlocks.
private void sendRegisteredBlocks(NetData.NetMessage.Builder message) {
synchronized (newlyRegisteredFamilies) {
for (BlockFamily family : newlyRegisteredFamilies) {
NetData.BlockFamilyRegisteredMessage.Builder blockRegMessage = NetData.BlockFamilyRegisteredMessage.newBuilder();
for (Block block : family.getBlocks()) {
blockRegMessage.addBlockUri(block.getURI().toString());
blockRegMessage.addBlockId(block.getId());
}
message.addBlockFamilyRegistered(blockRegMessage);
}
newlyRegisteredFamilies.clear();
}
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class ServerImpl method processBlockChanges.
/**
* Apply the block changes from the message to the local world.
*/
private void processBlockChanges(NetData.NetMessage message) {
for (NetData.BlockChangeMessage blockChange : message.getBlockChangeList()) {
Block newBlock = blockManager.getBlock((short) blockChange.getNewBlock());
logger.debug("Received block change to {}", newBlock);
// TODO: Store changes to blocks that aren't ready to be modified (the surrounding chunks aren't available)
WorldProvider worldProvider = CoreRegistry.get(WorldProvider.class);
Vector3i pos = NetMessageUtil.convert(blockChange.getPos());
if (worldProvider.isBlockRelevant(pos)) {
worldProvider.setBlock(pos, newBlock);
} else {
awaitingChunkReadyBlockUpdates.put(Chunks.toChunkPos(pos), blockChange);
}
}
}
Aggregations