Lay all groundwork for special flower models. Not yet tested.

This commit is contained in:
Vincent Lee committed 2015-12-24 02:14:21 -06:00
1 parent d58f153742
commit 319bab35a0
7 files changed
+352 -9

No files matched your search

@@ -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.<Optional<Class<? extends SubTileEntity>>, ModelResourceLocation>of());
// Models registered from the externel API thus far
private static final Map<Class<? extends SubTileEntity>, ModelResourceLocation> queuedModels = Maps.newHashMap();
private final ImmutableMap<Optional<Class<? extends SubTileEntity>>, ModelResourceLocation> models;
public SpecialFlowerModel(ImmutableMap<Optional<Class<? extends SubTileEntity>>, 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<? extends SubTileEntity> subTileClass, ModelResourceLocation model) {
queuedModels.put(subTileClass, model);
}
@Override
public Collection<ResourceLocation> getDependencies() {
return ImmutableList.<ResourceLocation>copyOf(models.values());
}
@Override
public Collection<ResourceLocation> getTextures() {
return ImmutableList.of();
}
@Override
public IFlexibleBakedModel bake(IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> 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<String, String> customData) {
// Load the base variant from blockstate json, and also add all the model paths we received from external API
ImmutableMap.Builder<Optional<Class<? extends SubTileEntity>>, ModelResourceLocation> builder = ImmutableMap.builder();
for (String key : customData.keySet()) {
if ("base".equals(key)) {
builder.put(Optional.<Class<? extends SubTileEntity>>absent(), getLocation(customData.get(key)));
}
for (Map.Entry<Class<? extends SubTileEntity>, ModelResourceLocation> e : queuedModels.entrySet()) {
builder.put(Optional.<Class<? extends SubTileEntity>>of(e.getKey()), e.getValue());
}
}
ImmutableMap<Optional<Class<? extends SubTileEntity>>, 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<ItemCameraTransforms.TransformType, TRSRTransformation> transforms;
private final ImmutableMap<Optional<Class<? extends SubTileEntity>>, ModelResourceLocation> models;
private final VertexFormat vertexFormat;
private IBakedModel baseModel;
private ImmutableMap<Class<? extends SubTileEntity>, IBakedModel> bakedFlowerModels;
private ImmutableMap<Optional<EnumFacing>, ImmutableList<BakedQuad>> quads;
public SpecialFlowerBakedModel(ImmutableMap<Optional<Class<? extends SubTileEntity>>, ModelResourceLocation> models, VertexFormat format, ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> 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.<Class<? extends SubTileEntity>>absent());
ImmutableMap.Builder<Class<? extends SubTileEntity>, IBakedModel> builder = ImmutableMap.builder();
for (Map.Entry<Optional<Class<? extends SubTileEntity>>, ModelResourceLocation> e : models.entrySet()) {
if (e.getKey().isPresent()) {
builder.put(e.getKey().get(), getModel(manager, e.getKey()));
}
}
bakedFlowerModels = builder.build();
ImmutableMap.Builder<Optional<EnumFacing>, ImmutableList<BakedQuad>> quadBuilder = ImmutableMap.builder();
quadBuilder.put(Optional.<EnumFacing>absent(), buildQuads(Optional.<EnumFacing>absent()));
for (EnumFacing side : EnumFacing.values()) {
quadBuilder.put(Optional.of(side), buildQuads(Optional.of(side)));
}
quads = quadBuilder.build();
}
}
private IBakedModel getModel(ModelManager manager, Optional<Class<? extends SubTileEntity>> layer) {
ModelResourceLocation loc = models.get(layer);
if (loc == null) {
loc = new ModelResourceLocation("builtin/missing", "missing");
}
return manager.getModel(loc);
}
private ImmutableList<BakedQuad> buildQuads(Optional<EnumFacing> side) {
ImmutableList.Builder<BakedQuad> 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<BakedQuad> getFaceQuads(EnumFacing side) {
refreshBakedModels();
return quads.get(Optional.of(side));
}
@Override
public List<BakedQuad> 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<? extends SubTileEntity> clazz = extendedState.getValue(BotaniaStateProps.SUBTILE_CLASS);
return bakedFlowerModels.get(clazz);
}
@Override
public VertexFormat getFormat() {
return vertexFormat;
}
@Override
public Pair<? extends IFlexibleBakedModel, Matrix4f> handlePerspective(ItemCameraTransforms.TransformType cameraTransformType) {
return IPerspectiveAwareModel.MapWrapper.handlePerspective(this, transforms, cameraTransformType);
}
}
}
@@ -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
@@ -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<Class<? extends SubTileEntity>> {
private final String name;
public PropertyClass(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public boolean isValid(Class<? extends SubTileEntity> value) {
return true;
}
@Override
public Class<Class<? extends SubTileEntity>> getType() {
// todo is there a safe way?
Class clazz = Class.class;
return clazz;
}
@Override
public String valueToString(Class<? extends SubTileEntity> value) {
return value.getName();
}
}
@@ -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());
@@ -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();
}
@@ -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<? extends SubTileEntity> 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();