Add model caching (#1449)

This commit is contained in:
Adrian Siekierka authored and mezz committed 2016-10-07 15:10:09 -07:00
1 parent 5e3c23e4eb
commit 9dcc818a17
7 files changed
+302 -164

No files matched your search

@@ -14,6 +14,8 @@ import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import forestry.arboriculture.genetics.Tree;
import forestry.core.models.ModelBlockCached;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.BakedQuad;
@@ -41,76 +43,33 @@ import forestry.core.models.ModelBlockDefault;
import forestry.core.models.baker.ModelBaker;
import forestry.core.proxy.Proxies;
public class ModelDecorativeLeaves extends ModelBlockDefault<BlockDecorativeLeaves> {
public class ModelDecorativeLeaves extends ModelBlockCached<BlockDecorativeLeaves, TreeDefinition> {
public ModelDecorativeLeaves() {
super(BlockDecorativeLeaves.class);
}
@Override
public List<BakedQuad> getQuads(IBlockState state, EnumFacing side, long rand) {
IModelBaker baker = new ModelBaker();
Block block = state.getBlock();
if (!blockClass.isInstance(block)) {
@Override
protected TreeDefinition getInventoryKey(@Nonnull ItemStack stack) {
Block block = Block.getBlockFromItem(stack.getItem());
if (!(block instanceof BlockDecorativeLeaves)) {
return null;
}
BlockDecorativeLeaves bBlock = blockClass.cast(block);
TreeDefinition tree = state.getValue(bBlock.getVariant());
baker.setRenderBounds(Block.FULL_BLOCK_AABB);
bakeBlock(bBlock, tree, baker);
blockModel = baker.bakeModel(false);
return blockModel.getQuads(state, side, rand);
}
@Override
protected ItemOverrideList createOverrides() {
return new LeaveOverideList(this);
}
private static class LeaveOverideList extends ItemOverrideList{
private final ModelDecorativeLeaves modelDecorativeLeaves;
public LeaveOverideList(ModelDecorativeLeaves modelDecorativeLeaves) {
super(Collections.emptyList());
this.modelDecorativeLeaves = modelDecorativeLeaves;
}
@Override
public IBakedModel handleItemState(IBakedModel originalModel, ItemStack stack, World world, EntityLivingBase entity) {
IModelBaker baker = new ModelBaker();
Block block = Block.getBlockFromItem(stack.getItem());
if (!modelDecorativeLeaves.blockClass.isInstance(block)) {
return null;
}
BlockDecorativeLeaves bBlock = modelDecorativeLeaves.blockClass.cast(block);
TreeDefinition tree = bBlock.getTreeType(stack.getMetadata());
baker.setRenderBounds(Block.FULL_BLOCK_AABB);
bakeBlock(bBlock, tree, baker);
return modelDecorativeLeaves.itemModel = baker.bakeModel(true);
}
BlockDecorativeLeaves bBlock = (BlockDecorativeLeaves) block;
return bBlock.getTreeType(stack.getMetadata());
}
@Override
protected void bakeInventoryBlock(@Nonnull BlockDecorativeLeaves block, @Nonnull ItemStack item, @Nonnull IModelBaker baker) {
protected TreeDefinition getWorldKey(@Nonnull IBlockState state, IBlockAccess world, BlockPos pos) {
Block block = state.getBlock();
if (!(block instanceof BlockDecorativeLeaves)) {
return null;
}
BlockDecorativeLeaves bBlock = (BlockDecorativeLeaves) block;
return state.getValue(bBlock.getVariant());
}
@Override
protected void bakeWorldBlock(@Nonnull BlockDecorativeLeaves block, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IExtendedBlockState stateExtended, @Nonnull IModelBaker baker) {
}
public static void bakeBlock(BlockDecorativeLeaves block, TreeDefinition treeDefinition, IModelBaker baker) {
if (treeDefinition == null) {
return;
}
protected void bakeBlock(@Nonnull BlockDecorativeLeaves block, @Nonnull TreeDefinition treeDefinition, @Nonnull IModelBaker baker, boolean inventory) {
TextureMap map = Proxies.common.getClientInstance().getTextureMapBlocks();
ITreeGenome genome = treeDefinition.getGenome();
@@ -119,7 +78,7 @@ public class ModelDecorativeLeaves extends ModelBlockDefault<BlockDecorativeLeav
ResourceLocation leafSpriteLocation = leafSpriteProvider.getSprite(false, Proxies.render.fancyGraphicsEnabled());
TextureAtlasSprite leafSprite = map.getAtlasSprite(leafSpriteLocation.toString());
// Render the plain leaf block.
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, leafSprite, 0);
@@ -129,7 +88,7 @@ public class ModelDecorativeLeaves extends ModelBlockDefault<BlockDecorativeLeav
TextureAtlasSprite fruitSprite = map.getAtlasSprite(fruitSpriteLocation.toString());
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, fruitSprite, 1);
}
// Set the particle sprite
baker.setParticleSprite(leafSprite);
}
@@ -12,7 +12,9 @@ package forestry.arboriculture.models;
import javax.annotation.Nonnull;
import forestry.core.models.ModelBlockCached;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.item.ItemStack;
@@ -33,18 +35,39 @@ import forestry.core.models.ModelBlockDefault;
import forestry.core.proxy.Proxies;
import forestry.core.tiles.TileUtil;
public class ModelLeaves extends ModelBlockDefault<BlockForestryLeaves> {
import java.util.Objects;
public ModelLeaves() {
super(BlockForestryLeaves.class);
public class ModelLeaves extends ModelBlockCached<BlockForestryLeaves, ModelLeaves.Key> {
public static class Key {
public final TextureAtlasSprite leafSprite, fruitSprite;
public final boolean fancy;
private final int hashCode;
public Key(TextureAtlasSprite leafSprite, TextureAtlasSprite fruitSprite, boolean fancy) {
this.leafSprite = leafSprite;
this.fruitSprite = fruitSprite;
this.fancy = fancy;
this.hashCode = Objects.hash(leafSprite, fruitSprite, fancy);
}
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof Key)) {
return false;
} else {
Key otherKey = (Key) other;
return otherKey.leafSprite == leafSprite && otherKey.fruitSprite == fruitSprite && otherKey.fancy == fancy;
}
}
@Override
public int hashCode() {
return hashCode;
}
}
@Override
public void bakeInventoryBlock(@Nonnull BlockForestryLeaves block, @Nonnull ItemStack itemStack, @Nonnull IModelBaker baker) {
if (!(itemStack.getItem() instanceof ItemBlockLeaves) || block == null) {
return;
}
protected Key getInventoryKey(@Nonnull ItemStack itemStack) {
TextureMap map = Proxies.common.getClientInstance().getTextureMapBlocks();
TileLeaves leaves = new TileLeaves();
@@ -54,55 +77,51 @@ public class ModelLeaves extends ModelBlockDefault<BlockForestryLeaves> {
leaves.setTree(TreeRoot.treeTemplates.get(0));
}
ResourceLocation leafSpriteLocation = leaves.getLeaveSprite(Proxies.render.fancyGraphicsEnabled());
TextureAtlasSprite leavesIcon = map.getAtlasSprite(leafSpriteLocation.toString());
if (leavesIcon == null) {
return;
}
boolean fancy = Proxies.render.fancyGraphicsEnabled();
ResourceLocation leafLocation = leaves.getLeaveSprite(fancy);
ResourceLocation fruitLocation = leaves.getFruitSprite();
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, leavesIcon, 0);
// add fruit
if (!leaves.hasFruit()) {
return;
}
ResourceLocation fruitSpriteLocation = leaves.getFruitSprite();
if (fruitSpriteLocation != null) {
TextureAtlasSprite fruitTexture = map.getAtlasSprite(fruitSpriteLocation.toString());
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, fruitTexture, 1);
}
return new Key(map.getAtlasSprite(leafLocation.toString()),
fruitLocation != null ? map.getAtlasSprite(fruitLocation.toString()) : null,
fancy);
}
@Override
public void bakeWorldBlock(@Nonnull BlockForestryLeaves block, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IExtendedBlockState stateExtended, @Nonnull IModelBaker baker) {
protected Key getWorldKey(@Nonnull IBlockState state, IBlockAccess world, BlockPos pos) {
TileLeaves tile = TileUtil.getTile(world, pos, TileLeaves.class);
boolean fancy = Proxies.render.fancyGraphicsEnabled();
TextureMap map = Proxies.common.getClientInstance().getTextureMapBlocks();
if (tile == null) {
IAlleleTreeSpecies oakSpecies = TreeDefinition.Oak.getIndividual().getGenome().getPrimary();
ResourceLocation spriteLocation = oakSpecies.getLeafSpriteProvider().getSprite(false, Proxies.render.fancyGraphicsEnabled());
ResourceLocation spriteLocation = oakSpecies.getLeafSpriteProvider().getSprite(false, fancy);
TextureAtlasSprite sprite = map.getAtlasSprite(spriteLocation.toString());
baker.setParticleSprite(sprite);
return;
return new Key(sprite, null, fancy);
}
ResourceLocation leafSpriteLocation = tile.getLeaveSprite(Proxies.render.fancyGraphicsEnabled());
TextureAtlasSprite leafSprite = map.getAtlasSprite(leafSpriteLocation.toString());
ResourceLocation leafLocation = tile.getLeaveSprite(fancy);
ResourceLocation fruitLocation = tile.getFruitSprite();
return new Key(map.getAtlasSprite(leafLocation.toString()),
fruitLocation != null ? map.getAtlasSprite(fruitLocation.toString()) : null,
fancy);
}
@Override
protected void bakeBlock(@Nonnull BlockForestryLeaves block, @Nonnull Key key, @Nonnull IModelBaker baker, boolean inventory) {
// Render the plain leaf block.
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, leafSprite, 0);
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, key.leafSprite, 0);
// Render overlay for fruit leaves.
ResourceLocation fruitSpriteLocation = tile.getFruitSprite();
if (fruitSpriteLocation != null) {
TextureAtlasSprite fruitSprite = map.getAtlasSprite(fruitSpriteLocation.toString());
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, fruitSprite, 1);
if (key.fruitSprite != null) {
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, key.fruitSprite, 1);
}
// Set the particle sprite
baker.setParticleSprite(leafSprite);
baker.setParticleSprite(key.leafSprite);
}
public ModelLeaves() {
super(BlockForestryLeaves.class);
}
}
@@ -12,6 +12,7 @@ package forestry.core;
import java.util.Collection;
import forestry.core.models.ModelBlockCached;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.LootPool;
@@ -94,6 +95,7 @@ public class EventHandlerCore {
public void handleTextureRemap(TextureStitchEvent.Pre event) {
ErrorStateRegistry.initSprites();
TextureManager.initDefaultSprites();
ModelBlockCached.clear();
}
@SubscribeEvent
@@ -0,0 +1,74 @@
package forestry.core.models;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public abstract class ModelBlockCached<B extends Block, K extends Object> extends ModelBlockDefault<B, K> {
private static final boolean DISABLE_CACHE = false;
private static final Set<ModelBlockCached> CACHE_PROVIDERS = new HashSet<>();
private final Cache<K, IBakedModel> inventoryCache, worldCache;
public static void clear() {
for (ModelBlockCached modelBlockCached : CACHE_PROVIDERS) {
modelBlockCached.worldCache.invalidateAll();
modelBlockCached.inventoryCache.invalidateAll();
}
}
protected ModelBlockCached(@Nonnull Class<B> blockClass) {
super(blockClass);
worldCache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build();
inventoryCache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build();
}
@Override
protected IBakedModel getModel(IBlockState state, IBlockAccess world, BlockPos pos) {
K key = getWorldKey(state, world, pos);
if (key == null) {
return null;
}
if (DISABLE_CACHE) {
return bakeModel(state, key);
}
IBakedModel model = worldCache.getIfPresent(key);
if (model == null) {
model = bakeModel(state, key);
worldCache.put(key, model);
}
return model;
}
@Override
protected IBakedModel getModel(ItemStack stack, World world) {
K key = getInventoryKey(stack);
if (key == null) {
return null;
}
if (DISABLE_CACHE) {
return bakeModel(stack, world, key);
}
IBakedModel model = inventoryCache.getIfPresent(key);
if (model == null) {
model = bakeModel(stack, world, key);
inventoryCache.put(key, model);
}
return model;
}
}
@@ -36,7 +36,7 @@ import forestry.core.blocks.propertys.UnlistedBlockAccess;
import forestry.core.blocks.propertys.UnlistedBlockPos;
import forestry.core.models.baker.ModelBaker;
public abstract class ModelBlockDefault<B extends Block> implements IBakedModel {
public abstract class ModelBlockDefault<B extends Block, K extends Object> implements IBakedModel {
private ItemOverrideList overrideList;
@Nonnull
protected final Class<B> blockClass;
@@ -48,12 +48,15 @@ public abstract class ModelBlockDefault<B extends Block> implements IBakedModel
this.blockClass = blockClass;
}
@Override
public List<BakedQuad> getQuads(IBlockState state, EnumFacing side, long rand) {
if(state instanceof IExtendedBlockState){
protected IBakedModel bakeModel(IBlockState state, K key) {
if (key == null) {
return null;
}
if(state instanceof IExtendedBlockState) {
IModelBaker baker = new ModelBaker();
IExtendedBlockState stateExtended = (IExtendedBlockState) state;
IBlockAccess world = stateExtended.getValue(UnlistedBlockAccess.BLOCKACCESS);
BlockPos pos = stateExtended.getValue(UnlistedBlockPos.POS);
Block block = state.getBlock();
@@ -61,15 +64,64 @@ public abstract class ModelBlockDefault<B extends Block> implements IBakedModel
return null;
}
B bBlock = blockClass.cast(block);
baker.setRenderBounds(block.getBoundingBox(state, world, pos));
bakeWorldBlock(bBlock, world, pos, stateExtended, baker);
baker.setRenderBounds(state.getBoundingBox(world, pos));
bakeBlock(bBlock, key, baker, false);
blockModel = baker.bakeModel(false);
onCreateModel(blockModel);
return blockModel.getQuads(state, side, rand);
return blockModel;
} else {
return null;
}
}
protected IBakedModel getModel(IBlockState state, IBlockAccess world, BlockPos pos) {
return bakeModel(state, getWorldKey(state, world, pos));
}
protected IBakedModel bakeModel(ItemStack stack, World world, K key) {
if (key == null) {
return null;
}
IModelBaker baker = new ModelBaker();
Block block = Block.getBlockFromItem(stack.getItem());
if (!blockClass.isInstance(block)) {
return null;
}
B bBlock = blockClass.cast(block);
// FIXME: This way of getting the IBlockState will probably backfire.
IBlockState state = block.getStateFromMeta(stack.getItemDamage());
baker.setRenderBounds(state.getBoundingBox(world, null));
bakeBlock(bBlock, key, baker, true);
return itemModel = baker.bakeModel(true);
}
protected IBakedModel getModel(ItemStack stack, World world) {
return bakeModel(stack, world, getInventoryKey(stack));
}
@Override
public List<BakedQuad> getQuads(IBlockState state, EnumFacing side, long rand) {
IBakedModel model;
if (state instanceof IExtendedBlockState) {
IExtendedBlockState stateExtended = (IExtendedBlockState) state;
IBlockAccess world = stateExtended.getValue(UnlistedBlockAccess.BLOCKACCESS);
BlockPos pos = stateExtended.getValue(UnlistedBlockPos.POS);
model = getModel(state, world, pos);
} else {
model = getModel(state, null, null);
}
if (model != null) {
return model.getQuads(state, side, rand);
} else {
return Collections.emptyList();
}
return Collections.emptyList();
}
protected void onCreateModel(IModelBakerModel model){
@@ -128,9 +180,9 @@ public abstract class ModelBlockDefault<B extends Block> implements IBakedModel
return overrideList;
}
protected abstract void bakeInventoryBlock(@Nonnull B block, @Nonnull ItemStack item, @Nonnull IModelBaker baker);
protected abstract void bakeWorldBlock(@Nonnull B block, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IExtendedBlockState stateExtended, @Nonnull IModelBaker baker);
protected abstract K getInventoryKey(@Nonnull ItemStack stack);
protected abstract K getWorldKey(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos);
protected abstract void bakeBlock(@Nonnull B block, @Nonnull K key, @Nonnull IModelBaker baker, boolean inventory);
private class DefaultItemOverrideList extends ItemOverrideList {
public DefaultItemOverrideList() {
@@ -139,17 +191,7 @@ public abstract class ModelBlockDefault<B extends Block> implements IBakedModel
@Override
public IBakedModel handleItemState(IBakedModel originalModel, ItemStack stack, World world, EntityLivingBase entity) {
IModelBaker baker = new ModelBaker();
Block block = Block.getBlockFromItem(stack.getItem());
if (!blockClass.isInstance(block)) {
return null;
}
B bBlock = blockClass.cast(block);
baker.setRenderBounds(block.getBoundingBox(block.getStateFromMeta(stack.getItemDamage()), world, null));
bakeInventoryBlock(bBlock, stack, baker);
return itemModel = baker.bakeModel(true);
return getModel(stack, world);
}
}
}
@@ -2,7 +2,10 @@ package forestry.farming.models;
import javax.annotation.Nonnull;
import forestry.arboriculture.models.ModelLeaves;
import forestry.core.models.ModelBlockCached;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
@@ -17,46 +20,71 @@ import forestry.farming.blocks.BlockFarm;
import forestry.farming.blocks.EnumFarmBlockType;
import forestry.farming.tiles.TileFarm;
public class ModelFarmBlock extends ModelBlockDefault<BlockFarm> {
import java.util.Objects;
public class ModelFarmBlock extends ModelBlockCached<BlockFarm, ModelFarmBlock.Key> {
public static class Key {
public final EnumFarmBlockTexture texture;
public final EnumFarmBlockType type;
private final int hashCode;
public Key(EnumFarmBlockTexture texture, EnumFarmBlockType type) {
this.texture = texture;
this.type = type;
this.hashCode = Objects.hash(texture, type);
}
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof Key)) {
return false;
} else {
Key otherKey = (Key) other;
return otherKey.texture == texture && otherKey.type == type;
}
}
@Override
public int hashCode() {
return hashCode;
}
}
public ModelFarmBlock() {
super(BlockFarm.class);
}
@Override
public void bakeInventoryBlock(@Nonnull BlockFarm blockFarm, @Nonnull ItemStack item, @Nonnull IModelBaker baker) {
if (blockFarm == null) {
return;
}
protected Key getInventoryKey(@Nonnull ItemStack stack) {
EnumFarmBlockTexture texture = EnumFarmBlockTexture.getFromCompound(stack.getTagCompound());
EnumFarmBlockType type = EnumFarmBlockType.VALUES[stack.getItemDamage()];
EnumFarmBlockTexture type = EnumFarmBlockTexture.getFromCompound(item.getTagCompound());
// Add the plain block.
baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, null, getSprites(type), 0);
// Add the overlay block.
baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, null, getOverlaySprites(EnumFarmBlockType.VALUES[item.getItemDamage()]), 0);
return new Key(texture, type);
}
@Override
public void bakeWorldBlock(@Nonnull BlockFarm blockFarm, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IExtendedBlockState stateExtended, @Nonnull IModelBaker baker) {
protected Key getWorldKey(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) {
TileFarm farm = TileUtil.getTile(world, pos, TileFarm.class);
EnumFarmBlockTexture texture = EnumFarmBlockTexture.BRICK;
TextureAtlasSprite[] overlayTextures = getOverlaySprites(EnumFarmBlockType.PLAIN);
if(farm != null){
EnumFarmBlockType type = EnumFarmBlockType.PLAIN;
if (farm != null) {
texture = farm.getFarmBlockTexture();
overlayTextures = getOverlaySprites(farm.getFarmBlockType());
type = farm.getFarmBlockType();
}
TextureAtlasSprite[] textures = getSprites(texture);
return new Key(texture, type);
}
@Override
protected void bakeBlock(@Nonnull BlockFarm blockFarm, @Nonnull Key key, @Nonnull IModelBaker baker, boolean inventory) {
TextureAtlasSprite[] textures = getSprites(key.texture);
// Add the plain block.
baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, pos, textures, 0);
baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, null, textures, 0);
// Add the overlay block.
baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, pos, overlayTextures, 0);
baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, null, getOverlaySprites(key.type), 0);
// Set the particle sprite
baker.setParticleSprite(textures[0]);
}
@@ -35,46 +35,61 @@ import forestry.core.utils.CamouflageUtil;
import forestry.greenhouse.blocks.BlockGreenhouse;
import forestry.greenhouse.blocks.BlockGreenhouseType;
public class ModelGreenhouse extends ModelBlockDefault<BlockGreenhouse> {
// DO NOT CACHE THIS. PLEASE. I BEG YOU. NOT IN THE CURRENT STATE.
public class ModelGreenhouse extends ModelBlockDefault<BlockGreenhouse, ModelGreenhouse.Key> {
public static class Key {
public final IExtendedBlockState state;
public final IBlockAccess world;
public final BlockPos pos;
public Key(IExtendedBlockState state, IBlockAccess world, BlockPos pos) {
this.state = state;
this.world = world;
this.pos = pos;
}
}
public ModelGreenhouse() {
super(BlockGreenhouse.class);
}
@Override
protected void bakeInventoryBlock(@Nonnull BlockGreenhouse block, @Nonnull ItemStack item, @Nonnull IModelBaker baker) {
bakeBlockModel(block, null, null, null, baker, null);
protected Key getInventoryKey(@Nonnull ItemStack stack) {
return new Key(null, null, null);
}
@Override
protected void bakeWorldBlock(@Nonnull BlockGreenhouse block, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IExtendedBlockState stateExtended, @Nonnull IModelBaker baker) {
ItemStack camouflageStack = CamouflageUtil.getCamouflageBlock(world, pos);
bakeBlockModel(block, world, pos, stateExtended, baker, camouflageStack);
protected Key getWorldKey(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos) {
return new Key(state instanceof IExtendedBlockState ? ((IExtendedBlockState) state) : null, world, pos);
}
private static void bakeBlockModel(@Nonnull BlockGreenhouse block, @Nullable IBlockAccess world, @Nullable BlockPos pos, @Nullable IExtendedBlockState stateExtended, @Nonnull IModelBaker baker, @Nullable ItemStack camouflageStack) {
@Override
protected void bakeBlock(@Nonnull BlockGreenhouse block, @Nonnull Key key, @Nonnull IModelBaker baker, boolean inventory) {
ItemStack camouflageStack = key.world != null ? CamouflageUtil.getCamouflageBlock(key.world, key.pos) : null;
IBlockAccess world = key.world;
BlockPos pos = key.pos;
if (camouflageStack != null) {
ICamouflageHandler camouflageHandler = CamouflageUtil.getCamouflageHandler(world, pos);
ICamouflagedTile camouflageTile = (ICamouflagedTile) world.getTileEntity(pos);
ICamouflageItemHandler itemHandler = CamouflageManager.camouflageAccess.getHandlerFromItem(camouflageStack);
if(itemHandler != null){
Pair<IBlockState, IBakedModel> model = itemHandler.getModel(camouflageStack, camouflageHandler, camouflageTile);
baker.addBakedModel(model.getLeft(), model.getRight());
baker.setParticleSprite(model.getRight().getParticleTexture());
}
}
//Bake the default blocks
else if (block.getGreenhouseType() == BlockGreenhouseType.GLASS) {
TextureAtlasSprite glassSprite = BlockGreenhouseType.getSprite(BlockGreenhouseType.GLASS, null, null, world, pos);
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, BlockGreenhouseType.getSprite(BlockGreenhouseType.GLASS, null, null, world, pos), 100);
baker.setParticleSprite(glassSprite);
} else {
TextureAtlasSprite plainSprite = BlockGreenhouseType.getSprite(BlockGreenhouseType.PLAIN, null, null, world, pos);
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, BlockGreenhouseType.getSprite(BlockGreenhouseType.PLAIN, null, null, world, pos), 100);
baker.setParticleSprite(plainSprite);
}
@@ -82,10 +97,9 @@ public class ModelGreenhouse extends ModelBlockDefault<BlockGreenhouse> {
if (block.getGreenhouseType().hasOverlaySprite) {
TextureAtlasSprite[] sprite = new TextureAtlasSprite[6];
for (EnumFacing facing : EnumFacing.VALUES) {
sprite[facing.ordinal()] = BlockGreenhouseType.getSprite(block.getGreenhouseType(), facing, stateExtended, world, pos);
sprite[facing.ordinal()] = BlockGreenhouseType.getSprite(block.getGreenhouseType(), facing, key.state, world, pos);
}
baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, sprite, 101);
}
}
}