diff --git a/src/main/java/net/minecraftforge/client/model/obj/MaterialLibrary.java b/src/main/java/net/minecraftforge/client/model/obj/MaterialLibrary.java index 57ad9656c..368a8989c 100644 --- a/src/main/java/net/minecraftforge/client/model/obj/MaterialLibrary.java +++ b/src/main/java/net/minecraftforge/client/model/obj/MaterialLibrary.java @@ -26,11 +26,17 @@ import net.minecraft.client.renderer.Vector4f; import java.io.IOException; import java.util.Arrays; import java.util.Map; +import java.util.NoSuchElementException; public class MaterialLibrary { + public static final MaterialLibrary EMPTY = new MaterialLibrary(); final Map materials = Maps.newHashMap(); + private MaterialLibrary() + { + } + public MaterialLibrary(OBJLoader.LineReader reader) throws IOException { Material currentMaterial = null; @@ -97,10 +103,12 @@ public class MaterialLibrary public Material getMaterial(String mat) { + if (!materials.containsKey(mat)) + throw new NoSuchElementException("The material was not found in the library: " + mat); return materials.get(mat); } - public class Material + public static class Material { public final String name; public Vector4f ambientColor = new Vector4f(0,0,0,1); diff --git a/src/main/java/net/minecraftforge/client/model/obj/OBJLoader.java b/src/main/java/net/minecraftforge/client/model/obj/OBJLoader.java index 9372333c6..7fe7e60f7 100644 --- a/src/main/java/net/minecraftforge/client/model/obj/OBJLoader.java +++ b/src/main/java/net/minecraftforge/client/model/obj/OBJLoader.java @@ -67,17 +67,19 @@ public class OBJLoader implements IModelLoader boolean diffuseLighting = JSONUtils.getBoolean(modelContents, "diffuseLighting", false); boolean flipV = JSONUtils.getBoolean(modelContents, "flip-v", false); boolean ambientToFullbright = JSONUtils.getBoolean(modelContents, "ambientToFullbright", true); + @Nullable + String materialLibraryOverrideLocation = modelContents.has("materialLibraryOverride") ? JSONUtils.getString(modelContents, "materialLibraryOverride") : null; - return loadModel(new ResourceLocation(modelLocation), detectCullableFaces, diffuseLighting, flipV, ambientToFullbright); + return loadModel(new ModelSettings(new ResourceLocation(modelLocation), detectCullableFaces, diffuseLighting, flipV, ambientToFullbright, materialLibraryOverrideLocation)); } - public OBJModel loadModel(ResourceLocation modelLocation, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright) + public OBJModel loadModel(ModelSettings settings) { - return modelCache.computeIfAbsent(new ModelSettings(modelLocation, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright), (data) -> { + return modelCache.computeIfAbsent(settings, (data) -> { IResource resource; try { - resource = manager.getResource(modelLocation); + resource = manager.getResource(settings.modelLocation); } catch (IOException e) { @@ -86,7 +88,7 @@ public class OBJLoader implements IModelLoader try(LineReader rdr = new LineReader(resource)) { - return new OBJModel(modelLocation, rdr, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright); + return new OBJModel(rdr, settings); } catch (Exception e) { @@ -133,6 +135,7 @@ public class OBJLoader implements IModelLoader @Nullable public String[] readAndSplitLine(boolean ignoreEmptyLines) throws IOException { + //noinspection LoopConditionNotUpdatedInsideLoop do { String currentLine = lineReader.readLine(); @@ -170,7 +173,7 @@ public class OBJLoader implements IModelLoader if (lineParts.size() > 0) return lineParts.toArray(new String[0]); } - while (ignoreEmptyLines); // conditional expression is not updated, yes, I know. + while (ignoreEmptyLines); return new String[0]; } @@ -183,22 +186,26 @@ public class OBJLoader implements IModelLoader } } - private class ModelSettings + public static class ModelSettings { @Nonnull - private final ResourceLocation modelLocation; - private final boolean detectCullableFaces; - private final boolean diffuseLighting; - private final boolean flipV; - private final boolean ambientToFullbright; + public final ResourceLocation modelLocation; + public final boolean detectCullableFaces; + public final boolean diffuseLighting; + public final boolean flipV; + public final boolean ambientToFullbright; + @Nullable + public final String materialLibraryOverrideLocation; - public ModelSettings(@Nonnull ResourceLocation modelLocation, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright) + public ModelSettings(@Nonnull ResourceLocation modelLocation, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright, + @Nullable String materialLibraryOverrideLocation) { this.modelLocation = modelLocation; this.detectCullableFaces = detectCullableFaces; this.diffuseLighting = diffuseLighting; this.flipV = flipV; this.ambientToFullbright = ambientToFullbright; + this.materialLibraryOverrideLocation = materialLibraryOverrideLocation; } @Override @@ -216,13 +223,14 @@ public class OBJLoader implements IModelLoader diffuseLighting == that.diffuseLighting && flipV == that.flipV && ambientToFullbright == that.ambientToFullbright && - modelLocation.equals(that.modelLocation); + modelLocation.equals(that.modelLocation) && + Objects.equals(materialLibraryOverrideLocation, that.materialLibraryOverrideLocation); } @Override public int hashCode() { - return Objects.hash(modelLocation, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright); + return Objects.hash(modelLocation, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright, materialLibraryOverrideLocation); } } } diff --git a/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java b/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java index ab6fc18a6..a43e1a471 100644 --- a/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java +++ b/src/main/java/net/minecraftforge/client/model/obj/OBJModel.java @@ -73,13 +73,18 @@ public class OBJModel implements IMultipartModelGeometry public final ResourceLocation modelLocation; - OBJModel(ResourceLocation modelLocation, OBJLoader.LineReader reader, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright) throws IOException + @Nullable + public final String materialLibraryOverrideLocation; + + + OBJModel(OBJLoader.LineReader reader, OBJLoader.ModelSettings settings) throws IOException { - this.modelLocation = modelLocation; - this.detectCullableFaces = detectCullableFaces; - this.diffuseLighting = diffuseLighting; - this.flipV = flipV; - this.ambientToFullbright = ambientToFullbright; + this.modelLocation = settings.modelLocation; + this.detectCullableFaces = settings.detectCullableFaces; + this.diffuseLighting = settings.diffuseLighting; + this.flipV = settings.flipV; + this.ambientToFullbright = settings.ambientToFullbright; + this.materialLibraryOverrideLocation = settings.materialLibraryOverrideLocation; // for relative references to material libraries String modelDomain = modelLocation.getNamespace(); @@ -90,7 +95,7 @@ public class OBJModel implements IMultipartModelGeometry else modelPath = ""; - MaterialLibrary mtllib = null; + MaterialLibrary mtllib = MaterialLibrary.EMPTY; MaterialLibrary.Material currentMat = null; String currentSmoothingGroup = null; ModelGroup currentGroup = null; @@ -99,6 +104,15 @@ public class OBJModel implements IMultipartModelGeometry boolean objAboveGroup = false; + if (materialLibraryOverrideLocation != null) + { + String lib = materialLibraryOverrideLocation; + if (lib.contains(":")) + mtllib = OBJLoader.INSTANCE.loadMaterialLibrary(new ResourceLocation(lib)); + else + mtllib = OBJLoader.INSTANCE.loadMaterialLibrary(new ResourceLocation(modelDomain, modelPath + lib)); + } + String[] line; while((line = reader.readAndSplitLine(true)) != null) { @@ -106,6 +120,9 @@ public class OBJModel implements IMultipartModelGeometry { case "mtllib": // Loads material library { + if (materialLibraryOverrideLocation != null) + break; + String lib = line[1]; if (lib.contains(":")) mtllib = OBJLoader.INSTANCE.loadMaterialLibrary(new ResourceLocation(lib)); diff --git a/src/test/java/net/minecraftforge/debug/client/model/TRSRTransformerTest.java b/src/test/java/net/minecraftforge/debug/client/model/TRSRTransformerTest.java index 314304215..55b1b5cb4 100644 --- a/src/test/java/net/minecraftforge/debug/client/model/TRSRTransformerTest.java +++ b/src/test/java/net/minecraftforge/debug/client/model/TRSRTransformerTest.java @@ -1,3 +1,22 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + package net.minecraftforge.debug.client.model; import com.google.common.collect.ImmutableList; diff --git a/src/test/resources/assets/new_model_loader_test/models/item/obj_item.json b/src/test/resources/assets/new_model_loader_test/models/item/obj_block.json similarity index 100% rename from src/test/resources/assets/new_model_loader_test/models/item/obj_item.json rename to src/test/resources/assets/new_model_loader_test/models/item/obj_block.json