diff --git a/src/main/java/forestry/api/arboriculture/ITreeGenome.java b/src/main/java/forestry/api/arboriculture/ITreeGenome.java index 25f781587..a2b5f8612 100644 --- a/src/main/java/forestry/api/arboriculture/ITreeGenome.java +++ b/src/main/java/forestry/api/arboriculture/ITreeGenome.java @@ -44,4 +44,9 @@ public interface ITreeGenome extends IGenome { * @return A ItemStack that is used in getPickBlock of the leave block. */ ItemStack getDecorativeLeaves(); + + /** + * @return true if this genome matches one of the default template genomes + */ + boolean matchesTemplateGenome(); } diff --git a/src/main/java/forestry/arboriculture/PluginArboriculture.java b/src/main/java/forestry/arboriculture/PluginArboriculture.java index 0322f1641..4a161276b 100644 --- a/src/main/java/forestry/arboriculture/PluginArboriculture.java +++ b/src/main/java/forestry/arboriculture/PluginArboriculture.java @@ -33,12 +33,14 @@ import forestry.api.core.ForestryAPI; import forestry.api.core.IArmorNaturalist; import forestry.api.genetics.AlleleManager; import forestry.api.genetics.IBlockTranslator; +import forestry.api.genetics.IIndividual; import forestry.api.genetics.IItemTranslator; import forestry.api.recipes.RecipeManagers; import forestry.api.storage.ICrateRegistry; import forestry.api.storage.StorageManager; import forestry.arboriculture.blocks.BlockArbLog; import forestry.arboriculture.blocks.BlockArbSlab; +import forestry.arboriculture.blocks.BlockDefaultLeaves; import forestry.arboriculture.blocks.BlockForestryLeaves; import forestry.arboriculture.blocks.BlockRegistryArboriculture; import forestry.arboriculture.capabilities.ArmorNaturalist; @@ -530,6 +532,17 @@ public class PluginArboriculture extends BlankForestryPlugin { return null; } }); + + for (BlockDefaultLeaves leaves : getBlocks().leavesDefault) { + TreeManager.treeRoot.registerTranslator(leaves, (IBlockTranslator) blockState -> { + TreeDefinition treeDefinition = leaves.getTreeDefinition(blockState); + if (treeDefinition != null) { + return treeDefinition.getIndividual(); + } else { + return null; + } + }); + } } @Override diff --git a/src/main/java/forestry/arboriculture/blocks/BlockAbstractLeaves.java b/src/main/java/forestry/arboriculture/blocks/BlockAbstractLeaves.java new file mode 100644 index 000000000..644cf8e45 --- /dev/null +++ b/src/main/java/forestry/arboriculture/blocks/BlockAbstractLeaves.java @@ -0,0 +1,201 @@ +package forestry.arboriculture.blocks; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import com.mojang.authlib.GameProfile; +import forestry.api.arboriculture.IToolGrafter; +import forestry.api.arboriculture.ITree; +import forestry.api.core.IItemModelRegister; +import forestry.api.core.Tabs; +import forestry.arboriculture.LeafDecayHelper; +import forestry.arboriculture.genetics.TreeDefinition; +import forestry.arboriculture.tiles.TileLeaves; +import forestry.core.blocks.IColoredBlock; +import forestry.core.proxy.Proxies; +import forestry.core.tiles.TileUtil; +import forestry.core.utils.BlockUtil; +import net.minecraft.block.BlockLeaves; +import net.minecraft.block.BlockPlanks; +import net.minecraft.block.state.IBlockState; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Enchantments; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.BlockRenderLayer; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.NonNullList; +import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +/** + * Parent class for shared behavior between {@link BlockDefaultLeaves} and {@link BlockForestryLeaves} + */ +public abstract class BlockAbstractLeaves extends BlockLeaves implements IItemModelRegister, IColoredBlock { + @Nullable + protected abstract ITree getTree(IBlockAccess world, BlockPos pos); + + @Override + public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) { + LeafDecayHelper.leafDecay(this, world, pos); + } + + public abstract int getMetaFromState(IBlockState state); + + public abstract IBlockState getStateFromMeta(int meta); + + @Override + public final void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList list) { + // creative menu shows BlockDecorativeLeaves instead of these + } + + @Override + public final ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) { + ITree tree = getTree(world, pos); + if (tree == null) { + return ItemStack.EMPTY; + } + + return tree.getGenome().getDecorativeLeaves(); + } + + @Override + public final List onSheared(ItemStack item, IBlockAccess world, BlockPos pos, int fortune) { + ITree tree = getTree(world, pos); + if (tree == null) { + tree = TreeDefinition.Oak.getIndividual(); + } + + ItemStack decorativeLeaves = tree.getGenome().getDecorativeLeaves(); + if (decorativeLeaves.isEmpty()) { + return Collections.emptyList(); + } else { + return Collections.singletonList(decorativeLeaves); + } + } + + @Nullable + @Override + public final AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) { + ITree tree = getTree(worldIn, pos); + if (tree != null && TreeDefinition.Willow.getUID().equals(tree.getIdent())) { + return null; + } + return super.getCollisionBoundingBox(blockState, worldIn, pos); + } + + /** + * Used for walking through willow leaves. + */ + @Override + public final void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) { + super.onEntityCollidedWithBlock(worldIn, pos, state, entityIn); + entityIn.motionX *= 0.4D; + entityIn.motionZ *= 0.4D; + } + + /* RENDERING */ + @Override + public final boolean isOpaqueCube(IBlockState state) { + return !Proxies.render.fancyGraphicsEnabled(); + } + + @SideOnly(Side.CLIENT) + @Override + public final boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) { + return (Proxies.render.fancyGraphicsEnabled() || blockAccess.getBlockState(pos.offset(side)).getBlock() != this) && + BlockUtil.shouldSideBeRendered(blockState, blockAccess, pos, side); + } + + @Override + @SideOnly(Side.CLIENT) + public final BlockRenderLayer getBlockLayer() { + return BlockRenderLayer.CUTOUT_MIPPED; // fruit overlays require CUTOUT_MIPPED, even in Fast graphics + } + + /** + * unused, just here to satisfy BlockLeaves + */ + @Override + public final BlockPlanks.EnumType getWoodType(int meta) { + return BlockPlanks.EnumType.OAK; + } + + /* PROPERTIES */ + @Override + public final int getFlammability(IBlockAccess world, BlockPos pos, EnumFacing face) { + return 60; + } + + @Override + public final boolean isFlammable(IBlockAccess world, BlockPos pos, EnumFacing face) { + return true; + } + + @Override + public final int getFireSpreadSpeed(IBlockAccess world, BlockPos pos, EnumFacing face) { + if (face == EnumFacing.DOWN) { + return 20; + } else if (face != EnumFacing.UP) { + return 10; + } else { + return 5; + } + } + + /* DROP HANDLING */ + // Hack: When harvesting leaves we need to get the drops in onBlockHarvested, + // because there is no Player parameter in getDrops() + // and Mojang destroys the block and tile before calling getDrops. + private final ThreadLocal> drops = new ThreadLocal<>(); + + /** + * {@link IToolGrafter}'s drop bonus handling is done here. + */ + @Override + public final void onBlockHarvested(World world, BlockPos pos, IBlockState state, EntityPlayer player) { + int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, player.getActiveItemStack()); + float saplingModifier = 1.0f; + + ItemStack heldStack = player.inventory.getCurrentItem(); + Item heldItem = heldStack.getItem(); + if (heldItem instanceof IToolGrafter) { + IToolGrafter grafter = (IToolGrafter) heldItem; + saplingModifier = grafter.getSaplingModifier(heldStack, world, player, pos); + heldStack.damageItem(1, player); + if (heldStack.isEmpty()) { + net.minecraftforge.event.ForgeEventFactory.onPlayerDestroyItem(player, heldStack, EnumHand.MAIN_HAND); + } + } + + GameProfile playerProfile = player.getGameProfile(); + List leafDrops = getLeafDrop(world, playerProfile, pos, saplingModifier, fortune); + drops.set(leafDrops); + } + + @Override + public final List getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) { + List ret = drops.get(); + drops.remove(); + + // leaves not harvested, get drops normally + if (ret == null) { + ret = getLeafDrop((World) world, null, pos, 1.0f, fortune); + } + + return ret; + } + + protected abstract NonNullList getLeafDrop(World world, @Nullable GameProfile playerProfile, BlockPos pos, float saplingModifier, int fortune); +} diff --git a/src/main/java/forestry/arboriculture/blocks/BlockDefaultLeaves.java b/src/main/java/forestry/arboriculture/blocks/BlockDefaultLeaves.java new file mode 100644 index 000000000..707d00be8 --- /dev/null +++ b/src/main/java/forestry/arboriculture/blocks/BlockDefaultLeaves.java @@ -0,0 +1,192 @@ +package forestry.arboriculture.blocks; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; + +import com.mojang.authlib.GameProfile; +import forestry.api.arboriculture.EnumGermlingType; +import forestry.api.arboriculture.IFruitProvider; +import forestry.api.arboriculture.ITree; +import forestry.api.arboriculture.ITreeGenome; +import forestry.api.arboriculture.TreeManager; +import forestry.api.core.IModelManager; +import forestry.arboriculture.genetics.TreeDefinition; +import net.minecraft.block.state.BlockStateContainer; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.NonNullList; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.client.model.ModelLoader; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +/** + * Genetic leaves with no tile entity, used for worldgen trees. + * Similar to decorative leaves, but these will drop saplings and can be used for pollination. + */ +public abstract class BlockDefaultLeaves extends BlockAbstractLeaves { + private static final int VARIANTS_PER_BLOCK = 4; + + public static List create() { + List blocks = new ArrayList<>(); + final int blockCount = PropertyTreeType.getBlockCount(VARIANTS_PER_BLOCK); + for (int blockNumber = 0; blockNumber < blockCount; blockNumber++) { + final PropertyTreeType variant = PropertyTreeType.create("variant", blockNumber, VARIANTS_PER_BLOCK); + BlockDefaultLeaves block = new BlockDefaultLeaves(blockNumber) { + @Override + public PropertyTreeType getVariant() { + return variant; + } + }; + blocks.add(block); + } + return blocks; + } + + private final int blockNumber; + + public BlockDefaultLeaves(int blockNumber) { + this.blockNumber = blockNumber; + PropertyTreeType variant = getVariant(); + setDefaultState(this.blockState.getBaseState() + .withProperty(variant, variant.getFirstType()) + .withProperty(CHECK_DECAY, false) + .withProperty(DECAYABLE, true)); + } + + public int getBlockNumber() { + return blockNumber; + } + + protected abstract PropertyTreeType getVariant(); + + @Nullable + public TreeDefinition getTreeDefinition(IBlockState blockState) { + if (blockState.getBlock() == this) { + return blockState.getValue(getVariant()); + } else { + return null; + } + } + + @Override + public int damageDropped(IBlockState state) { + TreeDefinition treeDefinition = getTreeDefinition(state); + if (treeDefinition == null) { + return 0; + } + return treeDefinition.getMetadata() - blockNumber * VARIANTS_PER_BLOCK; + } + + @Override + public IBlockState getStateFromMeta(int meta) { + return this.getDefaultState() + .withProperty(getVariant(), getTreeType(meta)) + .withProperty(DECAYABLE, (meta & 4) == 0) + .withProperty(CHECK_DECAY, (meta & 8) > 0); + } + + @Override + public int getMetaFromState(IBlockState state) { + int i = damageDropped(state); + + if (!state.getValue(DECAYABLE)) { + i |= 4; + } + + if (state.getValue(CHECK_DECAY)) { + i |= 8; + } + + return i; + } + + protected BlockStateContainer createBlockState() { + return new BlockStateContainer(this, getVariant(), CHECK_DECAY, DECAYABLE); + } + + public TreeDefinition getTreeType(int meta) { + int variantCount = getVariant().getAllowedValues().size(); + int variantMeta = (meta % variantCount) + blockNumber * VARIANTS_PER_BLOCK; + return TreeDefinition.byMetadata(variantMeta); + } + + @Override + protected NonNullList getLeafDrop(World world, @Nullable GameProfile playerProfile, BlockPos pos, float saplingModifier, int fortune) { + NonNullList prod = NonNullList.create(); + + ITree tree = getTree(world, pos); + if (tree == null) { + return prod; + } + + // Add saplings + List saplings = tree.getSaplings(world, playerProfile, pos, saplingModifier); + for (ITree sapling : saplings) { + if (sapling != null) { + prod.add(TreeManager.treeRoot.getMemberStack(sapling, EnumGermlingType.SAPLING)); + } + } + + // Add fruits + ITreeGenome genome = tree.getGenome(); + IFruitProvider fruitProvider = genome.getFruitProvider(); + if (fruitProvider.isFruitLeaf(genome, world, pos)) { + NonNullList produceStacks = tree.produceStacks(world, pos, Integer.MAX_VALUE); + prod.addAll(produceStacks); + } + + return prod; + } + + @Override + public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) { + TreeDefinition type = getTreeType(meta); + return getDefaultState().withProperty(getVariant(), type); + } + + @Override + @SideOnly(Side.CLIENT) + public void registerModel(Item item, IModelManager manager) { + for (IBlockState state : blockState.getValidStates()) { + int meta = getMetaFromState(state); + ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation("forestry:leaves.default." + blockNumber, "inventory")); + } + } + + @Override + protected ITree getTree(IBlockAccess world, BlockPos pos) { + IBlockState blockState = world.getBlockState(pos); + TreeDefinition treeDefinition = getTreeDefinition(blockState); + if (treeDefinition != null) { + return treeDefinition.getIndividual(); + } else { + return null; + } + } + + @Override + @SideOnly(Side.CLIENT) + public int colorMultiplier(IBlockState state, @Nullable IBlockAccess worldIn, @Nullable BlockPos pos, int tintIndex) { + TreeDefinition treeDefinition = getTreeDefinition(state); + if (treeDefinition == null) { + treeDefinition = TreeDefinition.Oak; + } + ITreeGenome genome = treeDefinition.getGenome(); + + if (tintIndex == 0) { + return genome.getPrimary().getLeafSpriteProvider().getColor(false); + } else { + IFruitProvider fruitProvider = genome.getFruitProvider(); + return fruitProvider.getDecorativeColor(); + } + } +} diff --git a/src/main/java/forestry/arboriculture/blocks/BlockForestryLeaves.java b/src/main/java/forestry/arboriculture/blocks/BlockForestryLeaves.java index 3ce5cba16..831975a7c 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockForestryLeaves.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockForestryLeaves.java @@ -11,36 +11,24 @@ package forestry.arboriculture.blocks; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Random; import com.mojang.authlib.GameProfile; import forestry.api.arboriculture.EnumGermlingType; -import forestry.api.arboriculture.IToolGrafter; import forestry.api.arboriculture.ITree; import forestry.api.arboriculture.TreeManager; -import forestry.api.core.IItemModelRegister; import forestry.api.core.IModelManager; import forestry.api.core.IToolScoop; -import forestry.api.core.Tabs; import forestry.api.lepidopterology.ButterflyManager; import forestry.api.lepidopterology.EnumFlutterType; import forestry.api.lepidopterology.IButterfly; -import forestry.arboriculture.LeafDecayHelper; import forestry.arboriculture.PluginArboriculture; -import forestry.arboriculture.genetics.TreeDefinition; import forestry.arboriculture.tiles.TileLeaves; -import forestry.core.blocks.IColoredBlock; import forestry.core.blocks.properties.UnlistedBlockAccess; import forestry.core.blocks.properties.UnlistedBlockPos; -import forestry.core.proxy.Proxies; import forestry.core.tiles.TileUtil; -import forestry.core.utils.BlockUtil; import forestry.core.utils.ItemStackUtil; -import net.minecraft.block.BlockLeaves; -import net.minecraft.block.BlockPlanks; import net.minecraft.block.IGrowable; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.properties.IProperty; @@ -48,21 +36,14 @@ import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelResourceLocation; -import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.enchantment.EnchantmentHelper; -import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Enchantments; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.BlockRenderLayer; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.NonNullList; -import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.client.model.ModelLoader; @@ -72,22 +53,45 @@ import net.minecraftforge.common.property.IUnlistedProperty; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class BlockForestryLeaves extends BlockLeaves implements ITileEntityProvider, IGrowable, IItemModelRegister, IColoredBlock { +public class BlockForestryLeaves extends BlockAbstractLeaves implements ITileEntityProvider, IGrowable { public BlockForestryLeaves() { - setCreativeTab(Tabs.tabArboriculture); - setDefaultState(this.blockState.getBaseState().withProperty(CHECK_DECAY, false).withProperty(DECAYABLE, true)); - } - - @Override - public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos) { - return ((IExtendedBlockState) super.getExtendedState(state, world, pos)).withProperty(UnlistedBlockPos.POS, pos) - .withProperty(UnlistedBlockAccess.BLOCKACCESS, world); + setDefaultState(this.blockState.getBaseState() + .withProperty(CHECK_DECAY, false) + .withProperty(DECAYABLE, true)); } @Override protected BlockStateContainer createBlockState() { - return new ExtendedBlockState(this, new IProperty[]{DECAYABLE, CHECK_DECAY}, new IUnlistedProperty[]{UnlistedBlockPos.POS, UnlistedBlockAccess.BLOCKACCESS}); + return new ExtendedBlockState(this, + new IProperty[]{DECAYABLE, CHECK_DECAY}, + new IUnlistedProperty[]{UnlistedBlockPos.POS, UnlistedBlockAccess.BLOCKACCESS} + ); + } + + @Override + protected ITree getTree(IBlockAccess world, BlockPos pos) { + TileLeaves leaves = TileUtil.getTile(world, pos, TileLeaves.class); + if (leaves != null) { + ITree tree = leaves.getTree(); + if (tree != null) { + return tree; + } + } + + return null; + } + + @Override + public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) { + super.updateTick(world, pos, state, rand); + + TileLeaves tileLeaves = TileUtil.getTile(world, pos, TileLeaves.class); + + // check leaves tile because they might have decayed + if (tileLeaves != null && !tileLeaves.isInvalid() && rand.nextFloat() <= 0.1) { + tileLeaves.onBlockTick(world, pos, state, rand); + } } @Override @@ -109,6 +113,12 @@ public class BlockForestryLeaves extends BlockLeaves implements ITileEntityProvi return this.getDefaultState().withProperty(DECAYABLE, (meta & 4) == 0).withProperty(CHECK_DECAY, (meta & 8) > 0); } + @Override + public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos) { + return ((IExtendedBlockState) super.getExtendedState(state, world, pos)).withProperty(UnlistedBlockPos.POS, pos) + .withProperty(UnlistedBlockAccess.BLOCKACCESS, world); + } + /* TILE ENTITY */ @Override public TileEntity createNewTileEntity(World world, int meta) { @@ -116,54 +126,8 @@ public class BlockForestryLeaves extends BlockLeaves implements ITileEntityProvi } @Override - public void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList list) { - - } - - /* DROP HANDLING */ - // Hack: When harvesting leaves we need to get the drops in onBlockHarvested, - // because Mojang destroys the block and tile before calling getDrops. - private final ThreadLocal> drops = new ThreadLocal<>(); - - @Override - public void onBlockHarvested(World world, BlockPos pos, IBlockState state, EntityPlayer player) { - TileLeaves leafTile = TileUtil.getTile(world, pos, TileLeaves.class); - if (leafTile == null) { - return; - } - - int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, player.getActiveItemStack()); - float saplingModifier = 1.0f; - - ItemStack held = player.inventory.getCurrentItem(); - if (held.getItem() instanceof IToolGrafter) { - saplingModifier = ((IToolGrafter) held.getItem()).getSaplingModifier(held, world, player, pos); - held.damageItem(1, player); - if (held.isEmpty()) { - net.minecraftforge.event.ForgeEventFactory.onPlayerDestroyItem(player, held, EnumHand.MAIN_HAND); - } - } - - GameProfile playerProfile = player.getGameProfile(); - List leafDrops = getLeafDrop(world, playerProfile, pos, saplingModifier, fortune); - drops.set(leafDrops); - } - - @Override - public List getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) { - List ret = drops.get(); - drops.remove(); - - // leaves not harvested, get drops normally - if (ret == null) { - ret = getLeafDrop(world, null, pos, 1.0f, fortune); - } - - return ret; - } - - private static List getLeafDrop(IBlockAccess world, @Nullable GameProfile playerProfile, BlockPos pos, float saplingModifier, int fortune) { - List prod = new ArrayList<>(); + protected NonNullList getLeafDrop(World world, @Nullable GameProfile playerProfile, BlockPos pos, float saplingModifier, int fortune) { + NonNullList prod = NonNullList.create(); TileLeaves tile = TileUtil.getTile(world, pos, TileLeaves.class); if (tile == null) { @@ -176,7 +140,7 @@ public class BlockForestryLeaves extends BlockLeaves implements ITileEntityProvi } // Add saplings - List saplings = tree.getSaplings((World) world, playerProfile, pos, saplingModifier); + List saplings = tree.getSaplings(world, playerProfile, pos, saplingModifier); for (ITree sapling : saplings) { if (sapling != null) { @@ -186,100 +150,12 @@ public class BlockForestryLeaves extends BlockLeaves implements ITileEntityProvi // Add fruits if (tile.hasFruit()) { - prod.addAll(tree.produceStacks((World) world, pos, tile.getRipeningTime())); + prod.addAll(tree.produceStacks(world, pos, tile.getRipeningTime())); } return prod; } - @Override - public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) { - TileLeaves leaves = TileUtil.getTile(world, pos, TileLeaves.class); - if (leaves == null) { - return ItemStack.EMPTY; - } - - ITree tree = leaves.getTree(); - if (tree == null) { - return ItemStack.EMPTY; - } - - return tree.getGenome().getDecorativeLeaves(); - } - - @Override - public List onSheared(ItemStack item, IBlockAccess world, BlockPos pos, int fortune) { - ITree tree = null; - - TileLeaves leaves = TileUtil.getTile(world, pos, TileLeaves.class); - if (leaves != null) { - tree = leaves.getTree(); - } - - if(tree == null){ - tree = TreeDefinition.Oak.getIndividual(); - } - - ItemStack decorativeLeaves = tree.getGenome().getDecorativeLeaves(); - if (decorativeLeaves.isEmpty()) { - return Collections.emptyList(); - } else { - return Collections.singletonList(decorativeLeaves); - } - } - - @Nullable - @Override - public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) { - TileLeaves tileLeaves = TileUtil.getTile(worldIn, pos, TileLeaves.class); - if (tileLeaves != null && TreeDefinition.Willow.getUID().equals(tileLeaves.getSpeciesUID())) { - return null; - } - return super.getCollisionBoundingBox(blockState, worldIn, pos); - } - - @Override - public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) { - super.onEntityCollidedWithBlock(worldIn, pos, state, entityIn); - entityIn.motionX *= 0.4D; - entityIn.motionZ *= 0.4D; - } - - @Override - public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) { - LeafDecayHelper.leafDecay(this, world, pos); - - TileLeaves tileLeaves = TileUtil.getTile(world, pos, TileLeaves.class); - - // check leaves tile because they might have decayed - if (tileLeaves != null && !tileLeaves.isInvalid() && rand.nextFloat() <= 0.1) { - tileLeaves.onBlockTick(world, pos, state, rand); - } - } - - /* RENDERING */ - @Override - public boolean isOpaqueCube(IBlockState state) { - return !Proxies.render.fancyGraphicsEnabled(); - } - - @SideOnly(Side.CLIENT) - @Override - public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) { - return (Proxies.render.fancyGraphicsEnabled() || blockAccess.getBlockState(pos.offset(side)).getBlock() != this) && BlockUtil.shouldSideBeRendered(blockState, blockAccess, pos, side); - } - - @Override - @SideOnly(Side.CLIENT) - public BlockRenderLayer getBlockLayer() { - return BlockRenderLayer.CUTOUT_MIPPED; // fruit overlays require CUTOUT_MIPPED, even in Fast graphics - } - - @Override - public BlockPlanks.EnumType getWoodType(int meta) { - return BlockPlanks.EnumType.OAK; - } - /* MODELS */ @Override @SideOnly(Side.CLIENT) @@ -287,28 +163,6 @@ public class BlockForestryLeaves extends BlockLeaves implements ITileEntityProvi ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation("forestry:leaves", "inventory")); } - /* PROPERTIES */ - @Override - public int getFlammability(IBlockAccess world, BlockPos pos, EnumFacing face) { - return 60; - } - - @Override - public boolean isFlammable(IBlockAccess world, BlockPos pos, EnumFacing face) { - return true; - } - - @Override - public int getFireSpreadSpeed(IBlockAccess world, BlockPos pos, EnumFacing face) { - if (face == EnumFacing.DOWN) { - return 20; - } else if (face != EnumFacing.UP) { - return 10; - } else { - return 5; - } - } - @Override public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { TileLeaves leaves = TileUtil.getTile(worldIn, pos, TileLeaves.class); diff --git a/src/main/java/forestry/arboriculture/blocks/BlockRegistryArboriculture.java b/src/main/java/forestry/arboriculture/blocks/BlockRegistryArboriculture.java index 1ae7514ef..f69293775 100644 --- a/src/main/java/forestry/arboriculture/blocks/BlockRegistryArboriculture.java +++ b/src/main/java/forestry/arboriculture/blocks/BlockRegistryArboriculture.java @@ -15,6 +15,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; + +import com.google.common.base.Preconditions; import forestry.api.arboriculture.EnumForestryWoodType; import forestry.api.arboriculture.EnumVanillaWoodType; import forestry.api.arboriculture.IAlleleFruit; @@ -60,6 +62,8 @@ public class BlockRegistryArboriculture extends BlockRegistry { public final BlockSapling saplingGE; public final BlockForestryLeaves leaves; + public final List leavesDefault; + public final Map speciesToLeavesDefault; public final List leavesDecorative; private final Map speciesToLeavesDecorative; public final Map podsMap; @@ -236,6 +240,20 @@ public class BlockRegistryArboriculture extends BlockRegistry { registerBlock(leaves, new ItemBlockLeaves(leaves), "leaves"); registerOreDictWildcard(OreDictUtil.TREE_LEAVES, leaves); + leavesDefault = BlockDefaultLeaves.create(); + speciesToLeavesDefault = new HashMap<>(); + for (BlockDefaultLeaves leaves : leavesDefault) { + registerBlock(leaves, new ItemBlockLeaves(leaves), "leaves.default." + leaves.getBlockNumber()); + registerOreDictWildcard(OreDictUtil.TREE_LEAVES, leaves); + + for (IBlockState state : leaves.getBlockState().getValidStates()) { + TreeDefinition treeDefinition = leaves.getTreeDefinition(state); + Preconditions.checkNotNull(treeDefinition); + String speciesUid = treeDefinition.getUID(); + speciesToLeavesDefault.put(speciesUid, state); + } + } + leavesDecorative = BlockDecorativeLeaves.create(); speciesToLeavesDecorative = new HashMap<>(); for (BlockDecorativeLeaves leaves : leavesDecorative) { @@ -282,6 +300,10 @@ public class BlockRegistryArboriculture extends BlockRegistry { return itemStack.copy(); } + public IBlockState getDefaultLeaves(String speciesUid) { + return speciesToLeavesDefault.get(speciesUid); + } + @Nullable public BlockFruitPod getFruitPod(IAlleleFruit fruit) { return podsMap.get(fruit.getUID()); diff --git a/src/main/java/forestry/arboriculture/blocks/PropertyTreeType.java b/src/main/java/forestry/arboriculture/blocks/PropertyTreeType.java index f577ae4e4..5346cf6aa 100644 --- a/src/main/java/forestry/arboriculture/blocks/PropertyTreeType.java +++ b/src/main/java/forestry/arboriculture/blocks/PropertyTreeType.java @@ -32,5 +32,7 @@ public class PropertyTreeType extends PropertyEnum { super(name, valueClass, allowedValues); } - + public TreeDefinition getFirstType() { + return getAllowedValues().iterator().next(); + } } diff --git a/src/main/java/forestry/arboriculture/blocks/PropertyWoodType.java b/src/main/java/forestry/arboriculture/blocks/PropertyWoodType.java index b739da196..03cb91ba3 100644 --- a/src/main/java/forestry/arboriculture/blocks/PropertyWoodType.java +++ b/src/main/java/forestry/arboriculture/blocks/PropertyWoodType.java @@ -23,9 +23,4 @@ public abstract class PropertyWoodType & IWoodType> extends Pr public T getFirstType() { return getAllowedValues().iterator().next(); } - - @Override - public Collection getAllowedValues() { - return super.getAllowedValues(); - } } diff --git a/src/main/java/forestry/arboriculture/genetics/TreeDefinition.java b/src/main/java/forestry/arboriculture/genetics/TreeDefinition.java index 5aa4cc5b3..ea9e8f840 100644 --- a/src/main/java/forestry/arboriculture/genetics/TreeDefinition.java +++ b/src/main/java/forestry/arboriculture/genetics/TreeDefinition.java @@ -996,30 +996,31 @@ public enum TreeDefinition implements ITreeDefinition, ITreeGenerator, IStringSe @Override public boolean setLeaves(ITreeGenome genome, World world, @Nullable GameProfile owner, BlockPos pos) { - boolean placed = world.setBlockState(pos, PluginArboriculture.getBlocks().leaves.getDefaultState()); - if (!placed) { - return false; - } + if (owner == null && genome.matchesTemplateGenome()) { + String speciesUid = genome.getPrimary().getUID(); + IBlockState defaultLeaves = PluginArboriculture.getBlocks().getDefaultLeaves(speciesUid); + return world.setBlockState(pos, defaultLeaves); + } else { + IBlockState leaves = PluginArboriculture.getBlocks().leaves.getDefaultState(); + boolean placed = world.setBlockState(pos, leaves); + if (!placed) { + return false; + } - Block block = world.getBlockState(pos).getBlock(); - if (PluginArboriculture.getBlocks().leaves != block) { - world.setBlockToAir(pos); - return false; - } + TileLeaves tileLeaves = TileUtil.getTile(world, pos, TileLeaves.class); + if (tileLeaves == null) { + world.setBlockToAir(pos); + return false; + } - TileLeaves tileLeaves = TileUtil.getTile(world, pos, TileLeaves.class); - if (tileLeaves == null) { - world.setBlockToAir(pos); - return false; - } + if (owner != null) { + tileLeaves.getOwnerHandler().setOwner(owner); + } + tileLeaves.setTree(new Tree(genome)); - if (owner != null) { - tileLeaves.getOwnerHandler().setOwner(owner); + world.markBlockRangeForRenderUpdate(pos, pos); + return true; } - tileLeaves.setTree(new Tree(genome)); - - world.markBlockRangeForRenderUpdate(pos, pos); - return true; } public static void initTrees() { diff --git a/src/main/java/forestry/arboriculture/genetics/TreeGenome.java b/src/main/java/forestry/arboriculture/genetics/TreeGenome.java index 629ff602f..af6f09381 100755 --- a/src/main/java/forestry/arboriculture/genetics/TreeGenome.java +++ b/src/main/java/forestry/arboriculture/genetics/TreeGenome.java @@ -10,6 +10,8 @@ ******************************************************************************/ package forestry.arboriculture.genetics; +import javax.annotation.Nullable; + import com.google.common.base.Preconditions; import forestry.api.arboriculture.EnumTreeChromosome; import forestry.api.arboriculture.IAlleleFruit; @@ -18,6 +20,7 @@ import forestry.api.arboriculture.IAlleleTreeSpecies; import forestry.api.arboriculture.IFruitProvider; import forestry.api.arboriculture.ITreeGenome; import forestry.api.arboriculture.TreeManager; +import forestry.api.genetics.IAllele; import forestry.api.genetics.IAlleleFloat; import forestry.api.genetics.IAlleleInteger; import forestry.api.genetics.IAlleleSpecies; @@ -28,6 +31,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; public class TreeGenome extends Genome implements ITreeGenome { + @Nullable + private Boolean matchesTemplateCached; public TreeGenome(IChromosome[] chromosomes) { super(chromosomes); @@ -109,4 +114,31 @@ public class TreeGenome extends Genome implements ITreeGenome { public ISpeciesRoot getSpeciesRoot() { return TreeManager.treeRoot; } + + @Override + public boolean matchesTemplateGenome() { + if (matchesTemplateCached == null) { + matchesTemplateCached = calculateMatchesTemplateGenome(); + } + return matchesTemplateCached; + } + + private boolean calculateMatchesTemplateGenome() { + IAlleleTreeSpecies primary = getPrimary(); + IAllele[] template = getSpeciesRoot().getTemplate(primary); + IChromosome[] chromosomes = getChromosomes(); + for (int i = 0; i < chromosomes.length; i++) { + IChromosome chromosome = chromosomes[i]; + String templateUid = template[i].getUID(); + IAllele primaryAllele = chromosome.getPrimaryAllele(); + if (!primaryAllele.getUID().equals(templateUid)) { + return false; + } + IAllele secondaryAllele = chromosome.getSecondaryAllele(); + if (!secondaryAllele.getUID().equals(templateUid)) { + return false; + } + } + return true; + } } diff --git a/src/main/java/forestry/arboriculture/items/ItemBlockLeaves.java b/src/main/java/forestry/arboriculture/items/ItemBlockLeaves.java index dcd049009..ccadd67f9 100644 --- a/src/main/java/forestry/arboriculture/items/ItemBlockLeaves.java +++ b/src/main/java/forestry/arboriculture/items/ItemBlockLeaves.java @@ -11,6 +11,7 @@ package forestry.arboriculture.items; import forestry.arboriculture.PluginArboriculture; +import forestry.arboriculture.blocks.BlockAbstractLeaves; import forestry.arboriculture.blocks.BlockForestryLeaves; import forestry.arboriculture.tiles.TileLeaves; import forestry.core.items.IColoredItem; @@ -27,7 +28,7 @@ import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class ItemBlockLeaves extends ItemBlockForestry implements IColoredItem { +public class ItemBlockLeaves extends ItemBlockForestry implements IColoredItem { public ItemBlockLeaves(Block block) { super(block); diff --git a/src/main/java/forestry/arboriculture/models/ModelDefaultLeaves.java b/src/main/java/forestry/arboriculture/models/ModelDefaultLeaves.java new file mode 100644 index 000000000..7bfa74916 --- /dev/null +++ b/src/main/java/forestry/arboriculture/models/ModelDefaultLeaves.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.arboriculture.models; + +import com.google.common.base.Preconditions; +import forestry.api.arboriculture.IAlleleTreeSpecies; +import forestry.api.arboriculture.ILeafSpriteProvider; +import forestry.api.arboriculture.ITreeGenome; +import forestry.api.core.IModelBaker; +import forestry.arboriculture.blocks.BlockDefaultLeaves; +import forestry.arboriculture.genetics.TreeDefinition; +import forestry.core.models.ModelBlockCached; +import forestry.core.models.baker.ModelBaker; +import forestry.core.proxy.Proxies; +import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.block.model.IBakedModel; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public class ModelDefaultLeaves extends ModelBlockCached { + public ModelDefaultLeaves() { + super(BlockDefaultLeaves.class); + } + + @Override + protected TreeDefinition getInventoryKey(ItemStack stack) { + Block block = Block.getBlockFromItem(stack.getItem()); + Preconditions.checkArgument(block instanceof BlockDefaultLeaves, "ItemStack must be for default leaves."); + BlockDefaultLeaves bBlock = (BlockDefaultLeaves) block; + return bBlock.getTreeType(stack.getMetadata()); + } + + @Override + protected TreeDefinition getWorldKey(IBlockState state) { + Block block = state.getBlock(); + Preconditions.checkArgument(block instanceof BlockDefaultLeaves, "state must be for default leaves."); + BlockDefaultLeaves bBlock = (BlockDefaultLeaves) block; + TreeDefinition treeDefinition = bBlock.getTreeDefinition(state); + Preconditions.checkNotNull(treeDefinition); + return treeDefinition; + } + + @Override + protected void bakeBlock(BlockDefaultLeaves block, TreeDefinition treeDefinition, IModelBaker baker, boolean inventory) { + TextureMap map = Minecraft.getMinecraft().getTextureMapBlocks(); + + ITreeGenome genome = treeDefinition.getGenome(); + IAlleleTreeSpecies species = genome.getPrimary(); + ILeafSpriteProvider leafSpriteProvider = species.getLeafSpriteProvider(); + + ResourceLocation leafSpriteLocation = leafSpriteProvider.getSprite(false, Proxies.render.fancyGraphicsEnabled()); + TextureAtlasSprite leafSprite = map.getAtlasSprite(leafSpriteLocation.toString()); + + // Render the plain leaf block. + baker.addBlockModel(null, leafSprite, 0); + + // Render overlay for fruit leaves. + ResourceLocation fruitSpriteLocation = genome.getFruitProvider().getDecorativeSprite(); + if (fruitSpriteLocation != null) { + TextureAtlasSprite fruitSprite = map.getAtlasSprite(fruitSpriteLocation.toString()); + baker.addBlockModel(null, fruitSprite, 1); + } + + // Set the particle sprite + baker.setParticleSprite(leafSprite); + } + + @Override + protected IBakedModel bakeModel(IBlockState state, TreeDefinition key, BlockDefaultLeaves block) { + IModelBaker baker = new ModelBaker(); + + bakeBlock(block, key, baker, false); + + blockModel = baker.bakeModel(false); + onCreateModel(blockModel); + return blockModel; + } +} diff --git a/src/main/java/forestry/arboriculture/proxy/ProxyArboricultureClient.java b/src/main/java/forestry/arboriculture/proxy/ProxyArboricultureClient.java index da68493ce..876ef560f 100644 --- a/src/main/java/forestry/arboriculture/proxy/ProxyArboricultureClient.java +++ b/src/main/java/forestry/arboriculture/proxy/ProxyArboricultureClient.java @@ -24,7 +24,9 @@ import forestry.arboriculture.IWoodTyped; import forestry.arboriculture.PluginArboriculture; import forestry.arboriculture.blocks.BlockArbSlab; import forestry.arboriculture.blocks.BlockDecorativeLeaves; +import forestry.arboriculture.blocks.BlockDefaultLeaves; import forestry.arboriculture.models.ModelDecorativeLeaves; +import forestry.arboriculture.models.ModelDefaultLeaves; import forestry.arboriculture.models.ModelLeaves; import forestry.arboriculture.models.MultipartModel; import forestry.arboriculture.models.WoodModelLoader; @@ -75,6 +77,15 @@ public class ProxyArboricultureClient extends ProxyArboriculture { ModelManager.getInstance().registerCustomBlockModel(blockModelIndex); } + for (BlockDefaultLeaves leaves : PluginArboriculture.getBlocks().leavesDefault) { + String resourceName = "forestry:leaves.default." + leaves.getBlockNumber(); + ModelResourceLocation blockModelLocation = new ModelResourceLocation(resourceName); + ModelResourceLocation itemModeLocation = new ModelResourceLocation(resourceName, "inventory"); + BlockModelEntry blockModelIndex = new BlockModelEntry(blockModelLocation, itemModeLocation, + new ModelDefaultLeaves(), leaves); + ModelManager.getInstance().registerCustomBlockModel(blockModelIndex); + } + ModelLoaderRegistry.registerLoader(WoodModelLoader.INSTANCE); for (BlockArbSlab slab : PluginArboriculture.getBlocks().slabsDouble) { registerWoodModel(slab, true); diff --git a/src/main/java/forestry/arboriculture/tiles/TileLeaves.java b/src/main/java/forestry/arboriculture/tiles/TileLeaves.java index 68d0b2823..07ff86226 100644 --- a/src/main/java/forestry/arboriculture/tiles/TileLeaves.java +++ b/src/main/java/forestry/arboriculture/tiles/TileLeaves.java @@ -38,6 +38,7 @@ import forestry.api.lepidopterology.ButterflyManager; import forestry.api.lepidopterology.IButterfly; import forestry.api.lepidopterology.IButterflyGenome; import forestry.api.lepidopterology.IButterflyNursery; +import forestry.arboriculture.PluginArboriculture; import forestry.arboriculture.genetics.TreeDefinition; import forestry.arboriculture.genetics.alleles.AlleleFruits; import forestry.arboriculture.network.IRipeningPacketReceiver; @@ -47,6 +48,7 @@ import forestry.core.network.packets.PacketTileStream; import forestry.core.utils.ClimateUtil; import forestry.core.utils.ColourUtil; import forestry.core.utils.GeneticsUtil; +import forestry.core.utils.Log; import forestry.core.utils.NetworkUtil; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; @@ -82,6 +84,13 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru private IEffectData effectData[] = new IEffectData[2]; + /** + * Worldgen trees used to create TileLeaves, but now they use BlockDefaultLeaves instead. + * We add a check to convert the TileLeaves into the new format. + * This boolean keeps track of whether this leaf has checked if it should replace itself. + */ + private boolean checkedForConversionToDefaultLeaves; + /* SAVING & LOADING */ @Override public void readFromNBT(NBTTagCompound nbttagcompound) { @@ -127,9 +136,19 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru } ITreeGenome genome = tree.getGenome(); + IAlleleTreeSpecies primary = genome.getPrimary(); + + if (!checkedForConversionToDefaultLeaves) { + if (shouldConvertToDefaultLeaves()) { + IBlockState defaultLeaves = PluginArboriculture.getBlocks().getDefaultLeaves(primary.getUID()); + worldIn.setBlockState(getPos(), defaultLeaves); + return; + } + checkedForConversionToDefaultLeaves = true; + } boolean isDestroyed = isDestroyed(tree, damage); - for (ILeafTickHandler tickHandler : genome.getPrimary().getRoot().getLeafTickHandlers()) { + for (ILeafTickHandler tickHandler : primary.getRoot().getLeafTickHandlers()) { if (tickHandler.onRandomLeafTick(tree, world, rand, getPos(), isDestroyed)) { return; } @@ -161,6 +180,19 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru effectData = tree.doEffect(effectData, world, getPos()); } + /** + * Worldgen trees with default genomes should be converted to leaves without tile entities. + */ + private boolean shouldConvertToDefaultLeaves() { + if (getOwnerHandler().getOwner() == null) { // null owner likely means it's a worldgen tree + ITree tree = getTree(); + if (tree != null && tree.getGenome().matchesTemplateGenome()) { + return true; + } + } + return false; + } + @Override public void setTree(ITree tree) { super.setTree(tree);