Skip to content

Instantly share code, notes, and snippets.

@alcatrazEscapee
Created February 22, 2019 23:22
Show Gist options
  • Save alcatrazEscapee/cfab635649b77f72a7c22105d18c56c4 to your computer and use it in GitHub Desktop.
Save alcatrazEscapee/cfab635649b77f72a7c22105d18c56c4 to your computer and use it in GitHub Desktop.
import java.util.Random;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.dries007.tfc.world.classic.CalenderTFC;
public class FlowerGrowth
{
static
{
// Examples of creating flower with different stages:
// A flower with no growth stages
new Flower(new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1);
// Seasonal flower (four stages)
new Flower(new int[]{0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 0}, 4);
// Late bloomer?
new Flower(new int[]{0, 0, 0, 1, 2, 3, 4, 3, 0, 0, 0, 0}, 5);
}
public static class Flower
{
// Stages for growth - index 0 = march, 1 = april, ... 11 = jan etc.
// Synced so that stages[Month.id()] should work
private int[] stages;
private int numStages;
public Flower(int[] stages, int numStages)
{
this.stages = stages;
this.numStages = numStages;
}
public int[] getStages()
{
return stages;
}
public int getNumStages()
{
return numStages;
}
}
// This is the block class
@ParametersAreNonnullByDefault
public static class BlockFlower extends Block
{
private final IProperty<Integer> growthStages;
private final Flower flower;
public BlockFlower(Flower flower)
{
super(Material.PLANTS);
setTickRandomly(true);
this.flower = flower;
// Separate property for each flower since they might have different amounts of growth stages
this.growthStages = PropertyInteger.create("stage", 0, flower.getNumStages());
}
@Override
public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
{
super.randomTick(worldIn, pos, state, random);
// Check the current time against the current stage
CalenderTFC.Month currentMonth = CalenderTFC.getMonthOfYear();
int currentStage = state.getValue(growthStages);
int expectedStage = flower.getStages()[currentMonth.id()];
// If it is late and should grow, and some randomness
if (currentStage != expectedStage && random.nextDouble() < 0.5)
{
// Update to the next stage
worldIn.setBlockState(pos, state.withProperty(growthStages, expectedStage));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment