Skip to content

Commit

Permalink
Merge pull request #135 from GrowthcraftCE/feature/improved-apple-tree
Browse files Browse the repository at this point in the history
Improved apple tree
  • Loading branch information
Avatair authored Jan 21, 2019
2 parents d409c61 + 498a153 commit 52b3e0d
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 122 deletions.
2 changes: 2 additions & 0 deletions src/main/java/growthcraft/apples/GrowthcraftApples.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void registerBlocks(RegistryEvent.Register<Block> event)

Init.registerBlocks(registry);
Init.registerFluidBlocks(registry);

proxy.registerStateMappers();
}

@SubscribeEvent
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/growthcraft/apples/client/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class ClientProxy extends CommonProxy {
@Override
public void preInit() {
super.preInit();
Init.setCustomBlockStateMappers();
}

@Override
Expand All @@ -33,5 +32,10 @@ public void registerModelBakeryVariants() {

public void registerSpecialRenders() {
}

@Override
public void registerStateMappers() {
Init.setCustomBlockStateMappers();
}

}

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/growthcraft/apples/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ public void registerTileEntities() {

public void postRegisterItems() {
}

public void registerStateMappers() {
}

}
56 changes: 45 additions & 11 deletions src/main/java/growthcraft/apples/common/Init.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package growthcraft.apples.common;

import growthcraft.apples.client.handlers.GrowthcraftApplesColorHandler;
import growthcraft.apples.common.block.BlockApple;
import growthcraft.apples.common.block.BlockAppleDoor;
import growthcraft.apples.common.block.BlockAppleFence;
Expand All @@ -13,6 +12,7 @@
import growthcraft.apples.common.block.BlockAppleSlabHalf;
import growthcraft.apples.common.block.BlockAppleStairs;
import growthcraft.apples.common.item.ItemAppleDoor;
import growthcraft.apples.common.item.ItemAppleLeaves;
import growthcraft.apples.shared.Reference;
import growthcraft.apples.shared.config.GrowthcraftApplesConfig;
import growthcraft.apples.shared.init.GrowthcraftApplesBlocks;
Expand All @@ -38,19 +38,28 @@
import growthcraft.core.shared.effect.EffectWeightedRandomList;
import growthcraft.core.shared.effect.SimplePotionEffectFactory;
import growthcraft.core.shared.item.OreItemStacks;
import growthcraft.core.shared.utils.ColorUtils;
import growthcraft.core.shared.utils.TickUtils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFenceGate;
import net.minecraft.block.BlockSlab;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.statemap.StateMap;
import net.minecraft.client.renderer.color.BlockColors;
import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.client.renderer.color.ItemColors;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemSlab;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ColorizerFoliage;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.biome.BiomeColorHelper;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.relauncher.Side;
Expand All @@ -59,7 +68,6 @@
import net.minecraftforge.registries.IForgeRegistry;

import static growthcraft.core.shared.GrowthcraftCoreApis.tabGrowthcraft;
import static growthcraft.grapes.shared.init.GrowthcraftGrapesFluids.grapeWineBooze;
import static growthcraft.apples.shared.init.GrowthcraftApplesFluids.appleCiderBooze;

public class Init {
Expand All @@ -74,7 +82,7 @@ public static void preInitBlocks() {
GrowthcraftApplesBlocks.blockAppleDoor = new BlockDefinition( new BlockAppleDoor("apple_door") );
GrowthcraftApplesBlocks.blockAppleFence = new BlockDefinition( new BlockAppleFence("apple_fence") );
GrowthcraftApplesBlocks.blockAppleFenceGate = new BlockDefinition( new BlockAppleFenceGate("apple_fence_gate") );
GrowthcraftApplesBlocks.blockAppleLeaves = new BlockDefinition( new BlockAppleLeaves("apple_leaves") );
GrowthcraftApplesBlocks.blockAppleLeaves = new BlockTypeDefinition<BlockAppleLeaves>( new BlockAppleLeaves("apple_leaves") );
GrowthcraftApplesBlocks.blockAppleLog = new BlockDefinition( new BlockAppleLog("apple_log") );
GrowthcraftApplesBlocks.blockApplePlanks = new BlockDefinition( new BlockApplePlanks("apple_planks") );
GrowthcraftApplesBlocks.blockAppleSapling = new BlockDefinition( new BlockAppleSapling("apple_sapling") );
Expand Down Expand Up @@ -113,7 +121,8 @@ public static void registerBlocks(IForgeRegistry<Block> registry) {
public static void registerBlockItems(IForgeRegistry<Item> registry) {
GrowthcraftApplesBlocks.blockAppleFence.registerBlockItem(registry);
GrowthcraftApplesBlocks.blockAppleFenceGate.registerBlockItem(registry);
GrowthcraftApplesBlocks.blockAppleLeaves.registerBlockItem(registry);
GrowthcraftApplesBlocks.blockAppleLeaves.registerBlockItem(registry,
new ItemAppleLeaves(GrowthcraftApplesBlocks.blockAppleLeaves.getBlock()));
GrowthcraftApplesBlocks.blockAppleLog.registerBlockItem(registry);
GrowthcraftApplesBlocks.blockApplePlanks.registerBlockItem(registry);
GrowthcraftApplesBlocks.blockAppleSapling.registerBlockItem(registry);
Expand Down Expand Up @@ -141,23 +150,36 @@ public static void registerBlockRenders() {

@SideOnly(Side.CLIENT)
public static void registerBlockColorHandlers() {
registerBlockColorHandler(GrowthcraftApplesBlocks.blockAppleLeaves.getBlock());
BlockColors blockColors = Minecraft.getMinecraft().getBlockColors();
blockColors.registerBlockColorHandler(
(state, worldIn, pos, tintindex) -> {
float[] color = ColorUtils.getFloat4ARGB(BlockAppleLeaves.LEAVES_COLOR);
if( worldIn != null && pos != null ) {
float[] baseColor = ColorUtils.getFloat4ARGB(ColorizerFoliage.getFoliageColorBasic());
float[] curColor = ColorUtils.getFloat4ARGB(BiomeColorHelper.getFoliageColorAtPos(worldIn, pos));

float[] colorRatio = new float[3];
for( int i = 1; i < 3; i ++ )
colorRatio[i - 1] = curColor[i] / baseColor[i]; // (curColor[i] - baseColor[i]) / baseColor[i] + 1.0f;

for( int i = 1; i < 3; i ++ )
color[i] *= colorRatio[i - 1];
}

return ColorUtils.getIntARGB(color);
},
GrowthcraftApplesBlocks.blockAppleLeaves.getBlock());
}

/*
* Credit to CJMinecraft for identifying how to ignore properties.
*/
@SideOnly(Side.CLIENT)
public static void setCustomBlockStateMappers() {
ModelLoader.setCustomStateMapper(GrowthcraftApplesBlocks.blockAppleLeaves.getBlock(), (new StateMap.Builder().ignore(BlockAppleLeaves.DECAYABLE, BlockAppleLeaves.CHECK_DECAY)).build());
ModelLoader.setCustomStateMapper(GrowthcraftApplesBlocks.blockAppleFenceGate.getBlock(), (new StateMap.Builder().ignore(BlockFenceGate.POWERED)).build());
}

@SideOnly(Side.CLIENT)
public static void registerBlockColorHandler(Block block) {
BlockColors blockColors = Minecraft.getMinecraft().getBlockColors();
blockColors.registerBlockColorHandler(new GrowthcraftApplesColorHandler(), block);
}


////////
// Items
Expand Down Expand Up @@ -193,6 +215,18 @@ public static void registerItemRenders() {
@SideOnly(Side.CLIENT)
public static void registerItemColorHandlers() {
ItemRenderUtils.registerItemColorHandler(GrowthcraftApplesItems.appleCider.getItem());

// TODO: Move to core utils
BlockColors blockColors = Minecraft.getMinecraft().getBlockColors();
ItemColors itemColors = Minecraft.getMinecraft().getItemColors();
itemColors.registerItemColorHandler(new IItemColor()
{
public int colorMultiplier(ItemStack stack, int tintIndex)
{
IBlockState iblockstate = ((ItemBlock)stack.getItem()).getBlock().getStateFromMeta(stack.getMetadata());
return blockColors.colorMultiplier(iblockstate, (IBlockAccess)null, (BlockPos)null, tintIndex);
}
}, GrowthcraftApplesBlocks.blockAppleLeaves.getBlock());
}

@SideOnly(Side.CLIENT)
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/growthcraft/apples/common/block/BlockApple.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.IGrowable;
import net.minecraft.block.SoundType;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -31,6 +32,10 @@

public class BlockApple extends BlockBush implements IGrowable {

// TODO: Make fields configurable
public static final int CHANCE_GROWTH = 10;
public static final int CHANCE_TO_FALL = 0; // CHANCE_GROWTH * 6; // NOTE: Must be approximately: "maximal production rate" * CHANCE_GROWTH

public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 7);

private static final AxisAlignedBB[] BOUNDING_BOXES = new AxisAlignedBB[]{
Expand All @@ -49,6 +54,7 @@ public BlockApple(String unlocalizedName) {
this.setUnlocalizedName(unlocalizedName);
this.setRegistryName(new ResourceLocation(Reference.MODID, unlocalizedName));
this.setTickRandomly(true);
this.setSoundType(SoundType.WOOD);
}

@Override
Expand Down Expand Up @@ -90,11 +96,20 @@ public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, Bloc
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
super.updateTick(worldIn, pos, state, rand);
if ( worldIn.getLightFromNeighbors(pos.up()) >= 9) {
if( ForgeHooks.onCropsGrowPre(worldIn, pos, state, rand.nextInt(7) == 0) ) {
grow(worldIn, rand, pos, state);
ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}

if( worldIn.isRemote )
return;

if( ForgeHooks.onCropsGrowPre(worldIn, pos, state, rand.nextInt(CHANCE_GROWTH) == 0) ) {
grow(worldIn, rand, pos, state);
ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}

if( CHANCE_TO_FALL > 0 ) {
if( this.getAge(state) == 7 && rand.nextInt(CHANCE_TO_FALL) == 0 ) {
this.dropBlockAsItem(worldIn, pos, state, 0);
worldIn.setBlockToAir(pos);
}
}
}

Expand All @@ -112,7 +127,7 @@ public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockSt
public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state) {
super.updateTick(worldIn, pos, state, rand);
// If we have enough light there is a 25% chance of growth to the next stage
if ( worldIn.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(1) == 0) {
if ( worldIn.getLightFromNeighbors(pos.up()) >= 9 ) {
// If the apple isn't full grown
if ( this.getAge(state) != 7) {
// Then increment the age.
Expand All @@ -133,6 +148,16 @@ public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state) {
Block block = worldIn.getBlockState(pos.up()).getBlock();
return block instanceof BlockAppleLeaves;
}

@SuppressWarnings("deprecation")
@Override
public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos)
{
if (!this.canBlockStay(worldIn, pos, state))
{
worldIn.destroyBlock(pos, false);
}
}

private int getAge(IBlockState state) {
return state.getValue(AGE).intValue();
Expand All @@ -152,7 +177,11 @@ public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state,

@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune) {
ItemStack appleStack = new ItemStack(Items.APPLE, 1);
return appleStack.getItem();
if ( this.getAge(state) == 7) {
ItemStack appleStack = new ItemStack(Items.APPLE, 1);
return appleStack.getItem();
}

return Items.AIR;
}
}
Loading

0 comments on commit 52b3e0d

Please sign in to comment.