use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class BlockManagerImpl method registerFamily.
@VisibleForTesting
protected void registerFamily(BlockFamily family) {
Preconditions.checkNotNull(family);
logger.info("Registered {}", family);
lock.lock();
try {
RegisteredState newState = new RegisteredState(registeredBlockInfo.get());
newState.registeredFamilyByUri.put(family.getURI(), family);
for (Block block : family.getBlocks()) {
registerBlock(block, newState);
}
registeredBlockInfo.set(newState);
} finally {
lock.unlock();
}
for (BlockRegistrationListener listener : listeners) {
listener.onBlockFamilyRegistered(family);
}
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class BlockCommands method replaceBlock.
@Command(shortDescription = "Replaces a block in front of user", helpText = "Replaces a block in front of the user at the specified max distance", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public void replaceBlock(@Sender EntityRef sender, @CommandParam("blockName") String uri, @CommandParam(value = "maxDistance", required = false) Integer maxDistanceParam) {
int maxDistance = maxDistanceParam != null ? maxDistanceParam : 12;
EntityRef playerEntity = sender.getComponent(ClientComponent.class).character;
EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(playerEntity);
LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
Set<ResourceUrn> matchingUris = Assets.resolveAssetUri(uri, BlockFamilyDefinition.class);
targetSystem.updateTarget(gazeLocation.getWorldPosition(new Vector3f()), gazeLocation.getWorldDirection(new Vector3f()), maxDistance);
EntityRef target = targetSystem.getTarget();
BlockComponent targetLocation = target.getComponent(BlockComponent.class);
if (matchingUris.size() == 1) {
Optional<BlockFamilyDefinition> def = Assets.get(matchingUris.iterator().next(), BlockFamilyDefinition.class);
if (def.isPresent()) {
BlockFamily blockFamily = blockManager.getBlockFamily(uri);
Block block = blockManager.getBlock(blockFamily.getURI());
world.setBlock(targetLocation.getPosition(), block);
} else if (matchingUris.size() > 1) {
StringBuilder builder = new StringBuilder();
builder.append("Non-unique shape name, possible matches: ");
Iterator<ResourceUrn> shapeUris = sortItems(matchingUris).iterator();
while (shapeUris.hasNext()) {
builder.append(shapeUris.next().toString());
if (shapeUris.hasNext()) {
builder.append(", ");
}
}
}
}
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class BlockEntitySystem method defaultDropsHandling.
@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
public void defaultDropsHandling(CreateBlockDropsEvent event, EntityRef entity, ActAsBlockComponent blockComponent) {
if (blockComponent.block != null) {
if (entity.hasComponent(BlockRegionComponent.class)) {
BlockRegionComponent blockRegion = entity.getComponent(BlockRegionComponent.class);
if (blockComponent.dropBlocksInRegion) {
// loop through all the blocks in this region and drop them
for (Vector3ic location : blockRegion.region) {
Block blockInWorld = worldProvider.getBlock(location);
commonDefaultDropsHandling(event, entity, location, blockInWorld.getBlockFamily().getArchetypeBlock());
}
} else {
// just drop the ActAsBlock block
Vector3i location = new Vector3i(blockRegion.region.center(new Vector3f()), RoundingMode.HALF_UP);
commonDefaultDropsHandling(event, entity, location, blockComponent.block.getArchetypeBlock());
}
} else if (entity.hasComponent(LocationComponent.class)) {
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
Vector3i location = new Vector3i(locationComponent.getWorldPosition(new Vector3f()), RoundingMode.HALF_UP);
commonDefaultDropsHandling(event, entity, location, blockComponent.block.getArchetypeBlock());
}
}
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class NeighbourBlockFamilyUpdateSystem method processUpdateForBlockLocation.
private void processUpdateForBlockLocation(Vector3ic blockLocation) {
for (Side side : Side.values()) {
Vector3i neighborLocation = blockLocation.add(side.direction(), new Vector3i());
if (worldProvider.isBlockRelevant(neighborLocation)) {
Block neighborBlock = worldProvider.getBlock(neighborLocation);
final BlockFamily blockFamily = neighborBlock.getBlockFamily();
if (blockFamily instanceof UpdatesWithNeighboursFamily) {
UpdatesWithNeighboursFamily neighboursFamily = (UpdatesWithNeighboursFamily) blockFamily;
Block neighborBlockAfterUpdate = neighboursFamily.getBlockForNeighborUpdate(neighborLocation, neighborBlock);
if (neighborBlock != neighborBlockAfterUpdate) {
worldProvider.setBlock(neighborLocation, neighborBlockAfterUpdate);
}
}
}
}
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class MultiConnectFamily method registerBlock.
/**
* @param root The root block URI of the family
* @param definition The definition of the block family as passed down from the engine
* @param blockBuilder The block builder to make the blocks in the family
* @param sides A byte representing the sides which should be connected for this block
* @param rotations All of the ways the block should be rotated
* @return All of the rotations possible for the block with the given sides
*/
public Set<Block> registerBlock(BlockUri root, BlockFamilyDefinition definition, final BlockBuilderHelper blockBuilder, byte sides, Iterable<Rotation> rotations) {
Set<Block> result = Sets.newLinkedHashSet();
for (Rotation rotation : rotations) {
byte sideBits = 0;
for (Side side : SideBitFlag.getSides(sides)) {
sideBits |= SideBitFlag.getSide(rotation.rotate(side));
}
BlockUri uri = new BlockUri(root, new Name(String.valueOf(sideBits)));
Block block = blockBuilder.constructTransformedBlock(definition, rotation, uri, this);
block.setUri(uri);
blocks.put(sideBits, block);
result.add(block);
}
return result;
}
Aggregations