From cf38a16f4908471c3a1e8d829ad3d1cc8d3fdce2 Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Thu, 7 May 2020 23:22:52 +0100 Subject: [PATCH] A slightly better way of loading the book model texture --- .../wizardry/client/model/ModelBookshelf.java | 40 ++++++++++++++++++- .../wizardry/client/model/WizardryModels.java | 5 --- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/main/java/electroblob/wizardry/client/model/ModelBookshelf.java b/src/main/java/electroblob/wizardry/client/model/ModelBookshelf.java index 38303c5c..107ea85d 100644 --- a/src/main/java/electroblob/wizardry/client/model/ModelBookshelf.java +++ b/src/main/java/electroblob/wizardry/client/model/ModelBookshelf.java @@ -1,5 +1,6 @@ package electroblob.wizardry.client.model; +import com.google.common.collect.ImmutableList; import electroblob.wizardry.Wizardry; import electroblob.wizardry.block.BlockBookshelf; import net.minecraft.client.renderer.block.model.IBakedModel; @@ -11,13 +12,26 @@ import net.minecraftforge.client.model.IModel; import net.minecraftforge.client.model.ModelLoaderRegistry; import net.minecraftforge.common.model.IModelState; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.function.Function; public class ModelBookshelf implements IModel { + private static final ResourceLocation BOOK_TEXTURE = new ResourceLocation(Wizardry.MODID, "blocks/books"); + + private static final List bookModelLocations = new ArrayList<>(); + private final ModelResourceLocation bookshelfModelLocation; private final String variant; + static { + for(int i = 0; i < BlockBookshelf.SLOT_COUNT; i++){ + bookModelLocations.add(new ResourceLocation(Wizardry.MODID, "bookshelf_parts/books" + i)); + } + } + public ModelBookshelf(String bookshelfModelName){ String[] args = bookshelfModelName.split("#", 2); String model = args[0]; @@ -37,10 +51,12 @@ public class ModelBookshelf implements IModel { IBakedModel[] books = new IBakedModel[BlockBookshelf.SLOT_COUNT]; for(int i = 0; i < BlockBookshelf.SLOT_COUNT; i++){ - IModel bookModel = ModelLoaderRegistry.getModel(new ModelResourceLocation(new ResourceLocation(Wizardry.MODID, "bookshelf_parts/books" + i), variant)); + IModel bookModel = ModelLoaderRegistry.getModel(new ModelResourceLocation(bookModelLocations.get(i), variant)); books[i] = bookModel.bake(bookModel.getDefaultState(), format, bakedTextureGetter); // Same here! } + // To clarify: the whole point of doing this was to avoid having to bake 4 * 2^12 models per bookshelf type + // Therefore we have to have a baked bookshelf model that's kind of dynamic return new BakedModelBookshelf(bookshelf, books); }catch(Exception exception){ @@ -50,4 +66,26 @@ public class ModelBookshelf implements IModel { } + @Override + public Collection getDependencies(){ + return bookModelLocations; + } + + @Override + public Collection getTextures(){ + + // I simply have no idea how I'm *supposed* to do it, so I'm using the best solution I can come up with + // This is at least better than manually registering it with TextureStitchEvent + // However I'm also pretty sure that if a texture pack decides to change the texture that the book models point + // to, it won't work... but since nobody ever will, I don't really care! + + // Why am I confused? + // 1. How come it magically finds the bookshelf texture, but not the book texture? + // 2. Why don't the textures get retrieved automatically based on getDependencies? Surely that's the point? + // 3. Is it correct to use getModel() to get the dependency models upon construction of this class so I can + // access their textures properly? Or is that too early? + // 4. If so, what's the point of getDependencies, and why should I override it? + return ImmutableList.of(BOOK_TEXTURE); + } + } diff --git a/src/main/java/electroblob/wizardry/client/model/WizardryModels.java b/src/main/java/electroblob/wizardry/client/model/WizardryModels.java index e03e22d5..eae2ecc9 100644 --- a/src/main/java/electroblob/wizardry/client/model/WizardryModels.java +++ b/src/main/java/electroblob/wizardry/client/model/WizardryModels.java @@ -378,9 +378,4 @@ public final class WizardryModels { new ModelResourceLocation(item.getRegistryName(), variant)); } - @SubscribeEvent - public static void onTextureStitchEvent(TextureStitchEvent.Pre event){ - event.getMap().registerSprite(new ResourceLocation(Wizardry.MODID, "blocks/books")); - } - }