From 319bab35a0bb2edc3d6f37c06714d148fb15d014 Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Thu, 24 Dec 2015 02:14:21 -0600 Subject: [PATCH] Lay all groundwork for special flower models. Not yet tested. --- .../api/render/SpecialFlowerModel.java | 257 ++++++++++++++++++ .../botania/api/state/BotaniaStateProps.java | 4 + .../botania/api/state/PropertyClass.java | 35 +++ .../client/core/handler/ModelHandler.java | 3 +- .../client/core/proxy/ClientProxy.java | 3 + .../common/block/BlockSpecialFlower.java | 45 ++- .../botania/blockstates/specialFlower.json | 14 + 7 files changed, 352 insertions(+), 9 deletions(-) create mode 100644 src/main/java/vazkii/botania/api/render/SpecialFlowerModel.java create mode 100644 src/main/java/vazkii/botania/api/state/PropertyClass.java create mode 100644 src/main/resources/assets/botania/blockstates/specialFlower.json diff --git a/src/main/java/vazkii/botania/api/render/SpecialFlowerModel.java b/src/main/java/vazkii/botania/api/render/SpecialFlowerModel.java new file mode 100644 index 000000000..5db3c3abe --- /dev/null +++ b/src/main/java/vazkii/botania/api/render/SpecialFlowerModel.java @@ -0,0 +1,257 @@ +package vazkii.botania.api.render; + +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.block.model.BakedQuad; +import net.minecraft.client.renderer.block.model.ItemCameraTransforms; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.client.renderer.vertex.VertexFormat; +import net.minecraft.client.resources.IResourceManager; +import net.minecraft.client.resources.model.IBakedModel; +import net.minecraft.client.resources.model.ModelManager; +import net.minecraft.client.resources.model.ModelResourceLocation; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.model.ICustomModelLoader; +import net.minecraftforge.client.model.IFlexibleBakedModel; +import net.minecraftforge.client.model.IModel; +import net.minecraftforge.client.model.IModelCustomData; +import net.minecraftforge.client.model.IModelState; +import net.minecraftforge.client.model.IPerspectiveAwareModel; +import net.minecraftforge.client.model.ISmartBlockModel; +import net.minecraftforge.client.model.TRSRTransformation; +import net.minecraftforge.common.property.IExtendedBlockState; +import net.minecraftforge.fml.common.FMLLog; +import org.apache.commons.lang3.tuple.Pair; +import vazkii.botania.api.state.BotaniaStateProps; +import vazkii.botania.api.subtile.SubTileEntity; +import vazkii.botania.common.block.BlockSpecialFlower; + +import javax.vecmath.Matrix4f; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +public class SpecialFlowerModel implements IModelCustomData { + + /** + * Internal implementation + **/ + // SpecialFlowerModel for when there are no models registered for a subtile + public static final SpecialFlowerModel INSTANCE = new SpecialFlowerModel(ImmutableMap.>, ModelResourceLocation>of()); + // Models registered from the externel API thus far + private static final Map, ModelResourceLocation> queuedModels = Maps.newHashMap(); + private final ImmutableMap>, ModelResourceLocation> models; + + + public SpecialFlowerModel(ImmutableMap>, ModelResourceLocation> models) { + this.models = models; + } + + /** + * Register your model for the given subtile class here. + * Call this DURING PREINIT. Calling it anytime after models have already baked does not guarantee that your model will work. + * Your model json must specify key "tintindex" in all the faces it wants tint applied. + * Tint is applied whenever a player recolors the flower using floral dye + * todo 1.8.8 move this elsewhere or otherwise make it fit better with the rest of the subtile registration process + * + * @param subTileClass + * @param model + */ + public static void register(Class subTileClass, ModelResourceLocation model) { + queuedModels.put(subTileClass, model); + } + + @Override + public Collection getDependencies() { + return ImmutableList.copyOf(models.values()); + } + + @Override + public Collection getTextures() { + return ImmutableList.of(); + } + + @Override + public IFlexibleBakedModel bake(IModelState state, VertexFormat format, Function bakedTextureGetter) { + // "Bake" the SpecialFlowerModel (in reality, just create another wrapper for all the delegate models) + return new SpecialFlowerBakedModel(models, format, IPerspectiveAwareModel.MapWrapper.getTransforms(state)); + } + + @Override + public IModelState getDefaultState() { + return TRSRTransformation.identity(); + } + + @Override + public IModel process(ImmutableMap customData) { + // Load the base variant from blockstate json, and also add all the model paths we received from external API + ImmutableMap.Builder>, ModelResourceLocation> builder = ImmutableMap.builder(); + for (String key : customData.keySet()) { + if ("base".equals(key)) { + builder.put(Optional.>absent(), getLocation(customData.get(key))); + } + for (Map.Entry, ModelResourceLocation> e : queuedModels.entrySet()) { + builder.put(Optional.>of(e.getKey()), e.getValue()); + } + } + ImmutableMap>, ModelResourceLocation> models = builder.build(); + if (models.isEmpty()) return INSTANCE; + return new SpecialFlowerModel(models); + } + + private ModelResourceLocation getLocation(String json) { + JsonElement e = new JsonParser().parse(json); + if (e.isJsonPrimitive() && e.getAsJsonPrimitive().isString()) { + return new ModelResourceLocation(e.getAsString()); + } + FMLLog.severe("Expect ModelResourceLocation, got: ", json); + return new ModelResourceLocation("builtin/missing", "missing"); + } + + public enum Loader implements ICustomModelLoader { + INSTANCE { + @Override + public void onResourceManagerReload(IResourceManager resourceManager) { + } + + @Override + public boolean accepts(ResourceLocation modelLocation) { + return "botania_SPECIAL".equals(modelLocation.getResourceDomain()) + && "specialFlower".equals(modelLocation.getResourcePath()); + } + + @Override + public IModel loadModel(ResourceLocation modelLocation) throws IOException { + // Load a dummy model for now, all actual models added in process(). + return SpecialFlowerModel.INSTANCE; + } + } + } + + public static class SpecialFlowerBakedModel implements IPerspectiveAwareModel, ISmartBlockModel { + + private final ImmutableMap transforms; + private final ImmutableMap>, ModelResourceLocation> models; + private final VertexFormat vertexFormat; + + private IBakedModel baseModel; + private ImmutableMap, IBakedModel> bakedFlowerModels; + private ImmutableMap, ImmutableList> quads; + + public SpecialFlowerBakedModel(ImmutableMap>, ModelResourceLocation> models, VertexFormat format, ImmutableMap cameraTransforms) { + this.models = models; + this.vertexFormat = format; + this.transforms = cameraTransforms; + } + + private void refreshBakedModels() { + if (baseModel == null) { + ModelManager manager = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelManager(); + baseModel = getModel(manager, Optional.>absent()); + + ImmutableMap.Builder, IBakedModel> builder = ImmutableMap.builder(); + for (Map.Entry>, ModelResourceLocation> e : models.entrySet()) { + if (e.getKey().isPresent()) { + builder.put(e.getKey().get(), getModel(manager, e.getKey())); + } + } + + bakedFlowerModels = builder.build(); + + ImmutableMap.Builder, ImmutableList> quadBuilder = ImmutableMap.builder(); + quadBuilder.put(Optional.absent(), buildQuads(Optional.absent())); + for (EnumFacing side : EnumFacing.values()) { + quadBuilder.put(Optional.of(side), buildQuads(Optional.of(side))); + } + quads = quadBuilder.build(); + } + } + + private IBakedModel getModel(ModelManager manager, Optional> layer) { + ModelResourceLocation loc = models.get(layer); + if (loc == null) { + loc = new ModelResourceLocation("builtin/missing", "missing"); + } + return manager.getModel(loc); + } + + private ImmutableList buildQuads(Optional side) { + ImmutableList.Builder builder = ImmutableList.builder(); + for (IBakedModel model : bakedFlowerModels.values()) { + if (side.isPresent()) { + builder.addAll(model.getFaceQuads(side.get())); + } else { + builder.addAll(model.getGeneralQuads()); + } + } + return builder.build(); + } + + @Override + public List getFaceQuads(EnumFacing side) { + refreshBakedModels(); + return quads.get(Optional.of(side)); + } + + @Override + public List getGeneralQuads() { + refreshBakedModels(); + return quads.get(Optional.absent()); + } + + @Override + public boolean isAmbientOcclusion() { + refreshBakedModels(); + return baseModel.isAmbientOcclusion(); + } + + @Override + public boolean isGui3d() { + refreshBakedModels(); + return baseModel.isGui3d(); + } + + @Override + public boolean isBuiltInRenderer() { + refreshBakedModels(); + return baseModel.isBuiltInRenderer(); + } + + @Override + public TextureAtlasSprite getParticleTexture() { + refreshBakedModels(); + return baseModel.getParticleTexture(); + } + + @Override + public ItemCameraTransforms getItemCameraTransforms() { + return ItemCameraTransforms.DEFAULT; + } + + @Override + public IBakedModel handleBlockState(IBlockState state) { + IExtendedBlockState extendedState = ((IExtendedBlockState) state); + Class clazz = extendedState.getValue(BotaniaStateProps.SUBTILE_CLASS); + return bakedFlowerModels.get(clazz); + } + + @Override + public VertexFormat getFormat() { + return vertexFormat; + } + + @Override + public Pair handlePerspective(ItemCameraTransforms.TransformType cameraTransformType) { + return IPerspectiveAwareModel.MapWrapper.handlePerspective(this, transforms, cameraTransformType); + } + } +} diff --git a/src/main/java/vazkii/botania/api/state/BotaniaStateProps.java b/src/main/java/vazkii/botania/api/state/BotaniaStateProps.java index b28839111..499947994 100644 --- a/src/main/java/vazkii/botania/api/state/BotaniaStateProps.java +++ b/src/main/java/vazkii/botania/api/state/BotaniaStateProps.java @@ -17,6 +17,10 @@ import javax.annotation.Nullable; */ public class BotaniaStateProps { + /** The property for specialFlower **/ + + public static final PropertyClass SUBTILE_CLASS = new PropertyClass("subtile_class"); + /** Common properties to all blocks to use **/ // The 16 Minecraft colors diff --git a/src/main/java/vazkii/botania/api/state/PropertyClass.java b/src/main/java/vazkii/botania/api/state/PropertyClass.java new file mode 100644 index 000000000..bb9f863ae --- /dev/null +++ b/src/main/java/vazkii/botania/api/state/PropertyClass.java @@ -0,0 +1,35 @@ +package vazkii.botania.api.state; + +import net.minecraftforge.common.property.IUnlistedProperty; +import vazkii.botania.api.subtile.SubTileEntity; + +public class PropertyClass implements IUnlistedProperty> { + + private final String name; + + public PropertyClass(String name) { + this.name = name; + } + + @Override + public String getName() { + return name; + } + + @Override + public boolean isValid(Class value) { + return true; + } + + @Override + public Class> getType() { + // todo is there a safe way? + Class clazz = Class.class; + return clazz; + } + + @Override + public String valueToString(Class value) { + return value.getName(); + } +} diff --git a/src/main/java/vazkii/botania/client/core/handler/ModelHandler.java b/src/main/java/vazkii/botania/client/core/handler/ModelHandler.java index f2ca9e230..6f77d006d 100644 --- a/src/main/java/vazkii/botania/client/core/handler/ModelHandler.java +++ b/src/main/java/vazkii/botania/client/core/handler/ModelHandler.java @@ -129,7 +129,8 @@ public final class ModelHandler { // Ignore redstone power state in some blocks ModelLoader.setCustomStateMapper(ModBlocks.starfield, (new StateMap.Builder()).ignore(BotaniaStateProps.POWERED).build()); - // Ignore color in unstable cube, mana beacon, and petals (handled by color multiplier) + // Ignore color in special flower, unstable cube, mana beacon, and petals (handled by color multiplier) + ModelLoader.setCustomStateMapper(ModBlocks.specialFlower, (new StateMap.Builder()).ignore(BotaniaStateProps.COLOR).build()); ModelLoader.setCustomStateMapper(ModBlocks.unstableBlock, (new StateMap.Builder()).ignore(BotaniaStateProps.COLOR).build()); ModelLoader.setCustomStateMapper(ModBlocks.manaBeacon, (new StateMap.Builder()).ignore(BotaniaStateProps.COLOR).build()); ModelLoader.setCustomStateMapper(ModBlocks.petalBlock, (new StateMap.Builder()).ignore(BotaniaStateProps.COLOR).build()); diff --git a/src/main/java/vazkii/botania/client/core/proxy/ClientProxy.java b/src/main/java/vazkii/botania/client/core/proxy/ClientProxy.java index e451cfd93..13a9aaabd 100644 --- a/src/main/java/vazkii/botania/client/core/proxy/ClientProxy.java +++ b/src/main/java/vazkii/botania/client/core/proxy/ClientProxy.java @@ -33,6 +33,7 @@ import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; import net.minecraft.world.WorldSettings.GameType; import net.minecraftforge.client.MinecraftForgeClient; +import net.minecraftforge.client.model.ModelLoaderRegistry; import net.minecraftforge.common.MinecraftForge; import vazkii.botania.api.item.IExtendedPlayerController; import vazkii.botania.api.lexicon.LexiconEntry; @@ -40,6 +41,7 @@ import vazkii.botania.api.lexicon.multiblock.IMultiblockRenderHook; import vazkii.botania.api.lexicon.multiblock.Multiblock; import vazkii.botania.api.lexicon.multiblock.MultiblockSet; import vazkii.botania.api.lexicon.multiblock.component.AnyComponent; +import vazkii.botania.api.render.SpecialFlowerModel; import vazkii.botania.api.wiki.IWikiProvider; import vazkii.botania.api.wiki.WikiHooks; import vazkii.botania.client.challenge.ModChallenges; @@ -180,6 +182,7 @@ public class ClientProxy extends CommonProxy { super.preInit(event); + ModelLoaderRegistry.registerLoader(SpecialFlowerModel.Loader.INSTANCE); ModelHandler.registerModels(); } diff --git a/src/main/java/vazkii/botania/common/block/BlockSpecialFlower.java b/src/main/java/vazkii/botania/common/block/BlockSpecialFlower.java index 07b9dfc0b..f577bae6d 100644 --- a/src/main/java/vazkii/botania/common/block/BlockSpecialFlower.java +++ b/src/main/java/vazkii/botania/common/block/BlockSpecialFlower.java @@ -12,13 +12,12 @@ package vazkii.botania.common.block; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import net.minecraft.block.Block; import net.minecraft.block.BlockFlower; import net.minecraft.block.ITileEntityProvider; +import net.minecraft.block.properties.IProperty; import net.minecraft.block.state.BlockState; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; @@ -32,17 +31,23 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockPos; import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumWorldBlockLayer; import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import net.minecraftforge.common.property.ExtendedBlockState; +import net.minecraftforge.common.property.IExtendedBlockState; +import net.minecraftforge.common.property.IUnlistedProperty; import vazkii.botania.api.BotaniaAPI; import vazkii.botania.api.state.BotaniaStateProps; import vazkii.botania.api.lexicon.ILexiconable; import vazkii.botania.api.lexicon.LexiconEntry; +import vazkii.botania.api.state.PropertyClass; import vazkii.botania.api.subtile.ISpecialFlower; +import vazkii.botania.api.subtile.SubTileEntity; import vazkii.botania.api.wand.IWandHUD; import vazkii.botania.api.wand.IWandable; -import vazkii.botania.client.lib.LibRenderIDs; +import vazkii.botania.common.block.subtile.generating.SubTileDaybloom; import vazkii.botania.common.block.tile.TileSpecialFlower; import vazkii.botania.common.core.BotaniaCreativeTab; import vazkii.botania.common.integration.coloredlights.LightHelper; @@ -112,12 +117,26 @@ public class BlockSpecialFlower extends BlockFlower implements ITileEntityProvid setTickRandomly(false); setCreativeTab(BotaniaCreativeTab.INSTANCE); setBlockBounds(0.3F, 0.0F, 0.3F, 0.8F, 1, 0.8F); - setDefaultState(blockState.getBaseState().withProperty(BotaniaStateProps.COLOR, EnumDyeColor.WHITE).withProperty(type, EnumFlowerType.POPPY)); + setDefaultState(((IExtendedBlockState) blockState.getBaseState()) + .withProperty(BotaniaStateProps.SUBTILE_CLASS, SubTileDaybloom.class) + .withProperty(BotaniaStateProps.COLOR, EnumDyeColor.WHITE).withProperty(type, EnumFlowerType.POPPY) + ); } @Override public BlockState createBlockState() { - return new BlockState(this, getTypeProperty(), BotaniaStateProps.COLOR); + return new ExtendedBlockState(this, new IProperty[]{ getTypeProperty(), BotaniaStateProps.COLOR }, new IUnlistedProperty[] { BotaniaStateProps.SUBTILE_CLASS } ); + } + + @Override + public IExtendedBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos) { + TileEntity te = world.getTileEntity(pos); + if (te instanceof TileSpecialFlower) { + Class clazz = ((TileSpecialFlower) te).getSubTile().getClass(); + return ((IExtendedBlockState) state).withProperty(BotaniaStateProps.SUBTILE_CLASS, clazz); + } else { + return ((IExtendedBlockState) state); + } } @Override @@ -168,7 +187,12 @@ public class BlockSpecialFlower extends BlockFlower implements ITileEntityProvid @Override public int getRenderType() { - return LibRenderIDs.idSpecialFlower; + return 3; + } + + @Override + public EnumWorldBlockLayer getBlockLayer() { + return EnumWorldBlockLayer.CUTOUT; } @Override @@ -257,11 +281,16 @@ public class BlockSpecialFlower extends BlockFlower implements ITileEntityProvid } @Override - public int colorMultiplier(IBlockAccess world, BlockPos pos, int pass) { - return ((EnumDyeColor) world.getBlockState(pos).getValue(BotaniaStateProps.COLOR)) + public int getRenderColor(IBlockState state) { + return ((EnumDyeColor) state.getValue(BotaniaStateProps.COLOR)) .getMapColor().colorValue; } + @Override + public int colorMultiplier(IBlockAccess world, BlockPos pos, int pass) { + return getRenderColor(world.getBlockState(pos)); + } + @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) { ItemStack stack = player.getCurrentEquippedItem(); diff --git a/src/main/resources/assets/botania/blockstates/specialFlower.json b/src/main/resources/assets/botania/blockstates/specialFlower.json new file mode 100644 index 000000000..5ea130c75 --- /dev/null +++ b/src/main/resources/assets/botania/blockstates/specialFlower.json @@ -0,0 +1,14 @@ +{ + "forge_marker": 1, + "defaults": { + "model": "botania_SPECIAL:specialFlower", + "transform": "forge:default-block" + }, + "variants": { + "normal": [{ + "custom": { + "base": "minecraft:poppy#normal" + } + }] + } +} \ No newline at end of file