Search in sources :

Example 1 with FarmlandBlock

use of net.minecraft.block.FarmlandBlock in project BluePower by Qmunity.

the class ItemCropSeed method useOn.

@Override
public ActionResultType useOn(ItemUseContext itemUseContext) {
    PlayerEntity player = itemUseContext.getPlayer();
    Hand hand = itemUseContext.getHand();
    Direction facing = itemUseContext.getClickedFace();
    World world = itemUseContext.getLevel();
    BlockPos pos = itemUseContext.getClickedPos();
    ItemStack itemStack = player.getItemInHand(hand);
    if (facing.ordinal() != 1) {
        return ActionResultType.PASS;
    } else if (player.mayUseItemAt(pos, facing, itemStack) && player.mayUseItemAt(pos.above(), facing, itemStack)) {
        if (world.getBlockState(pos).getBlock().canSustainPlant(world.getBlockState(pos), world, pos, Direction.UP, this) && world.isEmptyBlock(pos.above()) && world.getBlockState(pos).getBlock() instanceof FarmlandBlock) {
            world.setBlock(pos.above(), field_150925_a.defaultBlockState(), 2);
            itemStack.setCount(itemStack.getCount() - 1);
            player.setItemInHand(hand, itemStack);
            return ActionResultType.SUCCESS;
        } else {
            return ActionResultType.PASS;
        }
    } else {
        return ActionResultType.PASS;
    }
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) FarmlandBlock(net.minecraft.block.FarmlandBlock) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Hand(net.minecraft.util.Hand) Direction(net.minecraft.util.Direction) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 2 with FarmlandBlock

use of net.minecraft.block.FarmlandBlock in project AgriCraft by AgriCraft.

the class TileEntitySprinkler method irrigateColumn.

/**
 * 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 independent 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.
 */
@SuppressWarnings("deprecation")
protected void irrigateColumn(final int targetX, final int targetZ, final int highestY, final int lowestY) {
    if (this.getWorld() == null || !(this.getWorld() instanceof ServerWorld)) {
        return;
    }
    for (int targetY = highestY; targetY >= lowestY; targetY -= 1) {
        BlockPos target = new BlockPos(targetX, targetY, targetZ);
        BlockState state = this.getWorld().getBlockState(target);
        Block block = state.getBlock();
        // Option A: Skip empty/air blocks.
        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().nextDouble() < AgriCraft.instance.getConfig().sprinkleGrowthChance()) {
                block.randomTick(state, (ServerWorld) this.getWorld(), target, this.getRandom());
            }
            continue;
        }
        // Option C: Dry farmland gets set as moist.
        if (block instanceof FarmlandBlock) {
            if (state.get(FarmlandBlock.MOISTURE) < 7) {
                this.getWorld().setBlockState(target, state.with(FarmlandBlock.MOISTURE, 7), 2);
            }
            // Explicitly expresses the intent to stop.
            break;
        }
        // Option D: If it's none of the above, it blocks the sprinkler's irrigation. Stop.
        break;
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) BlockState(net.minecraft.block.BlockState) IGrowable(net.minecraft.block.IGrowable) IPlantable(net.minecraftforge.common.IPlantable) Block(net.minecraft.block.Block) FarmlandBlock(net.minecraft.block.FarmlandBlock) BlockPos(net.minecraft.util.math.BlockPos) FarmlandBlock(net.minecraft.block.FarmlandBlock)

Aggregations

FarmlandBlock (net.minecraft.block.FarmlandBlock)2 BlockPos (net.minecraft.util.math.BlockPos)2 Block (net.minecraft.block.Block)1 BlockState (net.minecraft.block.BlockState)1 IGrowable (net.minecraft.block.IGrowable)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 ItemStack (net.minecraft.item.ItemStack)1 Direction (net.minecraft.util.Direction)1 Hand (net.minecraft.util.Hand)1 World (net.minecraft.world.World)1 ServerWorld (net.minecraft.world.server.ServerWorld)1 IPlantable (net.minecraftforge.common.IPlantable)1