Search in sources :

Example 1 with MalformedMaterialLibraryException

use of com.almuradev.content.model.obj.material.MalformedMaterialLibraryException in project Almura by AlmuraDev.

the class OBJModelParser method parseMaterialLibrary.

private MaterialLibrary parseMaterialLibrary(final ResourceLocation source) throws Exception {
    final MaterialLibrary.Builder mtllibBuilder = MaterialLibrary.builder();
    final IResource resource = this.resourceManager.getResource(source);
    try (final InputStream stream = resource.getInputStream()) {
        MaterialDefinition.Builder mtlBuilder = null;
        String currentMaterial = null;
        final List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
        for (int i = 0; i < lines.size(); i++) {
            final String line = lines.get(i);
            // Skip comments and newlines
            if (line.startsWith(OBJModelConfig.COMMENT) || line.isEmpty()) {
                continue;
            }
            final String[] combinedLineContents = line.split(" ");
            final String lineHeader = combinedLineContents[0];
            final String[] lineContents = Arrays.copyOfRange(combinedLineContents, 1, combinedLineContents.length);
            switch(lineHeader) {
                case OBJModelConfig.Material.NEW_MATERIAL:
                    if (mtlBuilder != null) {
                        mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
                    }
                    mtlBuilder = MaterialDefinition.builder();
                    currentMaterial = lineContents[0];
                    break;
                case OBJModelConfig.Material.DIFFUSE:
                    if (mtlBuilder == null) {
                        throw new MalformedMaterialLibraryException("Material attribute cannot occur before defining new material " + "definition! Source -> Line: " + (i + 1) + ", Content: " + Arrays.toString(combinedLineContents));
                    }
                    @Nullable final ResourceLocation parentLocation = getParent(this.source);
                    String parentPath = null;
                    if (parentLocation != null) {
                        // Making some assumptions here...carry on now
                        final int lastSlashIndex = parentLocation.getResourcePath().lastIndexOf("/");
                        if (lastSlashIndex != -1) {
                            parentPath = parentLocation.getResourcePath().substring(lastSlashIndex + 1, parentLocation.getResourcePath().length());
                        }
                    }
                    mtlBuilder.diffuseTexture(ResourceLocations.buildResourceLocationPath(lineContents[0], parentPath));
                    break;
                default:
            }
        }
        if (mtlBuilder != null) {
            mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
        }
    }
    return mtllibBuilder.build(source, getFileName(source).split("\\.")[0]);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MaterialDefinition(com.almuradev.content.model.obj.material.MaterialDefinition) MaterialLibrary(com.almuradev.content.model.obj.material.MaterialLibrary) ResourceLocation(net.minecraft.util.ResourceLocation) BufferedReader(java.io.BufferedReader) MalformedMaterialLibraryException(com.almuradev.content.model.obj.material.MalformedMaterialLibraryException) IResource(net.minecraft.client.resources.IResource) Nullable(javax.annotation.Nullable)

Aggregations

MalformedMaterialLibraryException (com.almuradev.content.model.obj.material.MalformedMaterialLibraryException)1 MaterialDefinition (com.almuradev.content.model.obj.material.MaterialDefinition)1 MaterialLibrary (com.almuradev.content.model.obj.material.MaterialLibrary)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Nullable (javax.annotation.Nullable)1 IResource (net.minecraft.client.resources.IResource)1 ResourceLocation (net.minecraft.util.ResourceLocation)1