use of org.terasology.world.block.loader.BlockFamilyDefinition 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(), gazeLocation.getWorldDirection(), 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.world.block.loader.BlockFamilyDefinition 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.loader.BlockFamilyDefinition in project Terasology by MovingBlocks.
the class BlockManagerImpl method loadFamily.
private Optional<BlockFamily> loadFamily(BlockUri uri) {
Optional<BlockFamilyDefinition> familyDef = assetManager.getAsset(uri.getBlockFamilyDefinitionUrn(), BlockFamilyDefinition.class);
if (familyDef.isPresent() && familyDef.get().isLoadable()) {
if (familyDef.get().isFreeform()) {
ResourceUrn shapeUrn;
if (uri.getShapeUrn().isPresent()) {
shapeUrn = uri.getShapeUrn().get();
} else {
shapeUrn = CUBE_SHAPE_URN;
}
Optional<BlockShape> shape = assetManager.getAsset(shapeUrn, BlockShape.class);
if (shape.isPresent()) {
return Optional.of(familyDef.get().createFamily(shape.get(), blockBuilder));
}
} else if (!familyDef.get().isFreeform()) {
return Optional.of(familyDef.get().createFamily(blockBuilder));
}
} else {
logger.error("Family not available: {}", uri);
}
return Optional.empty();
}
use of org.terasology.world.block.loader.BlockFamilyDefinition in project Terasology by MovingBlocks.
the class TerasologyEngine method initAssets.
private void initAssets() {
DefaultBlockFamilyFactoryRegistry familyFactoryRegistry = new DefaultBlockFamilyFactoryRegistry();
rootContext.put(BlockFamilyFactoryRegistry.class, familyFactoryRegistry);
// cast lambdas explicitly to avoid inconsistent compiler behavior wrt. type inference
assetTypeManager.registerCoreAssetType(Prefab.class, (AssetFactory<Prefab, PrefabData>) PojoPrefab::new, false, "prefabs");
assetTypeManager.registerCoreAssetType(BlockShape.class, (AssetFactory<BlockShape, BlockShapeData>) BlockShapeImpl::new, "shapes");
assetTypeManager.registerCoreAssetType(BlockSounds.class, (AssetFactory<BlockSounds, BlockSoundsData>) BlockSounds::new, "blockSounds");
assetTypeManager.registerCoreAssetType(BlockTile.class, (AssetFactory<BlockTile, TileData>) BlockTile::new, "blockTiles");
assetTypeManager.registerCoreAssetType(BlockFamilyDefinition.class, (AssetFactory<BlockFamilyDefinition, BlockFamilyDefinitionData>) BlockFamilyDefinition::new, "blocks");
assetTypeManager.registerCoreFormat(BlockFamilyDefinition.class, new BlockFamilyDefinitionFormat(assetTypeManager.getAssetManager(), familyFactoryRegistry));
assetTypeManager.registerCoreAssetType(UISkin.class, (AssetFactory<UISkin, UISkinData>) UISkin::new, "skins");
assetTypeManager.registerCoreAssetType(BehaviorTree.class, (AssetFactory<BehaviorTree, BehaviorTreeData>) BehaviorTree::new, false, "behaviors");
assetTypeManager.registerCoreAssetType(UIElement.class, (AssetFactory<UIElement, UIData>) UIElement::new, "ui");
for (EngineSubsystem subsystem : allSubsystems) {
subsystem.registerCoreAssetTypes(assetTypeManager);
}
}
use of org.terasology.world.block.loader.BlockFamilyDefinition in project Terasology by MovingBlocks.
the class HeadlessEnvironment method setupAssetManager.
@Override
protected AssetManager setupAssetManager() {
ModuleAwareAssetTypeManager assetTypeManager = new ModuleAwareAssetTypeManager();
// cast lambdas explicitly to avoid inconsistent compiler behavior wrt. type inference
assetTypeManager.registerCoreAssetType(Prefab.class, (AssetFactory<Prefab, PrefabData>) PojoPrefab::new, false, "prefabs");
assetTypeManager.registerCoreAssetType(BlockShape.class, (AssetFactory<BlockShape, BlockShapeData>) BlockShapeImpl::new, "shapes");
assetTypeManager.registerCoreAssetType(BlockSounds.class, (AssetFactory<BlockSounds, BlockSoundsData>) BlockSounds::new, "blockSounds");
assetTypeManager.registerCoreAssetType(BlockTile.class, (AssetFactory<BlockTile, TileData>) BlockTile::new, "blockTiles");
assetTypeManager.registerCoreAssetType(BlockFamilyDefinition.class, (AssetFactory<BlockFamilyDefinition, BlockFamilyDefinitionData>) BlockFamilyDefinition::new, "blocks");
assetTypeManager.registerCoreAssetType(StaticSound.class, NullSound::new, "sounds");
assetTypeManager.registerCoreAssetType(StreamingSound.class, NullStreamingSound::new, "music");
DefaultBlockFamilyFactoryRegistry blockFamilyFactoryRegistry = new DefaultBlockFamilyFactoryRegistry();
blockFamilyFactoryRegistry.setBlockFamilyFactory("horizontal", new HorizontalBlockFamilyFactory());
blockFamilyFactoryRegistry.setBlockFamilyFactory("alignToSurface", new AttachedToSurfaceFamilyFactory());
assetTypeManager.registerCoreFormat(BlockFamilyDefinition.class, new BlockFamilyDefinitionFormat(assetTypeManager.getAssetManager(), blockFamilyFactoryRegistry));
assetTypeManager.registerCoreAssetType(UISkin.class, (AssetFactory<UISkin, UISkinData>) UISkin::new, "skins");
assetTypeManager.registerCoreAssetType(BehaviorTree.class, (AssetFactory<BehaviorTree, BehaviorTreeData>) BehaviorTree::new, false, "behaviors");
assetTypeManager.registerCoreAssetType(UIElement.class, (AssetFactory<UIElement, UIData>) UIElement::new, "ui");
assetTypeManager.registerCoreAssetType(Font.class, (AssetFactory<Font, FontData>) FontImpl::new, "fonts");
assetTypeManager.registerCoreAssetType(Texture.class, (AssetFactory<Texture, TextureData>) HeadlessTexture::new, "textures", "fonts");
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.NEAREST, path -> path.getName(2).toString().equals("textures")));
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.LINEAR, path -> path.getName(2).toString().equals("fonts")));
assetTypeManager.registerCoreAssetType(Shader.class, (AssetFactory<Shader, ShaderData>) HeadlessShader::new, "shaders");
assetTypeManager.registerCoreAssetType(Material.class, (AssetFactory<Material, MaterialData>) HeadlessMaterial::new, "materials");
assetTypeManager.registerCoreAssetType(Mesh.class, (AssetFactory<Mesh, MeshData>) HeadlessMesh::new, "mesh");
assetTypeManager.registerCoreAssetType(SkeletalMesh.class, (AssetFactory<SkeletalMesh, SkeletalMeshData>) HeadlessSkeletalMesh::new, "skeletalMesh");
assetTypeManager.registerCoreAssetType(MeshAnimation.class, (AssetFactory<MeshAnimation, MeshAnimationData>) MeshAnimationImpl::new, "animations");
assetTypeManager.registerCoreAssetType(Atlas.class, (AssetFactory<Atlas, AtlasData>) Atlas::new, "atlas");
assetTypeManager.registerCoreAssetType(Subtexture.class, (AssetFactory<Subtexture, SubtextureData>) Subtexture::new);
assetTypeManager.switchEnvironment(context.get(ModuleManager.class).getEnvironment());
context.put(ModuleAwareAssetTypeManager.class, assetTypeManager);
context.put(AssetManager.class, assetTypeManager.getAssetManager());
return assetTypeManager.getAssetManager();
}
Aggregations