use of org.terasology.world.block.BlockUri in project Terasology by MovingBlocks.
the class BlockCommands method giveBlock.
/**
* Called by 'give' command in ItemCommands.java to attempt to put a block in the player's inventory when no item is found.
* Called by 'giveBulkBlock' command in BlockCommands.java to put a block in the player's inventory.
* @return Null if not found, otherwise success or warning message
*/
public String giveBlock(@Sender EntityRef sender, @CommandParam("blockName") String uri, @CommandParam(value = "quantity", required = false) Integer quantityParam, @CommandParam(value = "shapeName", required = false) String shapeUriParam) {
Set<ResourceUrn> matchingUris = Assets.resolveAssetUri(uri, BlockFamilyDefinition.class);
BlockFamily blockFamily = null;
if (matchingUris.size() == 1) {
Optional<BlockFamilyDefinition> def = Assets.get(matchingUris.iterator().next(), BlockFamilyDefinition.class);
if (def.isPresent()) {
if (def.get().isFreeform()) {
if (shapeUriParam == null) {
blockFamily = blockManager.getBlockFamily(new BlockUri(def.get().getUrn(), new ResourceUrn("engine:cube")));
} else {
Set<ResourceUrn> resolvedShapeUris = Assets.resolveAssetUri(shapeUriParam, BlockShape.class);
if (resolvedShapeUris.isEmpty()) {
return "Found block. No shape found for '" + shapeUriParam + "'";
} else if (resolvedShapeUris.size() > 1) {
StringBuilder builder = new StringBuilder();
builder.append("Found block. Non-unique shape name, possible matches: ");
Iterator<ResourceUrn> shapeUris = sortItems(resolvedShapeUris).iterator();
while (shapeUris.hasNext()) {
builder.append(shapeUris.next().toString());
if (shapeUris.hasNext()) {
builder.append(", ");
}
}
return builder.toString();
}
blockFamily = blockManager.getBlockFamily(new BlockUri(def.get().getUrn(), resolvedShapeUris.iterator().next()));
}
} else {
blockFamily = blockManager.getBlockFamily(new BlockUri(def.get().getUrn()));
}
}
if (blockFamily == null) {
// Should never be reached
return "Block not found";
}
int defaultQuantity = blockFamily.getArchetypeBlock().isStackable() ? 16 : 1;
int quantity = quantityParam != null ? quantityParam : defaultQuantity;
return giveBlock(blockFamily, quantity, sender);
} else if (matchingUris.size() > 1) {
StringBuilder builder = new StringBuilder();
builder.append("Non-unique block name, possible matches: ");
Joiner.on(", ").appendTo(builder, matchingUris);
return builder.toString();
}
return null;
}
use of org.terasology.world.block.BlockUri in project Terasology by MovingBlocks.
the class BlockCommands method listBlocks.
@Command(shortDescription = "List all available blocks\nYou can filter by adding the beginning of words after the" + "commands, e.g.: \"listBlocks engine: core:\" will list all blocks from the engine and core module", requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String listBlocks(@CommandParam(value = "startsWith", required = false) String[] startsWith) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Used Blocks");
stringBuilder.append(Console.NEW_LINE);
stringBuilder.append("-----------");
stringBuilder.append(Console.NEW_LINE);
List<BlockUri> registeredBlocks = sortItems(blockManager.listRegisteredBlockUris());
for (BlockUri blockUri : registeredBlocks) {
if (!uriStartsWithAnyString(blockUri.toString(), startsWith)) {
continue;
}
stringBuilder.append(blockUri.toString());
stringBuilder.append(Console.NEW_LINE);
}
stringBuilder.append(Console.NEW_LINE);
stringBuilder.append("Available Blocks");
stringBuilder.append(Console.NEW_LINE);
stringBuilder.append("----------------");
stringBuilder.append(Console.NEW_LINE);
List<BlockUri> availableBlocks = sortItems(blockExplorer.getAvailableBlockFamilies());
for (BlockUri blockUri : availableBlocks) {
if (!uriStartsWithAnyString(blockUri.toString(), startsWith)) {
continue;
}
stringBuilder.append(blockUri.toString());
stringBuilder.append(Console.NEW_LINE);
}
return stringBuilder.toString();
}
use of org.terasology.world.block.BlockUri in project Terasology by MovingBlocks.
the class HorizontalBlockFamilyFactory method createBlockFamily.
@Override
public BlockFamily createBlockFamily(BlockFamilyDefinition definition, BlockShape shape, BlockBuilderHelper blockBuilder) {
if (!definition.isFreeform()) {
throw new IllegalStateException("A shape cannot be provided when creating a family for a non-freeform block family definition");
}
Map<Side, Block> blockMap = Maps.newHashMap();
for (Rotation rot : Rotation.horizontalRotations()) {
Side side = rot.rotate(Side.FRONT);
blockMap.put(side, blockBuilder.constructTransformedBlock(definition, shape, side.toString().toLowerCase(Locale.ENGLISH), rot));
}
BlockUri uri;
if (CUBE_SHAPE_URN.equals(shape.getUrn())) {
uri = new BlockUri(definition.getUrn());
} else {
uri = new BlockUri(definition.getUrn(), shape.getUrn());
}
return new HorizontalBlockFamily(uri, getArchetypeSide(), blockMap, definition.getCategories());
}
use of org.terasology.world.block.BlockUri in project Terasology by MovingBlocks.
the class SymmetricBlockFamilyFactory method createBlockFamily.
@Override
public BlockFamily createBlockFamily(BlockFamilyDefinition definition, BlockShape shape, BlockBuilderHelper blockBuilder) {
if (!definition.isFreeform()) {
throw new IllegalStateException("A shape cannot be provided when creating a family for a non-freeform block family definition");
}
Block block = blockBuilder.constructSimpleBlock(definition, shape);
BlockUri uri;
if (CUBE_SHAPE_URN.equals(shape.getUrn())) {
uri = new BlockUri(definition.getUrn());
} else {
uri = new BlockUri(definition.getUrn(), shape.getUrn());
}
return new SymmetricFamily(uri, block, definition.getCategories());
}
use of org.terasology.world.block.BlockUri in project Terasology by MovingBlocks.
the class UpdatesWithNeighboursFamilyFactory method createBlockFamily.
@Override
public BlockFamily createBlockFamily(BlockFamilyDefinition definition, BlockBuilderHelper blockBuilder) {
TByteObjectMap<String>[] basicBlocks = new TByteObjectMap[7];
TByteObjectMap<Block> blocksForConnections = new TByteObjectHashMap<>();
addConnections(basicBlocks, 0, NO_CONNECTIONS);
addConnections(basicBlocks, 1, ONE_CONNECTION);
addConnections(basicBlocks, 2, TWO_CONNECTIONS_LINE);
addConnections(basicBlocks, 2, TWO_CONNECTIONS_CORNER);
addConnections(basicBlocks, 3, THREE_CONNECTIONS_CORNER);
addConnections(basicBlocks, 3, THREE_CONNECTIONS_T);
addConnections(basicBlocks, 4, FOUR_CONNECTIONS_CROSS);
addConnections(basicBlocks, 4, FOUR_CONNECTIONS_SIDE);
addConnections(basicBlocks, 5, FIVE_CONNECTIONS);
addConnections(basicBlocks, 6, SIX_CONNECTIONS);
BlockUri blockUri = new BlockUri(definition.getUrn());
// Now make sure we have all combinations based on the basic set (above) and rotations
for (byte connections = 0; connections < 64; connections++) {
// Only the allowed connections should be created
if ((connections & connectionSides) == connections) {
Block block = constructBlockForConnections(connections, blockBuilder, definition, basicBlocks);
if (block == null) {
throw new IllegalStateException("Unable to find correct block definition for connections: " + connections);
}
block.setUri(new BlockUri(blockUri, new Name(String.valueOf(connections))));
blocksForConnections.put(connections, block);
}
}
final Block archetypeBlock = blocksForConnections.get(SideBitFlag.getSides(Side.RIGHT, Side.LEFT));
return new UpdatesWithNeighboursFamily(connectionCondition, blockUri, definition.getCategories(), archetypeBlock, blocksForConnections, connectionSides);
}
Aggregations