Java Code Examples for net.minecraft.block.Block#canSustainPlant()

The following examples show how to use net.minecraft.block.Block#canSustainPlant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockRoseBush.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 5 votes vote down vote up
/**
 * Can this block stay at this position.  Similar to canPlaceBlockAt except gets checked often with plants.
 */
public boolean canBlockStay(World world, int x, int y, int z)
{
    if (world.getBlock(x, y, z) != this) return super.canBlockStay(world, x, y, z); //Forge: This function is called during world gen and placement, before this block is set, so if we are not 'here' then assume it's the pre-check.
    if(world.getBlockMetadata(x, y, z) == 0)
        return world.getBlock(x, y - 1, z) == this;
    Block block = world.getBlock(x, y - 1, z);
    return block.canSustainPlant(world, x, y - 1, z, ForgeDirection.UP, this);
}
 
Example 2
Source File: PlayerInteractEventHandler.java    From AgriCraft with MIT License 4 votes vote down vote up
/**
 * Event handler to disable vanilla farming.
 * 
 * @param event
 */
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void vanillaSeedPlanting(PlayerInteractEvent.RightClickBlock event) {
    // If not disabled, don't bother.
    if (!AgriCraftConfig.disableVanillaFarming) {
        return;
    }

    // Fetch the event itemstack.
    final ItemStack stack = event.getItemStack();

    // If the stack is null, or otherwise invalid, who cares?
    if (!StackHelper.isValid(stack)) {
        return;
    }

    // If the item in the player's hand is not a seed, who cares?
    if (!AgriApi.getSeedRegistry().hasAdapter(stack)) {
        return;
    }

    // Fetch world information.
    final BlockPos pos = event.getPos();
    final World world = event.getWorld();
    final IBlockState state = world.getBlockState(pos);

    // Fetch the block at the location.
    final Block block = state.getBlock();

    // If clicking crop block, who cares?
    if (block instanceof IAgriCrop) {
        return;
    }

    // If the item is an instance of IPlantable we need to perfom an extra check.
    if (stack.getItem() instanceof IPlantable) {
        // If the clicked block cannot support the given plant, then who cares?
        if (!block.canSustainPlant(state, world, pos, EnumFacing.UP, (IPlantable) stack.getItem())) {
            return;
        }
    }

    // If clicking crop tile, who cares?
    if (WorldHelper.getTile(event.getWorld(), event.getPos(), IAgriCrop.class).isPresent()) {
        return;
    }

    // The player is attempting to plant a seed, which is simply unacceptable.
    // We must deny this event.
    event.setUseItem(Event.Result.DENY);

    // If we are on the client side we are done.
    if (event.getSide().isClient()) {
        return;
    }

    // Should the server notify the player that vanilla farming has been disabled?
    if (AgriCraftConfig.showDisabledVanillaFarmingWarning) {
        MessageUtil.messagePlayer(event.getEntityPlayer(), "`7Vanilla planting is disabled!`r");
    }
}
 
Example 3
Source File: BlockBlackFlower.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 4 votes vote down vote up
@Override
public boolean canBlockStay(World world, int x, int y, int z) {
    Block block = world.getBlock(x, y - 1, z);
    return block.canSustainPlant(world, x, y - 1, z, ForgeDirection.UP, this);
}
 
Example 4
Source File: BlockPneumaticPlantBase.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
public boolean canPlantGrowOnThisBlock(Block block, World world, int x, int y, int z){
    return block.canSustainPlant(world, x, y, z, ForgeDirection.UP, this);
}
 
Example 5
Source File: BlockPneumaticPlantBase.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Gets the growth rate for the crop. Setup to encourage rows by halving
 * growth rate if there is diagonals, crops on different sides that aren't
 * opposing, and by adding growth for every crop next to this one (and for
 * crop below this one). Args: x, y, z
 */
protected float getGrowthRate(World world, int x, int y, int z){
    float growthFactor = 1.0F;
    Block var6 = world.getBlock(x, y, z - 1);
    Block var7 = world.getBlock(x, y, z + 1);
    Block var8 = world.getBlock(x - 1, y, z);
    Block var9 = world.getBlock(x + 1, y, z);
    Block var10 = world.getBlock(x - 1, y, z - 1);
    Block var11 = world.getBlock(x + 1, y, z - 1);
    Block var12 = world.getBlock(x + 1, y, z + 1);
    Block var13 = world.getBlock(x - 1, y, z + 1);
    boolean var14 = var8 == this || var9 == this;
    boolean var15 = var6 == this || var7 == this;
    boolean var16 = var10 == this || var11 == this || var12 == this || var13 == this;

    for(int var17 = x - 1; var17 <= x + 1; ++var17) {
        for(int var18 = z - 1; var18 <= z + 1; ++var18) {
            Block var19 = world.getBlock(var17, y - 1, var18);
            float var20 = 0.0F;

            if(var19.canSustainPlant(world, var17, y - (isPlantHanging() ? -1 : 1), var18, ForgeDirection.UP, this)) {
                var20 = 1.0F;

                if(var19.isFertile(world, var17, y - (isPlantHanging() ? -1 : 1), var18)) {
                    var20 = 3.0F;
                }
            }

            if(var17 != x || var18 != z) {
                var20 /= 4.0F;
            }

            growthFactor += var20;
        }
    }

    if(var16 || var14 && var15) {
        growthFactor /= 2.0F;
    }
    return growthFactor;
}