A slightly better way of loading the book model texture

This commit is contained in:
Electroblob77
2020-05-07 23:22:52 +01:00
parent a31b1c79e3
commit cf38a16f49
2 changed files with 39 additions and 6 deletions
@@ -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<ResourceLocation> 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<ResourceLocation> getDependencies(){
return bookModelLocations;
}
@Override
public Collection<ResourceLocation> 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);
}
}
@@ -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"));
}
}