下面列出了怎么用net.minecraftforge.common.IPlantable的API类实例代码及写法,或者点击链接到github查看源代码。
/*******************************************************************************
* 1. Content
*******************************************************************************/
@Override
public void updateTick(World world, BlockPos pos, IBlockState state, Random rand)
{
if(world.isRemote)
return;
if(!(world.getBlockState(pos.up()).getBlock() instanceof IPlantable))
{
if(!isFertile(world, pos) && rand.nextInt(100) == 0)
{
world.setBlockState(pos, TFCBlocks.Dirt.getDefaultState().withProperty(META_PROPERTY, state.getValue(META_PROPERTY)));
}
}
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
net.minecraftforge.common.EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
if(plantType == EnumPlantType.Crop)
return true;
if(plantType == EnumPlantType.Plains)
return true;
if(plantable == TFCBlocks.Sapling)
return true;
return false;
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
VegType veg = (VegType)state.getValue(META_PROPERTY);
if(plant.getBlock() == this)
{
if(veg == VegType.DoubleGrassBottom && plant.getValue(META_PROPERTY) == VegType.DoubleGrassTop)
return true;
if(veg == VegType.DoubleGrassBottomLush && plant.getValue(META_PROPERTY) == VegType.DoubleGrassTopLush)
return true;
}
return false;
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
net.minecraftforge.common.EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
if(plant.getBlock() == Blocks.SAPLING)
return false;//This may break some cross mod compatability but for now its needed to prevent vanilla and some pam trees from generating
if(plantType == EnumPlantType.Plains)
return true;
if(plant.getBlock() == TFCBlocks.VegDesert)
return true;
return false;
}
private void init () {
registerCompostMaterial(new ItemStack(Blocks.melon_block), defaultMaterial);
registerCompostMaterial(new ItemStack(Blocks.pumpkin), defaultMaterial);
registerCompostMaterial(new ItemStack(Blocks.hay_block), defaultMaterial);
registerCompostMaterial(new ItemStack(Items.string), new StandardCompostMaterial(100, 0.0625f));
registerCompostMaterial(new ItemStack(Items.wheat), new StandardCompostMaterial(100, 0.125f));
registerCompostMaterial(new ItemStack(Items.reeds), new StandardCompostMaterial(150, 0.125f));
registerCompostMaterial(new ItemStack(Items.feather), new StandardCompostMaterial(50, 0.0625f));
registerCompostMaterial(new ItemStack(Items.rotten_flesh), new StandardCompostMaterial(150, 0.125f));
registerCompostMaterial(new ItemStack(Items.leather), new StandardCompostMaterial(150, 0.125f));
registerCompostMaterial("treeWood", new StandardCompostMaterial(300, 0.25f));
registerCompostMaterial("logWood", new StandardCompostMaterial(300, 0.25f));
registerCompostMaterial("treeLeaves", defaultMaterial);
registerCompostMaterial("treeSapling", defaultMaterial);
registerCompostMaterial("stickWood", defaultMaterial);
registerCompostMaterial(IPlantable.class, defaultMaterial);
registerCompostMaterial(IGrowable.class, defaultMaterial);
registerCompostMaterial(BlockLeavesBase.class, defaultMaterial);
registerCompostMaterial(BlockVine.class, defaultMaterial);
registerCompostMaterial(ItemFood.class, defaultMaterial);
}
public static PlantItem getForItem (IBlockAccess blockAccess, ItemStack itemStack) {
if (itemStack == null || itemStack.getItem() == null)
return null;
IPlantable plantable = PlantRegistry.getPlantable(itemStack);
if (plantable == null)
return getForItem(itemStack);
Block block = plantable.getPlant(blockAccess, 0, -1, 0);
if (block == null)
return getForItem(itemStack);
int meta = plantable.getPlantMetadata(blockAccess, 0, -1, 0);
if (meta == 0)
meta = itemStack.getItemDamage();
return new PlantItem(itemStack, block, meta);
}
@Override
public void onUpdate(){
oldMotionX = motionX;
oldMotionY = motionY;
oldMotionZ = motionZ;
super.onUpdate();
//blowOtherEntities();
motionX *= 0.95D;// equal to the potion effect friction. 0.95F
motionY *= 0.95D;
motionZ *= 0.95D;
if(motionX * motionX + motionY * motionY + motionZ * motionZ < 0.1D) {
setDead();
}
if(!worldObj.isRemote) {
int blockX = (int)Math.floor(posX);
int blockY = (int)Math.floor(posY);
int blockZ = (int)Math.floor(posZ);
for(int i = 0; i < 7; i++) { // to 7 so the middle block will also trigger (with UNKNOWN direction)
Block block = worldObj.getBlock(blockX + ForgeDirection.getOrientation(i).offsetX, blockY + ForgeDirection.getOrientation(i).offsetY, blockZ + ForgeDirection.getOrientation(i).offsetZ);
if(block instanceof IPlantable || block instanceof BlockLeaves) {
worldObj.func_147480_a(blockX + ForgeDirection.getOrientation(i).offsetX, blockY + ForgeDirection.getOrientation(i).offsetY, blockZ + ForgeDirection.getOrientation(i).offsetZ, true);
}
}
}
}
/**
* Returns a boolean indicating whether or not the current location for the tree, spanning basePos to to the height
* limit, is valid.
*/
private boolean validTreeLocation()
{
BlockPos down = this.basePos.down();
net.minecraft.block.state.IBlockState state = this.world.getBlockState(down);
boolean isSoil = state.getBlock().canSustainPlant(state, this.world, down, net.minecraft.util.EnumFacing.UP, ((IPlantable)BlockLoader.SAKURA_SAPLING));
if (!isSoil)
{
return false;
}
int i = this.checkBlockLine(this.basePos, this.basePos.up(this.heightLimit - 1));
if (i == -1)
{
return true;
}
else if (i < 6)
{
return false;
}
else
{
this.heightLimit = i;
return true;
}
}
/**
* Returns a boolean indicating whether or not the current location for the tree, spanning basePos to to the height
* limit, is valid.
*/
private boolean validTreeLocation()
{
BlockPos down = this.basePos.down();
net.minecraft.block.state.IBlockState state = this.world.getBlockState(down);
boolean isSoil = state.getBlock().canSustainPlant(state, this.world, down, net.minecraft.util.EnumFacing.UP, ((IPlantable)metaSapling.getBlock()));
if (!isSoil)
{
return false;
}
int i = this.checkBlockLine(this.basePos, this.basePos.up(this.heightLimit - 1));
if (i == -1)
{
return true;
}
else if (i < 6)
{
return false;
}
else
{
this.heightLimit = i;
return true;
}
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction,
IPlantable plantable) {
return state.getMaterial() == Material.GROUND || state.getMaterial() == Material.GRASS
|| (state.getBlock() instanceof BlockPepperCrop && this.getAge(state) >= 2)
|| (state.getBlock() instanceof BlockPepperSplint);
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction,
IPlantable plantable) {
return state.getMaterial() == Material.GROUND || state.getMaterial() == Material.GRASS
|| (state.getBlock() instanceof BlockVanillaCrop && this.getAge(state) >= 2)
|| (state.getBlock() instanceof BlockVanillaSplint);
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction,
IPlantable plantable) {
return state.getBlock() == Blocks.FARMLAND||
(this.getAge(state)>=4&&state.getBlock() instanceof BlockHighCrop
&&world.getBlockState(pos.down()).getBlock() == Blocks.FARMLAND);
}
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable) {
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
if (plant.getBlock() == BlockLoader.BAMBOO) {
return this == BlockLoader.BAMBOO;
}
return super.canSustainPlant(state, world, pos, direction, plantable);
}
/**
* Determines if this block can support the passed in plant, allowing it to be planted and grow.
* Some examples:
* Reeds check if its a reed, or if its sand/dirt/grass and adjacent to water
* Cacti checks if its a cacti, or if its sand
* Nether types check for soul sand
* Crops check for tilled soil
* Caves check if it's a solid surface
* Plains check if its grass or dirt
* Water check if its still water
*
* @param state The Current state
* @param world The current world
* @param facing The direction relative to the given position the plant wants to be, typically its UP
* @param plantable The plant that wants to check
* @return True to allow the plant to be planted/stay.
*/
default boolean canSustainPlant(BlockState state, BlockView world, BlockPos pos, Direction facing, IPlantable plantable) {
BlockState plant = plantable.getPlant(world, pos.offset(facing));
if (plant.getBlock() == Blocks.CACTUS) {
return this.getBlock() == Blocks.CACTUS || this.getBlock() == Blocks.SAND || this.getBlock() == Blocks.RED_SAND;
}
if (plant.getBlock() == Blocks.SUGAR_CANE && this.getBlock() == Blocks.SUGAR_CANE) {
return true;
}
if (plantable instanceof PlantBlock && ((PlantBlockAccessor) plantable).invokeCanPlantOnTop(state, world, pos)) {
return true;
}
switch (plantable.getPlantType(world, pos)) {
case Desert:
return this.getBlock() == Blocks.SAND || this.getBlock() == Blocks.TERRACOTTA || this.getBlock() instanceof GlazedTerracottaBlock;
case Nether:
return this.getBlock() == Blocks.SOUL_SAND;
case Crop:
return this.getBlock() == Blocks.FARMLAND;
case Cave:
return Block.isSideSolidFullSquare(state, world, pos, Direction.UP);
case Plains:
return this.getBlock() == Blocks.GRASS_BLOCK || Block.isNaturalDirt(this.getBlock()) || this.getBlock() == Blocks.FARMLAND;
case Water:
return state.getMaterial() == Material.WATER;
case Beach:
boolean isBeach = this.getBlock() == Blocks.GRASS_BLOCK || Block.isNaturalDirt(this.getBlock()) || this.getBlock() == Blocks.SAND;
boolean hasWater = (world.getBlockState(pos.east()).getMaterial() == Material.WATER
|| world.getBlockState(pos.west()).getMaterial() == Material.WATER
|| world.getBlockState(pos.north()).getMaterial() == Material.WATER
|| world.getBlockState(pos.south()).getMaterial() == Material.WATER);
return isBeach && hasWater;
}
return false;
}
@Redirect(method = "onScheduledTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;getBlock()Lnet/minecraft/block/Block;"))
private Block redirectSoilCheck(BlockState blockState) {
if (((IForgeBlockState) blockState).canSustainPlant(currentWorld.get(), currentBlockPos.get().down(), Direction.UP, (IPlantable) this)) {
return Blocks.DIRT;
}
return blockState.getBlock();
}
@Inject(method = "hasCrop", cancellable = true, at = @At("HEAD"))
private static void onHasCrop(BlockView world, BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
final BlockState ourState = world.getBlockState(pos);
final BlockState cropState = world.getBlockState(pos.up());
if (cropState.getBlock() instanceof IPlantable && ((IForgeBlockState) ourState).canSustainPlant(world, pos, Direction.UP, (IPlantable) cropState.getBlock())) {
cir.setReturnValue(true);
cir.cancel();
}
}
public static boolean isSeedItem(Item item) {
if (item instanceof IPlantable) {
return true;
}
if (item == Items.REEDS) {
return true;
}
return false;
}
public static IBlockState getBlockStateFromItemStackForPlanting(ItemStack itemStack, World world, BlockPos pos) {
if (itemStack.getItem() == Items.WHEAT_SEEDS) {
return Blocks.WHEAT.getDefaultState();
}
if (itemStack.getItem() == Items.POTATO) {
return Blocks.POTATOES.getDefaultState();
}
if (itemStack.getItem() == Items.CARROT) {
return Blocks.CARROTS.getDefaultState();
}
if (itemStack.getItem() == Items.BEETROOT_SEEDS) {
return Blocks.BEETROOTS.getDefaultState();
}
if (itemStack.getItem() == Items.REEDS) {
return Blocks.REEDS.getDefaultState();
}
if (itemStack.getItem() == Items.PUMPKIN_SEEDS) {
return Blocks.PUMPKIN_STEM.getDefaultState();
}
if (itemStack.getItem() == Items.MELON_SEEDS) {
return Blocks.MELON_STEM.getDefaultState();
}
if (itemStack.getItem() instanceof IPlantable) {
return ((IPlantable) itemStack.getItem()).getPlant(world, pos);
}
return null;
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
EnumPlantType type = plantable.getPlantType(world, pos.offset(direction));
EnumPlantType[] sustainedPlants = getContent().sustainedPlants.get(getSubtype(state)).orElse(null);
if (sustainedPlants != null)
{
return ArrayUtils.contains(sustainedPlants, type);
} else
{
return super.canSustainPlant(state, world, pos, direction, plantable);
}
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
if(plantable == TFCBlocks.Vegetation && (VegType)plant.getValue(BlockVegetation.META_PROPERTY) == VegType.Grass)
return true;
return false;
}
/*******************************************************************************
* 1. Content
*******************************************************************************/
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
net.minecraftforge.common.EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
if(plantType == EnumPlantType.Plains)
return true;
return false;
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
DesertCactusType veg = (DesertCactusType)state.getValue(META_PROPERTY);
if(plant.getBlock() == this)
{
}
return false;
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable)
{
IBlockState plant = plantable.getPlant(world, pos.offset(direction));
EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
DesertVegType veg = (DesertVegType)state.getValue(META_PROPERTY);
if(plant.getBlock() == this)
{
if(veg == DesertVegType.DoubleGrassBottomSparse && plant.getValue(META_PROPERTY) == DesertVegType.DoubleGrassTopSparse)
return true;
}
return false;
}
/**
* This method will search through a vertical column of positions, starting from the top. It
* will stop searching any lower once it hits anything other than air or plants. Any plant found
* has an independant chance for a growth tick. That percentage is controlled by
* AgriCraftConfig. Farmland also ends the search, but it first has its moisture set to max (7)
* if it isn't already. The lowest position is special: a plant this far away is not helped.
* Only farmland is currently.
*/
private void irrigateColumn(final int targetX, final int targetZ, final int highestY, final int lowestY) {
for (int targetY = highestY; targetY >= lowestY; targetY -= 1) {
BlockPos target = new BlockPos(targetX, targetY, targetZ);
IBlockState state = this.getWorld().getBlockState(target);
Block block = state.getBlock();
// Option A: Skip empty/air blocks.
// TODO: Is there a way to use isSideSolid to ignore minor obstructions? (Farmland isn't solid.)
if (block.isAir(state, this.getWorld(), target)) {
continue;
}
// Option B: Give plants a chance to grow, and then continue onward to irrigate the farmland too.
if ((block instanceof IPlantable || block instanceof IGrowable) && targetY != lowestY) {
if (this.getRandom().nextInt(100) < AgriCraftConfig.sprinklerGrowthChance) {
block.updateTick(this.getWorld(), target, state, this.getRandom());
}
continue;
}
// Option C: Dry farmland gets set as moist.
if (block instanceof BlockFarmland) {
if (state.getValue(BlockFarmland.MOISTURE) < 7) {
this.getWorld().setBlockState(target, state.withProperty(BlockFarmland.MOISTURE, 7), 2);
}
break; // Explicitly expresses the intent to stop.
}
// Option D: If it's none of the above, it blocks the sprinkler's irrigation. Stop.
break;
}
}
private boolean plantItemFromInventorySlot(World world, EntityPlayer player, EnumHand hand,
IItemHandler inv, int slot, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
boolean ret = false;
ItemStack plantStack = inv.getStackInSlot(slot);
if (plantStack.isEmpty() == false && plantStack.getItem() instanceof IPlantable)
{
plantStack = inv.extractItem(slot, 1, false);
if (plantStack.isEmpty())
{
return false;
}
ItemStack stackHand = player.getHeldItem(hand);
EntityUtils.setHeldItemWithoutEquipSound(player, hand, plantStack);
if (plantStack.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ) == EnumActionResult.SUCCESS)
{
ret = true;
}
EntityUtils.setHeldItemWithoutEquipSound(player, hand, stackHand);
if (plantStack.isEmpty() == false)
{
plantStack = InventoryUtils.tryInsertItemStackToInventory(inv, plantStack);
if (plantStack.isEmpty() == false)
{
player.dropItem(plantStack, false, true);
}
}
player.inventoryContainer.detectAndSendChanges();
}
return ret;
}
@Override
public boolean canSustainPlant (IBlockAccess world, int x, int y, int z, ForgeDirection direction, IPlantable plantable) {
TileEntityGarden gardenTile = getTileEntity(world, x, y, z);
EnumPlantType plantType = plantable.getPlantType(world, x, y, z);
if (plantType == EnumPlantType.Crop)
return substrateSupportsCrops(gardenTile.getSubstrate());
return false;
}
public static PlantItem getForItem (ItemStack itemStack) {
if (itemStack == null || itemStack.getItem() == null)
return null;
Block block = Block.getBlockFromItem(itemStack.getItem());
if (block == null || !(block instanceof IPlantable))
return null;
return new PlantItem(itemStack, block, itemStack.getItemDamage());
}
public static Block getPlantBlock (IPlantable plant) {
try {
return getPlantBlock(null, 0, 0, 0, plant);
}
catch (Exception e) {
return null;
}
}
public static int getPlantMetadata (IPlantable plant) {
try {
return getPlantMetadata(null, 0, 0, 0, plant);
}
catch (Exception e) {
return 0;
}
}
private static int getPlantMetadata (IBlockAccess world, int x, int y, int z, IPlantable plant, int defaultMeta) {
if (plant == null)
return 0;
Block itemBlock = plant.getPlant(world, x, y, z);
int itemMeta = defaultMeta;
if (itemBlock != null) {
int plantMeta = plant.getPlantMetadata(world, x, y, z);
if (plantMeta > 0)
itemMeta = plantMeta;
}
return itemMeta;
}