@Override
 public BlockUri resolveBlockFamilyUri(String uri) {
   List<BlockUri> matches = resolveAllBlockFamilyUri(uri);
   switch (matches.size()) {
     case 0:
       logger.warn("Failed to resolve block family '{}'", uri);
       return null;
     case 1:
       return matches.get(0);
     default:
       Module context = ModuleContext.getContext();
       if (context != null) {
         Set<String> dependencies = moduleManager.getDependencyNamesOf(context);
         Iterator<BlockUri> iterator = matches.iterator();
         while (iterator.hasNext()) {
           BlockUri possibleUri = iterator.next();
           if (context.getId().equals(possibleUri.getNormalisedModuleName())) {
             return possibleUri;
           }
           if (!dependencies.contains(possibleUri.getNormalisedModuleName())) {
             iterator.remove();
           }
         }
         if (matches.size() == 1) {
           return matches.get(0);
         }
       }
       logger.warn(
           "Failed to resolve block family '{}' - too many valid matches {}", uri, matches);
       return null;
   }
 }
 @Override
 public boolean hasBlockFamily(BlockUri uri) {
   if (registeredBlockInfo.get().registeredFamilyByUri.containsKey(uri)
       || availableFamilies.containsKey(uri)
       || freeformBlockUris.contains(uri)) {
     return true;
   }
   return uri.hasShape()
       && Assets.get(uri.getShapeUri()) != null
       && freeformBlockUris.contains(uri.getRootFamilyUri());
 }
Beispiel #3
0
  public BlockFamily loadWithShape(BlockUri uri) {
    try (ModuleContext.ContextSpan ignored = ModuleContext.setContext(uri.getModuleName())) {
      BlockShape shape = cubeShape;
      if (uri.hasShape()) {
        AssetUri shapeUri = uri.getShapeUri();
        if (!shapeUri.isValid()) {
          return null;
        }
        shape = (BlockShape) Assets.get(shapeUri);
        if (shape == null) {
          return null;
        }
      }
      AssetUri blockDefUri =
          new AssetUri(AssetType.BLOCK_DEFINITION, uri.getModuleName(), uri.getFamilyName());
      BlockDefinition def;
      if (assetManager.getAssetURLs(blockDefUri).isEmpty()) {
        // An auto-block
        def = new BlockDefinition();
      } else {
        def =
            createBlockDefinition(
                inheritData(blockDefUri, readJson(blockDefUri).getAsJsonObject()));
      }

      def.shape = (shape.getURI().toSimpleString());
      if (shape.isCollisionYawSymmetric()) {
        Block block = constructSingleBlock(blockDefUri, def);
        if (block.getDisplayName().isEmpty()) {
          block.setDisplayName(shape.getDisplayName());
        } else if (!shape.getDisplayName().isEmpty()) {
          block.setDisplayName(block.getDisplayName() + " " + shape.getDisplayName());
        }
        return new SymmetricFamily(uri, block, def.categories);
      } else {
        Map<Side, Block> blockMap = Maps.newEnumMap(Side.class);
        constructHorizontalBlocks(blockDefUri, def, blockMap);
        for (Block block : blockMap.values()) {
          if (block.getDisplayName().isEmpty()) {
            block.setDisplayName(shape.getDisplayName());
          } else if (!shape.getDisplayName().isEmpty()) {
            block.setDisplayName(block.getDisplayName() + " " + shape.getDisplayName());
          }
        }
        return new HorizontalBlockFamily(uri, blockMap, def.categories);
      }
    } catch (Exception e) {
      logger.error("Error loading block shape {}", uri, e);
    }
    return null;
  }
 @Override
 public BlockFamily getBlockFamily(BlockUri uri) {
   BlockFamily family = registeredBlockInfo.get().registeredFamilyByUri.get(uri);
   if (family == null && generateNewIds) {
     if (isFreeformFamily(uri.getRootFamilyUri())) {
       family = blockLoader.loadWithShape(uri);
     } else {
       family = getAvailableBlockFamily(uri);
     }
     if (family != null) {
       lock.lock();
       try {
         for (Block block : family.getBlocks()) {
           block.setId(getNextId());
         }
         registerFamily(family);
       } finally {
         lock.unlock();
       }
     } else {
       logger.warn("Unable to resolve block family {}", uri);
     }
   }
   return family;
 }
 /**
  * Retrieve all {@code BlockUri}s that match the given string.
  *
  * <p>In order to resolve the {@code BlockUri}s, every package is searched for the given uri
  * pattern.
  *
  * @param uri the uri pattern to match
  * @return a list of matching block uris
  */
 @Override
 public List<BlockUri> resolveAllBlockFamilyUri(String uri) {
   List<BlockUri> matches = Lists.newArrayList();
   BlockUri straightUri = new BlockUri(uri);
   if (straightUri.isValid()) {
     if (hasBlockFamily(straightUri)) {
       matches.add(straightUri);
     }
   } else {
     for (String packageName : Assets.listModules()) {
       BlockUri modUri = new BlockUri(packageName, uri);
       if (hasBlockFamily(modUri)) {
         matches.add(modUri);
       }
     }
   }
   return matches;
 }
 @Override
 public Block getBlock(BlockUri uri) {
   Block block = registeredBlockInfo.get().blocksByUri.get(uri);
   if (block == null) {
     // Check if partially registered by getting the block family
     BlockFamily family = getBlockFamily(uri.getFamilyUri());
     if (family != null) {
       block = family.getBlockFor(uri);
     }
     if (block == null) {
       return getAir();
     }
   }
   return block;
 }
 @Override
 public boolean isFreeformFamily(BlockUri familyUri) {
   return freeformBlockUris.contains(familyUri.getRootFamilyUri());
 }