Skip to content

Commit

Permalink
Fix leaves not supporting fast rendering. Closes #418
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbegt committed Dec 31, 2017
1 parent 45838ad commit 4ad19f8
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 172 deletions.
@@ -0,0 +1,305 @@
package com.progwml6.natura.common.block.base;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.IShearable;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public abstract class BlockLeavesBase extends Block implements IShearable
{
public static final PropertyBool DECAYABLE = PropertyBool.create("decayable");

public static final PropertyBool CHECK_DECAY = PropertyBool.create("check_decay");

int[] surroundings;

public BlockLeavesBase()
{
super(Material.LEAVES);

this.setTickRandomly(true);
this.setCreativeTab(CreativeTabs.DECORATIONS);
this.setHardness(0.2F);
this.setLightOpacity(1);
this.setSoundType(SoundType.PLANT);
}

/**
* Called serverside after this block is replaced with another in Chunk, but before the Tile Entity is updated
*/
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
int k = pos.getX();
int l = pos.getY();
int i1 = pos.getZ();

if (worldIn.isAreaLoaded(new BlockPos(k - 2, l - 2, i1 - 2), new BlockPos(k + 2, l + 2, i1 + 2)))
{
for (int j1 = -1; j1 <= 1; ++j1)
{
for (int k1 = -1; k1 <= 1; ++k1)
{
for (int l1 = -1; l1 <= 1; ++l1)
{
BlockPos blockpos = pos.add(j1, k1, l1);
IBlockState iblockstate = worldIn.getBlockState(blockpos);

if (iblockstate.getBlock().isLeaves(iblockstate, worldIn, blockpos))
{
iblockstate.getBlock().beginLeavesDecay(iblockstate, worldIn, blockpos);
}
}
}
}
}
}

@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (!worldIn.isRemote)
{
if (state.getValue(CHECK_DECAY).booleanValue() && state.getValue(DECAYABLE).booleanValue())
{
int k = pos.getX();
int l = pos.getY();
int i1 = pos.getZ();

if (this.surroundings == null)
{
this.surroundings = new int[32768];
}

if (worldIn.isAreaLoaded(new BlockPos(k - 5, l - 5, i1 - 5), new BlockPos(k + 5, l + 5, i1 + 5)))
{
BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();

for (int i2 = -4; i2 <= 4; ++i2)
{
for (int j2 = -4; j2 <= 4; ++j2)
{
for (int k2 = -4; k2 <= 4; ++k2)
{
IBlockState iblockstate = worldIn.getBlockState(blockpos$mutableblockpos.setPos(k + i2, l + j2, i1 + k2));
Block block = iblockstate.getBlock();

if (!block.canSustainLeaves(iblockstate, worldIn, blockpos$mutableblockpos.setPos(k + i2, l + j2, i1 + k2)))
{
if (block.isLeaves(iblockstate, worldIn, blockpos$mutableblockpos.setPos(k + i2, l + j2, i1 + k2)))
{
this.surroundings[(i2 + 16) * 1024 + (j2 + 16) * 32 + k2 + 16] = -2;
}
else
{
this.surroundings[(i2 + 16) * 1024 + (j2 + 16) * 32 + k2 + 16] = -1;
}
}
else
{
this.surroundings[(i2 + 16) * 1024 + (j2 + 16) * 32 + k2 + 16] = 0;
}
}
}
}

for (int i3 = 1; i3 <= 4; ++i3)
{
for (int j3 = -4; j3 <= 4; ++j3)
{
for (int k3 = -4; k3 <= 4; ++k3)
{
for (int l3 = -4; l3 <= 4; ++l3)
{
if (this.surroundings[(j3 + 16) * 1024 + (k3 + 16) * 32 + l3 + 16] == i3 - 1)
{
if (this.surroundings[(j3 + 16 - 1) * 1024 + (k3 + 16) * 32 + l3 + 16] == -2)
{
this.surroundings[(j3 + 16 - 1) * 1024 + (k3 + 16) * 32 + l3 + 16] = i3;
}

if (this.surroundings[(j3 + 16 + 1) * 1024 + (k3 + 16) * 32 + l3 + 16] == -2)
{
this.surroundings[(j3 + 16 + 1) * 1024 + (k3 + 16) * 32 + l3 + 16] = i3;
}

if (this.surroundings[(j3 + 16) * 1024 + (k3 + 16 - 1) * 32 + l3 + 16] == -2)
{
this.surroundings[(j3 + 16) * 1024 + (k3 + 16 - 1) * 32 + l3 + 16] = i3;
}

if (this.surroundings[(j3 + 16) * 1024 + (k3 + 16 + 1) * 32 + l3 + 16] == -2)
{
this.surroundings[(j3 + 16) * 1024 + (k3 + 16 + 1) * 32 + l3 + 16] = i3;
}

if (this.surroundings[(j3 + 16) * 1024 + (k3 + 16) * 32 + (l3 + 16 - 1)] == -2)
{
this.surroundings[(j3 + 16) * 1024 + (k3 + 16) * 32 + (l3 + 16 - 1)] = i3;
}

if (this.surroundings[(j3 + 16) * 1024 + (k3 + 16) * 32 + l3 + 16 + 1] == -2)
{
this.surroundings[(j3 + 16) * 1024 + (k3 + 16) * 32 + l3 + 16 + 1] = i3;
}
}
}
}
}
}
}

int l2 = this.surroundings[16912];

if (l2 >= 0)
{
worldIn.setBlockState(pos, state.withProperty(CHECK_DECAY, Boolean.valueOf(false)), 4);
}
else
{
this.destroy(worldIn, pos);
}
}
}
}

