@SubscribeEvent
public void onSaplingGrow(SaplingGrowTreeEvent event)
{
World world = event.world;
LocalWorld localWorld = WorldHelper.toLocalWorld(world);
if (localWorld == null)
{
// World not managed by Terrain Control
return;
}
SaplingGrower saplingGrower = new SaplingGrower(localWorld, event.x, event.y, event.z);
if (saplingGrower.saplingType == null)
{
// Unsupported sapling
return;
}
// Get the sapling generator
SaplingGen saplingGen = this.getSaplingGen(localWorld, saplingGrower.saplingType, saplingGrower.x, saplingGrower.z);
if (saplingGen == null)
{
// No sapling generator set for this sapling
return;
}
// When we have reached this point, we know that we have to handle the
// event ourselves
// So cancel it
event.setResult(Result.DENY);
// Remove saplings
if (saplingGrower.saplingType.requiresFourSaplings())
{
world.setBlock(saplingGrower.x, saplingGrower.y, saplingGrower.z, Blocks.air);
world.setBlock(saplingGrower.x + 1, saplingGrower.y, saplingGrower.z, Blocks.air);
world.setBlock(saplingGrower.x, saplingGrower.y, saplingGrower.z + 1, Blocks.air);
world.setBlock(saplingGrower.x + 1, saplingGrower.y, saplingGrower.z + 1, Blocks.air);
} else
{
world.setBlock(saplingGrower.x, saplingGrower.y, saplingGrower.z, Blocks.air);
}
// Try ten times to grow sapling
boolean saplingGrown = false;
for (int i = 0; i < 10; i++)
{
if (saplingGen.growSapling(localWorld, new Random(), saplingGrower.x, saplingGrower.y, saplingGrower.z))
{
saplingGrown = true;
break;
}
}
if (!saplingGrown)
{
// Restore sapling
if (saplingGrower.saplingType.requiresFourSaplings())
{
localWorld.setBlock(saplingGrower.x, saplingGrower.y, saplingGrower.z, saplingGrower.material);
localWorld.setBlock(saplingGrower.x + 1, saplingGrower.y, saplingGrower.z, saplingGrower.material);
localWorld.setBlock(saplingGrower.x, saplingGrower.y, saplingGrower.z + 1, saplingGrower.material);
localWorld.setBlock(saplingGrower.x + 1, saplingGrower.y, saplingGrower.z + 1, saplingGrower.material);
} else
{
localWorld.setBlock(saplingGrower.x, saplingGrower.y, saplingGrower.z, saplingGrower.material);
}
}
}