mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-22 21:10:00 +00:00
Add fallback model loader
Allows loading the first model from a list with a matching mod ID
This commit is contained in:
1 parent
6c0fb541b8
commit
e658d4b5f9
3 files changed
+105
-15
No files matched your search
@@ -13,6 +13,7 @@ import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import slimeknights.mantle.Mantle;
|
||||
import slimeknights.mantle.client.book.BookLoader;
|
||||
import slimeknights.mantle.client.model.FallbackModelLoader;
|
||||
import slimeknights.mantle.client.model.connected.ConnectedModel;
|
||||
import slimeknights.mantle.client.model.inventory.InventoryModel;
|
||||
|
||||
@@ -30,6 +31,7 @@ public class ClientEvents {
|
||||
|
||||
@SubscribeEvent
|
||||
static void registerModelLoaders(ModelRegistryEvent event) {
|
||||
ModelLoaderRegistry.registerLoader(Mantle.getResource("fallback"), FallbackModelLoader.INSTANCE);
|
||||
ModelLoaderRegistry.registerLoader(Mantle.getResource("inventory"), InventoryModel.Loader.INSTANCE);
|
||||
ModelLoaderRegistry.registerLoader(Mantle.getResource("connected"), ConnectedModel.Loader.INSTANCE);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package slimeknights.mantle.client.model;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.client.renderer.model.BlockModel;
|
||||
import net.minecraft.client.renderer.model.IBakedModel;
|
||||
import net.minecraft.client.renderer.model.IModelTransform;
|
||||
import net.minecraft.client.renderer.model.IUnbakedModel;
|
||||
import net.minecraft.client.renderer.model.ItemOverrideList;
|
||||
import net.minecraft.client.renderer.model.ModelBakery;
|
||||
import net.minecraft.client.renderer.model.RenderMaterial;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.resources.IResourceManager;
|
||||
import net.minecraft.util.JSONUtils;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.model.IModelConfiguration;
|
||||
import net.minecraftforge.client.model.IModelLoader;
|
||||
import net.minecraftforge.client.model.geometry.IModelGeometry;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class FallbackModelLoader implements IModelLoader<FallbackModelLoader.BlockModelWrapper> {
|
||||
/** Loader instance */
|
||||
public static final FallbackModelLoader INSTANCE = new FallbackModelLoader();
|
||||
|
||||
@Override
|
||||
public void onResourceManagerReload(IResourceManager resourceManager) {}
|
||||
|
||||
@Override
|
||||
public BlockModelWrapper read(JsonDeserializationContext context, JsonObject data) {
|
||||
JsonArray models = JSONUtils.getJsonArray(data, "models");
|
||||
if (models.size() < 2) {
|
||||
throw new JsonSyntaxException("Fallback model must contain at least 2 models");
|
||||
}
|
||||
|
||||
// try loading each model
|
||||
for (int i = 0; i < models.size(); i++) {
|
||||
String debugName = "models[" + i + "]";
|
||||
JsonObject entry = JSONUtils.getJsonObject(models.get(i), debugName);
|
||||
|
||||
// first, determine required mod ID
|
||||
String modId = null;
|
||||
if (entry.has("fallback_mod_id")) {
|
||||
modId = JSONUtils.getString(entry, "fallback_mod_id");
|
||||
} else if (entry.has("loader")) {
|
||||
ResourceLocation loader = new ResourceLocation(JSONUtils.getString(entry, "loader"));
|
||||
modId = loader.getNamespace();
|
||||
}
|
||||
|
||||
// if the mod is loaded, try loading the given model
|
||||
if (modId == null || ModList.get().isLoaded(modId)) {
|
||||
try {
|
||||
// use a model wrapper to ensure the child model gets the proper context
|
||||
// this means its not possible to extend the fallback model, but that is not normally possible with loaders
|
||||
return new BlockModelWrapper(context.deserialize(entry, BlockModel.class));
|
||||
} catch (JsonSyntaxException e) {
|
||||
// wrap exceptions to make it more clear what failed
|
||||
throw new JsonSyntaxException("Failed to parse fallback model " + debugName, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// no model was successful, sadness
|
||||
throw new JsonSyntaxException("Failed to load fallback model, all " + models.size() + " variants had a failed condition");
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around a single block model, redirects all standard calls to vanilla logic
|
||||
* Final baked model will still be the original instance, which is what is important
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
static class BlockModelWrapper implements IModelGeometry<BlockModelWrapper> {
|
||||
private final BlockModel model;
|
||||
|
||||
@Override
|
||||
public IBakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<RenderMaterial,TextureAtlasSprite> spriteGetter, IModelTransform modelTransform, ItemOverrideList overrides, ResourceLocation modelLocation) {
|
||||
return model.bakeModel(bakery, model, spriteGetter, modelTransform, modelLocation, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<RenderMaterial> getTextures(IModelConfiguration owner, Function<ResourceLocation,IUnbakedModel> modelGetter, Set<Pair<String,String>> missingTextureErrors) {
|
||||
return model.getTextures(modelGetter, missingTextureErrors);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,11 @@ public class ConnectedModel implements IModelGeometry<ConnectedModel> {
|
||||
for (Entry<String,String[]> entry : connectedTextures.entrySet()) {
|
||||
// fetch data from the base texture
|
||||
String name = entry.getKey();
|
||||
RenderMaterial base = owner.resolveTexture(name);
|
||||
// skip if missing
|
||||
if (!model.isTexturePresent(name)) {
|
||||
continue;
|
||||
}
|
||||
RenderMaterial base = model.resolveTextureName(name);
|
||||
ResourceLocation atlas = base.getAtlasLocation();
|
||||
ResourceLocation texture = base.getTextureLocation();
|
||||
String namespace = texture.getNamespace();
|
||||
@@ -108,8 +112,8 @@ public class ConnectedModel implements IModelGeometry<ConnectedModel> {
|
||||
if (!suffixedTextures.containsKey(suffixedName)) {
|
||||
RenderMaterial mat;
|
||||
// allow overriding a specific texture
|
||||
if (owner.isTexturePresent(suffixedName)) {
|
||||
mat = owner.resolveTexture(suffixedName);
|
||||
if (model.isTexturePresent(suffixedName)) {
|
||||
mat = model.resolveTextureName(suffixedName);
|
||||
} else {
|
||||
mat = new RenderMaterial(atlas, new ResourceLocation(namespace, path + "/" + suffixes[i]));
|
||||
}
|
||||
@@ -371,13 +375,6 @@ public class ConnectedModel implements IModelGeometry<ConnectedModel> {
|
||||
TransformationMatrix rotation = transforms.getRotation();
|
||||
data.setData(CONNECTIONS, getConnections((dir) -> parent.sides.contains(dir) && parent.connectionPredicate.test(state, world.getBlockState(pos.offset(rotation.rotateTransform(dir))))));
|
||||
|
||||
// set connected properties
|
||||
// for (Direction dir : Direction.values()) {
|
||||
// if (parent.sides.contains(dir)) {
|
||||
// data.setData(CONNECTED_PROPERTIES.get(dir), parent.connectionPredicate.test(state, world.getBlockState(pos.offset(rotation.rotateTransform(dir)))));
|
||||
// }
|
||||
// }
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -460,12 +457,9 @@ public class ConnectedModel implements IModelGeometry<ConnectedModel> {
|
||||
// build texture list
|
||||
ImmutableMap.Builder<String,String[]> connectedTextures = new ImmutableMap.Builder<>();
|
||||
for (Entry<String,JsonElement> entry : connected.entrySet()) {
|
||||
// texture must be in the model
|
||||
String name = entry.getKey();
|
||||
if(!model.textures.containsKey(name)) {
|
||||
throw new JsonSyntaxException("Invalid connected texture " + name + ", missing in model");
|
||||
}
|
||||
// don't validate texture as it may be contained in a child model that is not yet loaded
|
||||
// get type, put in map
|
||||
String name = entry.getKey();
|
||||
connectedTextures.put(name, ConnectedModelRegistry.deserializeType(entry.getValue(), "textures[" + name + "]"));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user