private void destroy(World worldIn, BlockPos pos)
{
this.dropBlockAsItem(worldIn, pos, worldIn.getBlockState(pos), 0);
worldIn.setBlockToAir(pos);
}

@SuppressWarnings("deprecation")
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
{
if (worldIn.isRainingAt(pos.up()) && !worldIn.getBlockState(pos.down()).isTopSolid() && rand.nextInt(15) == 1)
{
double d0 = pos.getX() + rand.nextFloat();
double d1 = pos.getY() - 0.05D;
double d2 = pos.getZ() + rand.nextFloat();
worldIn.spawnParticle(EnumParticleTypes.DRIP_WATER, d0, d1, d2, 0.0D, 0.0D, 0.0D);
}
}

/**
* Returns the quantity of items to drop on block destruction.
*/
@Override
public int quantityDropped(Random random)
{
return random.nextInt(20) == 0 ? 1 : 0;
}

/**
* Get the Item that this Block should drop when harvested.
*/
@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune)
{
return Item.getItemFromBlock(Blocks.SAPLING);
}

/**
* Spawns this Block's drops into the World as EntityItems.
*/
@Override
public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
{
super.dropBlockAsItemWithChance(worldIn, pos, state, chance, fortune);
}

protected void dropApple(World worldIn, BlockPos pos, IBlockState state, int chance)
{
}

protected int getSaplingDropChance(IBlockState state)
{
return 20;
}

@Override
public boolean causesSuffocation(IBlockState state)
{
return false;
}

@Override
public boolean isShearable(ItemStack item, IBlockAccess world, BlockPos pos)
{
return true;
}

@Override
public boolean isLeaves(IBlockState state, IBlockAccess world, BlockPos pos)
{
return true;
}

@Override
public void beginLeavesDecay(IBlockState state, World world, BlockPos pos)
{
if (!(Boolean) state.getValue(CHECK_DECAY))
{
world.setBlockState(pos, state.withProperty(CHECK_DECAY, true), 4);
}
}

@Override
public void getDrops(net.minecraft.util.NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
{
Random rand = world instanceof World ? ((World) world).rand : new Random();
int chance = this.getSaplingDropChance(state);

if (fortune > 0)
{
chance -= 2 << fortune;
if (chance < 10)
chance = 10;
}

if (rand.nextInt(chance) == 0)
{
ItemStack drop = new ItemStack(getItemDropped(state, rand, fortune), 1, damageDropped(state));
if (!drop.isEmpty())
drops.add(drop);
}

chance = 200;
if (fortune > 0)
{
chance -= 10 << fortune;
if (chance < 40)
chance = 40;
}

this.captureDrops(true);
if (world instanceof World)
this.dropApple((World) world, pos, state, chance); // Dammet mojang
drops.addAll(this.captureDrops(false));
}

@SuppressWarnings("deprecation")
@Override
@SideOnly(Side.CLIENT)
public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side)
{
return super.shouldSideBeRendered(blockState, blockAccess, pos, side);
}
}
Expand Up @@ -7,6 +7,7 @@

import com.progwml6.natura.common.ClientProxy;
import com.progwml6.natura.common.block.BlockEnumBerryBush;
import com.progwml6.natura.common.block.base.BlockLeavesBase;
import com.progwml6.natura.common.client.LeavesColorizer;
import com.progwml6.natura.library.Util;
import com.progwml6.natura.library.client.state.CustomStateMap;
Expand All @@ -21,7 +22,6 @@

import net.minecraft.block.BlockDoor;
import net.minecraft.block.BlockHopper;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -89,8 +89,8 @@ public int colorMultiplier(@Nonnull ItemStack stack, int tintIndex)
protected void registerModels()
{
// blocks
ModelLoader.setCustomStateMapper(NaturaNether.netherLeaves, (new StateMap.Builder()).ignore(BlockLeaves.CHECK_DECAY, BlockLeaves.DECAYABLE).build());
ModelLoader.setCustomStateMapper(NaturaNether.netherLeaves2, (new StateMap.Builder()).ignore(BlockLeaves.CHECK_DECAY, BlockLeaves.DECAYABLE).build());
ModelLoader.setCustomStateMapper(NaturaNether.netherLeaves, (new StateMap.Builder()).ignore(BlockLeavesBase.CHECK_DECAY, BlockLeavesBase.DECAYABLE).build());
ModelLoader.setCustomStateMapper(NaturaNether.netherLeaves2, (new StateMap.Builder()).ignore(BlockLeavesBase.CHECK_DECAY, BlockLeavesBase.DECAYABLE).build());

ModelLoader.setCustomStateMapper(NaturaNether.netherSapling, (new StateMap.Builder()).ignore(BlockNetherSapling.STAGE, BlockSapling.TYPE).build());
ModelLoader.setCustomStateMapper(NaturaNether.netherSapling2, (new StateMap.Builder()).ignore(BlockNetherSapling2.STAGE, BlockSapling.TYPE).build());
Expand Down

0 comments on commit 4ad19f8

Please sign in to comment.