279 lines
8.7 KiB
Java
Executable file
279 lines
8.7 KiB
Java
Executable file
package game.block;
|
|
|
|
import game.color.DyeColor;
|
|
import game.entity.types.EntityLiving;
|
|
import game.init.Blocks;
|
|
import game.init.Config;
|
|
import game.init.Items;
|
|
import game.item.Item;
|
|
import game.item.ItemStack;
|
|
import game.material.Material;
|
|
import game.model.ModelRotation;
|
|
import game.properties.IProperty;
|
|
import game.properties.PropertyInteger;
|
|
import game.renderer.BlockLayer;
|
|
import game.renderer.blockmodel.ModelBlock;
|
|
import game.rng.Random;
|
|
import game.world.BlockPos;
|
|
import game.world.BoundingBox;
|
|
import game.world.Facing;
|
|
import game.world.IWorldAccess;
|
|
import game.world.State;
|
|
import game.world.World;
|
|
import game.world.WorldServer;
|
|
|
|
public class BlockCocoa extends BlockDirectional implements IGrowable
|
|
{
|
|
public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 2);
|
|
|
|
public BlockCocoa()
|
|
{
|
|
super(Material.plants);
|
|
this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH).withProperty(AGE, Integer.valueOf(0)));
|
|
this.setTickRandomly();
|
|
}
|
|
|
|
public void updateTick(WorldServer worldIn, BlockPos pos, State state, Random rand)
|
|
{
|
|
if (!this.canBlockStay(worldIn, pos, state))
|
|
{
|
|
this.dropBlock(worldIn, pos, state);
|
|
}
|
|
else if (Config.cocoaGrowth > 0 && worldIn.rand.chance(Config.cocoaGrowth))
|
|
{
|
|
int i = ((Integer)state.getValue(AGE)).intValue();
|
|
|
|
if (i < 2)
|
|
{
|
|
worldIn.setState(pos, state.withProperty(AGE, Integer.valueOf(i + 1)), 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean canBlockStay(World worldIn, BlockPos pos, State state)
|
|
{
|
|
pos = pos.offset((Facing)state.getValue(FACING));
|
|
State iblockstate = worldIn.getState(pos);
|
|
return iblockstate.getBlock() == Blocks.jungle_log; // && iblockstate.getValue(BlockPlanks.VARIANT) == BlockPlanks.EnumType.JUNGLE;
|
|
}
|
|
|
|
public boolean isFullCube()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Used to determine ambient occlusion and culling when rebuilding chunks for render
|
|
*/
|
|
public boolean isOpaqueCube()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public BoundingBox getCollisionBoundingBox(World worldIn, BlockPos pos, State state)
|
|
{
|
|
this.setBlockBoundsBasedOnState(worldIn, pos);
|
|
return super.getCollisionBoundingBox(worldIn, pos, state);
|
|
}
|
|
|
|
public BoundingBox getSelectedBoundingBox(World worldIn, BlockPos pos)
|
|
{
|
|
this.setBlockBoundsBasedOnState(worldIn, pos);
|
|
return super.getSelectedBoundingBox(worldIn, pos);
|
|
}
|
|
|
|
|
|
public void setBlockBoundsBasedOnState(IWorldAccess worldIn, BlockPos pos)
|
|
{
|
|
State iblockstate = worldIn.getState(pos);
|
|
Facing enumfacing = (Facing)iblockstate.getValue(FACING);
|
|
int i = ((Integer)iblockstate.getValue(AGE)).intValue();
|
|
int j = 4 + i * 2;
|
|
int k = 5 + i * 2;
|
|
float f = (float)j / 2.0F;
|
|
|
|
switch (enumfacing)
|
|
{
|
|
case SOUTH:
|
|
this.setBlockBounds((8.0F - f) / 16.0F, (12.0F - (float)k) / 16.0F, (15.0F - (float)j) / 16.0F, (8.0F + f) / 16.0F, 0.75F, 0.9375F);
|
|
break;
|
|
|
|
case NORTH:
|
|
this.setBlockBounds((8.0F - f) / 16.0F, (12.0F - (float)k) / 16.0F, 0.0625F, (8.0F + f) / 16.0F, 0.75F, (1.0F + (float)j) / 16.0F);
|
|
break;
|
|
|
|
case WEST:
|
|
this.setBlockBounds(0.0625F, (12.0F - (float)k) / 16.0F, (8.0F - f) / 16.0F, (1.0F + (float)j) / 16.0F, 0.75F, (8.0F + f) / 16.0F);
|
|
break;
|
|
|
|
case EAST:
|
|
this.setBlockBounds((15.0F - (float)j) / 16.0F, (12.0F - (float)k) / 16.0F, (8.0F - f) / 16.0F, 0.9375F, 0.75F, (8.0F + f) / 16.0F);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
|
*/
|
|
public void onBlockPlacedBy(World worldIn, BlockPos pos, State state, EntityLiving placer, ItemStack stack)
|
|
{
|
|
Facing enumfacing = Facing.fromAngle((double)placer.rotYaw);
|
|
worldIn.setState(pos, state.withProperty(FACING, enumfacing), 2);
|
|
}
|
|
|
|
/**
|
|
* Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
|
* IBlockstate
|
|
*/
|
|
public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, int meta, EntityLiving placer)
|
|
{
|
|
if (!facing.getAxis().isHorizontal())
|
|
{
|
|
facing = Facing.NORTH;
|
|
}
|
|
|
|
return this.getState().withProperty(FACING, facing.getOpposite()).withProperty(AGE, Integer.valueOf(0));
|
|
}
|
|
|
|
/**
|
|
* Called when a neighboring block changes.
|
|
*/
|
|
public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
|
|
{
|
|
if (!this.canBlockStay(worldIn, pos, state))
|
|
{
|
|
this.dropBlock(worldIn, pos, state);
|
|
}
|
|
}
|
|
|
|
private void dropBlock(World worldIn, BlockPos pos, State state)
|
|
{
|
|
worldIn.setState(pos, Blocks.air.getState(), 3);
|
|
this.dropBlockAsItem(worldIn, pos, state, 0);
|
|
}
|
|
|
|
/**
|
|
* Spawns this Block's drops into the World as EntityItems.
|
|
*/
|
|
public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, State state, float chance, int fortune)
|
|
{
|
|
int i = ((Integer)state.getValue(AGE)).intValue();
|
|
int j = 1;
|
|
|
|
if (i >= 2)
|
|
{
|
|
j = 3;
|
|
}
|
|
|
|
for (int k = 0; k < j; ++k)
|
|
{
|
|
spawnAsEntity(worldIn, pos, new ItemStack(Items.dye, 1, DyeColor.BROWN.getDyeDamage()));
|
|
}
|
|
}
|
|
|
|
public Item getItem(World worldIn, BlockPos pos)
|
|
{
|
|
return Items.dye;
|
|
}
|
|
|
|
/**
|
|
* Gets the meta to use for the Pick Block ItemStack result
|
|
*/
|
|
public int getDamageValue(World worldIn, BlockPos pos)
|
|
{
|
|
return DyeColor.BROWN.getDyeDamage();
|
|
}
|
|
|
|
/**
|
|
* Whether this IGrowable can grow
|
|
*/
|
|
public boolean canGrow(World worldIn, BlockPos pos, State state, boolean isClient)
|
|
{
|
|
return ((Integer)state.getValue(AGE)).intValue() < 2;
|
|
}
|
|
|
|
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, State state)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void grow(WorldServer worldIn, Random rand, BlockPos pos, State state)
|
|
{
|
|
worldIn.setState(pos, state.withProperty(AGE, Integer.valueOf(((Integer)state.getValue(AGE)).intValue() + 1)), 2);
|
|
}
|
|
|
|
public BlockLayer getBlockLayer()
|
|
{
|
|
return BlockLayer.CUTOUT;
|
|
}
|
|
|
|
/**
|
|
* Convert the given metadata into a BlockState for this Block
|
|
*/
|
|
public State getStateFromMeta(int meta)
|
|
{
|
|
return this.getState().withProperty(FACING, Facing.getHorizontal(meta)).withProperty(AGE, Integer.valueOf((meta & 12) == 12 ? 0 : ((meta & 12) >> 2)));
|
|
}
|
|
|
|
/**
|
|
* Convert the BlockState into the correct metadata value
|
|
*/
|
|
public int getMetaFromState(State state)
|
|
{
|
|
int i = 0;
|
|
i = i | ((Facing)state.getValue(FACING)).getHorizontalIndex();
|
|
i = i | ((Integer)state.getValue(AGE)).intValue() << 2;
|
|
return i;
|
|
}
|
|
|
|
protected IProperty[] getProperties()
|
|
{
|
|
return new IProperty[] {FACING, AGE};
|
|
}
|
|
|
|
public ModelBlock getModel(String name, State state) {
|
|
ModelBlock model;
|
|
switch(state.getValue(AGE)) {
|
|
case 0:
|
|
model = new ModelBlock("cocoa_0").noOcclude()
|
|
.add(6, 7, 11, 10, 12, 15)
|
|
.d().uv(0, 0, 4, 4).noCull()
|
|
.u().uv(0, 0, 4, 4).noCull()
|
|
.n().uv(11, 4, 15, 9).noCull()
|
|
.s().uv(11, 4, 15, 9).noCull()
|
|
.w().uv(11, 4, 15, 9).noCull()
|
|
.e().uv(11, 4, 15, 9).noCull()
|
|
.add(8, 12, 12, 8, 16, 16)
|
|
.w().uv(12, 0, 16, 4).noCull()
|
|
.e().uv(16, 0, 12, 4).noCull();
|
|
break;
|
|
case 1:
|
|
model = new ModelBlock("cocoa_1").noOcclude()
|
|
.add(5, 5, 9, 11, 12, 15)
|
|
.d().uv(0, 0, 6, 6).noCull()
|
|
.u().uv(0, 0, 6, 6).noCull()
|
|
.n().uv(9, 4, 15, 11).noCull()
|
|
.s().uv(9, 4, 15, 11).noCull()
|
|
.w().uv(9, 4, 15, 11).noCull()
|
|
.e().uv(9, 4, 15, 11).noCull()
|
|
.add(8, 12, 12, 8, 16, 16)
|
|
.w().uv(12, 0, 16, 4).noCull()
|
|
.e().uv(16, 0, 12, 4).noCull();
|
|
break;
|
|
case 2:
|
|
default:
|
|
model = new ModelBlock("cocoa_2").noOcclude()
|
|
.add(4, 3, 7, 12, 12, 15)
|
|
.d().uv(0, 0, 7, 7).noCull()
|
|
.u().uv(0, 0, 7, 7).noCull()
|
|
.n().uv(7, 4, 15, 13).noCull()
|
|
.s().uv(7, 4, 15, 13).noCull()
|
|
.w().uv(7, 4, 15, 13).noCull()
|
|
.e().uv(7, 4, 15, 13).noCull()
|
|
.add(8, 12, 12, 8, 16, 16)
|
|
.w().uv(12, 0, 16, 4).noCull()
|
|
.e().uv(16, 0, 12, 4).noCull();
|
|
break;
|
|
}
|
|
return model.rotate(ModelRotation.getNorthRot(state.getValue(FACING).getOpposite()));
|
|
}
|
|
}
|